O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Caching

The serving of content in the first iteration of the web server was rather inefficient—I hope you noticed! On every GET request, the blog source file is read from the disk and is processed. To gain some efficiency, we can use a map added to the Blog class as a field. Let's take a look at the following code snippet for more information:

    BlogPost getBlogPost(int index) {
    if (!_cache.containsKey(index)) {
      _cache[index] = new BlogPost(postPaths[index], index);
    }
    return _cache[index];
  }

The map object uses the blog index as a key and the resultant blog post as the value. This cache is kept in the memory so that if the blog server application is stopped and restarted, new requests will not initially come from the cache.

Note

Caching of content ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required