1. Cache caching is a temporary way to access data, because fetching the raw data is too expensive, and caching can make it faster. The cache can be thought of as a subset of the original data, copied from the original data, and added to the flag in order to be retrieved.
In Android development, often access to network data such as a large number of network images, if you need the same image every time to go to the network, the cost is obviously too large. Consider setting up local file cache and memory cache to store data obtained from the network; local file cache space is not infinite, the larger the capacity, the lower the read efficiency, you can set a compromise cache capacity such as 10M, if the cache is full, we need to replace an existing data object with the appropriate replacement strategy, and has a new data object; the memory cache, as the first data to be read, should store those data objects that are used frequently, with limited memory capacity, and the capacity of the memory cache should be qualified. In this way, the process of getting a picture (the total number of images is N) should look like this:
A. First in the memory cache (set storage k), if the fetch is returned (hit rate is k/n, time is TA), otherwise B;
B. In the local file cache (set can store M), if it is taken to return and update the memory cache (hit rate is (m-k)/n, time is TB), otherwise C;
c. Download pictures over the network and update the local file cache and memory cache (hit rate is (n-m)/N, time is TC);
The time expected to take a picture is: W = TA * (k/n) + TB * (m-k)/n + TC * (n-m)/n, where TA < TB < TC, in order to make the W cost small, that is, as soon as possible to obtain data, we should improve the memory cache hit rate and local file cache hit Rate, but the capacity of both is limited, so you must use the appropriate replacement algorithm to update the objects stored by both. Choosing the appropriate replacement algorithm is the difficulty of caching.
2. Common cache algorithm least frequently Used (LFU)
Calculates the frequency at which they are used for each cached object. Swap out the least commonly used cache objects.
Least recently User (LRU)
Swap out the least recently used cache object. There is always a need to know when and which cache object to use. It is very difficult for someone to understand why the least recently used objects are always kicked off. The browser is using LRU as the caching algorithm. The new object will be placed at the top of the cache, and when the cache reaches the capacity limit, I'll kick the bottom object, and the trick is: I'll put the most recently accessed cache object on top of the cache pool.
As a result, cached objects that are often read remain in the cache pool. Can be implemented with data or linked lists. The improved algorithm has LRU2 and 2Q.
Least Recently used 2 (LRU2)
Put the objects that have been accessed two times into the cache pool, and when the cache pool is full, I will kick off the cache object with two times of minimum use. Because the object needs to be tracked 2 times, the access load increases as the cache pool increases. If you are using a large cache pool, there is a problem. Also, keep track of objects that are not cached because they have not been read a second time. It's better than LRU.
Queues (2Q)
Put the accessed data into the LRU cache, and if the object is accessed again, transfer it to the second larger LRU cache. The cache object is replaced to maintain that the first cache pool is 1/3 of the second cache pool. When the cached access load is fixed, it is better to change the LRU to LRU2 than to increase the capacity of the cache. This mechanism makes the algorithm better than LRU2.
Adaptive replacement Cache (ARC)
This algorithm is between LRU and LFU, consisting of 2 LRU, the first, that is, L1, contains entries that have been used only once, while the second LRU, or L2, contains entries that have been used twice recently. As a result, L1 puts new objects in place, while L2 objects are commonly used. The algorithm is one of the best-performing caching algorithms, capable of self-tuning and low load. Save the historical objects so that you can remember the objects that were removed, and you can see whether the replaced objects can be left behind, instead of replacing them with other objects. The algorithm has a poor memory, but soon, the applicability is also strong.
Most recently Used (MRU)
The algorithm is corresponding to LRU. It replaces the most recently used objects, and you are sure to ask why. The reason is that when a visit comes in, something is unpredictable, and finding the least recently used object in the cache system is a very time-complex operation. This algorithm is very much in the database memory cache! Whenever a cache record is used, it is placed at the top of the stack. When the stack is full, the object of the top of the stack will be replaced with the new incoming object!
First In first out (FIFO)
This is a low-load algorithm and does not require a high level of management for cached objects. All cached objects are tracked through a queue, the most recently used cache objects are placed behind, and the earlier cache objects are placed in front of them, and when the cache is full, the cache objects that precede them are kicked away and the new cache objects are added. Soon, but not applicable.
Second Chance
The improved FIFO algorithm, which is better than FIFO, is to improve the FIFO cost. The same is observed in the front of the queue, but very FIFO of the immediate replacement, it will check the object to be kicked out has been used before the flag (11 bit), if not been used, he swapped out, otherwise, the flag bit cleared, This cache object is then added to the queue as a new cache object. As you can imagine, it's like a ring queue. Once again when the team head encounters this object, because it has no sign, you can immediately swap it out. Faster than FIFO in speed.
CLock
This is a better FIFO and better than second chance. Because it does not put a flagged cache object at the end of the queue like second chance, it can also achieve the effect of second chance. It holds a ring list of cached objects, and the head pointer points to the oldest cached object in the list. When the cache miss occurs and there is no new cache space, it determines what should be done based on the flag bit of the cached object pointed to by the pointer. If the flag is 0, replace the cached object directly with the new cache object, and if the flag bit is 1, increment the head pointer and repeat the process until the new cache object can be placed.
Simple time-based
The cache objects are invalidated through an absolute time period. For new objects, save a specific time. Soon, but not applicable.
Extended time-based Expiration
Cache objects are invalidated by relative time, and for new cache objects, save a specific time, such as 12 points per day for every 5 minutes.
Sliding time-based Expiration
The life starting point of the managed cache object is the last accessed time of the cache. Soon, it's not very suitable.
The cache algorithm mainly considers the following points:
Cost. If the cache objects have different costs, you should save the hard-to-get objects.
Capacity. If the cache objects are of different sizes, you should clear those large cache objects so that more small cache objects can be brought in.
Time. Some caches also hold the cache expiration time. The computer will fail them because they have expired.
3. Image caching framework for enhanced user experience see a network interface and picture caching framework enif on Android.
- Previous caution to get the height and width of the control to prevent unnecessary performance problems
- Next Web service and RPC differences
Caching, caching algorithms, and caching frameworks on Android