- Dart:Scalable Application Development
- Davy Mitchell Sergey Akopkokhyants Ivo Balbaert
- 255字
- 2021-07-09 18:56:23
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 in web applications is quite an art. For example, a cache could be put in place at the request level for both entire pages and images. The underlying operating system will also cache files in memory, making operations much faster on subsequent requests.
As ever, a memory cache is just one tool that may or may not improve a particular application. Benchmarking a performance and finding the bottleneck that needs caching is critical for a high-performance application.
The cache is something that must be remembered when the content is refreshed and when a new blog post is added. A simple strategy is to set the _cache
object to {}
(an empty map), relying on the first display of the post to fill the cache.
- C++面向對象程序設計(第三版)
- UNIX編程藝術
- AngularJS入門與進階
- vSphere High Performance Cookbook
- 動手玩轉Scratch3.0編程:人工智能科創教育指南
- Wireshark Network Security
- 區塊鏈:以太坊DApp開發實戰
- Magento 1.8 Development Cookbook
- C語言程序設計學習指導與習題解答
- HTML5+CSS3網頁設計
- C語言程序設計
- Spring快速入門
- Raspberry Pi Home Automation with Arduino(Second Edition)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- 響應式Web設計:HTML5和CSS3實戰(第2版)