The original text is also published in the author's personal blog, Welcome to visit: http://www.koulianbing.com/?p=23
In the process of running the app, many random, fragmented and unstructured data are generated, which is a difficult point in data management, which is important and changeable in data volume. Here to share some of the many multi-level cache strategies that I have used in recent years.
In front of these questions mentioned above, comb the goals we are going to achieve: random access, unstructured data, and data are ranked by importance .
1, in order to achieve random access, need to establish a level two cache. Define the memory cache for the fixed-size LRU policy, read the data from the memory cache first, and then read from Flash If there is no hit. When storing data, write to memory cache, and then refresh the data to flash in the background. As for the memory cache implementation, Android above the Lrucache,ios above the Nscache is a good solution.
To avoid data conflicts, version compatibility issues, you need to set a unique key for each access. Depending on the size of the cache data, there are two options for the storage policy on flash. If you need less data for the cache, you can create a file for each item in the cache, and a mapping between the key and the file path. If you need to cache more and smaller data, you can store all the data in a file, the data in the location of the file to establish the index. Depending on the situation, you can see which strategy to use. 2, because the data structure as the operational target is changeable, I usually choose Binary as the access target. 3, the data according to the importance of can be lost and permanent, respectively, the establishment of different flash storage strategy. Control the amount of cached data for data that can be lost, and avoid too much cache file size. Here are a few places where bottlenecks can occur: 1, read the data, if not hit the memory cache, will be read directly from the flash above. There is a risk of UI threading operating IO, and if poorly managed, there may be a phenomenon of inexplicable lag. Each time this happens, I record the IO time and set the security threshold, and if the threshold is exceeded, the data is escalated. From the data observation, this does not constitute a realization problem, which is inseparable from the increasingly perfect flash technology, after all, the quality of the handset ROM is a few years ago the market everywhere fly dozens of dollars sdcard can not compare. 2. Flush the data from the memory cache to the frequency control of the flash. The author in the use of the process, did not encounter a short time cache a large number of updates, there is no centralized outbreak of the flush task, using the background single task execution queue enough to avoid the memory fluctuations, but also to circumvent the flush file operation synchronization problem. 3, if in a more special business scenario encountered some cache data is particularly large, it is recommended to use normal file access logic, honestly display loading, and then open the background thread to perform the task. 4, data synchronization is a common but important problem, need to do a good job of prevention.
Multi-level cache policy in mobile apps