Original address: http://www.360doc.com/content/13/0805/15/13247663_304901967.shtml
Reference address (a series of cache-related, followed by a few are also here): http://www.360doc.com/userhome.aspx?userid=13247663&cid=48#
1. LRU
1.1. Principle
The core idea of the LRU (Least recently used, least recently used) algorithm is to retire data based on the historical access records of the data, with the heart being that "if the data has been accessed recently, the chances of being accessed in the future are higher".
1.2. Implement
The most common implementation is to use a linked list to save the cached data, the detailed algorithm is implemented as follows:
1. Inserting new data into the list head;
2. Whenever the cache hits (that is, the cached data is accessed), the data is moved to the list header;
3. When the list is full, discard the data at the end of the list.
1.3. Analysis
Hit rate
When there is hot data, LRU efficiency is very good, but the occasional, periodic batch operation will cause the LRU hit rate drops sharply, the cache pollution is more serious.
"Complexity"
Simple to implement.
Cost
A hit will need to traverse the linked list, find the hit block index, and then need to move the data to the head.
2. Lru-k
2.1. Principle
The k in Lru-k represents the number of recent uses, so LRU can be considered as LRU-1. The main purpose of lru-k is to solve the problem of "cache pollution" of LRU algorithm, whose core idea is to extend the criterion of "recently used 1 times" to "recently used K-Times".
2.2. Implement
There is a need to maintain a queue more than lru,lru-k to record the history of all cached data being accessed. Data is placed in the cache only when the number of accesses to the data has reached K times. When you need to retire data, Lru-k will eliminate the data that is the largest of the K access time from the current time. The detailed implementation is as follows:
1. Data is accessed for the first time and added to the Access History list;
2. If the data does not reach the K-visit after accessing the history list, it will be phased out according to certain rules (FIFO,LRU);
3. When the number of data accesses in the history queue reaches K times, the data index is deleted from the history queue, the data is moved to the cache queue, and the data is cached, and the cache queue is sorted again by time;
4. The cache data queue is re-accessed and reordered;
5. In the case of data elimination, the data at the end of the cache queue is eliminated, that is, the "last-second-most-visited" data is eliminated.
Lru-k has the advantages of LRU, at the same time can avoid the shortcomings of LRU, the actual application of LRU-2 is a combination of various factors after the optimal choice, LRU-3 or greater K-value hit rate will be high, but the adaptability is poor, need a large number of data access to the history of access records erased.
2.3. Analysis
Hit rate
Lru-k reduces the problem caused by "cache pollution", which is higher than LRU.
"Complexity"
Lru-k queue is a priority queue, the algorithm complexity and cost is relatively high.
Cost
Because Lru-k also needs to record objects that have been accessed but not yet cached, memory consumption is much higher than LRU, and memory consumption can be significant when the amount of data is large.
The lru-k needs to be sorted based on time (which can be sorted when it is retired or sorted instantly), and CPU consumption is higher than LRU.
3. Queues (2Q)
3.1. Principle
Two queues (the following 2Q substitution) algorithm is similar to LRU-2, except that 2Q will change the access history queue in the LRU-2 algorithm (note that this is not cached data) to a FIFO cache queue, that is: The 2Q algorithm has two cache queues, one is the FIFO queue, and the other is the LRU queue.
3.2. Implement
When the data is first accessed, the 2Q algorithm caches the data in the FIFO queue, and when the data is accessed for the second time, the data is moved from the FIFO queue to the LRU queue, and two queues each retire their data in their own way. The detailed implementation is as follows:
1. The newly accessed data is inserted into the FIFO queue;
2. If the data has not been re-accessed in the FIFO queue, it will eventually be phased out according to FIFO rules;
3. If the data is accessed again in the FIFO queue, the data is moved to the LRU queue header;
4. If the data is accessed again in the LRU queue, the data is moved to the LRU queue header;
5. The LRU queue eliminates data at the end.
Note: The FIFO queue is shorter than the LRU queue, but it does not mean that this is the algorithm requirements, the actual application of the ratio is not rigidly defined.
3.3. Analysis
Hit rate
The 2Q algorithm has a higher hit rate than LRU.
"Complexity"
Requires two queues, but the two queues themselves are relatively simple.
Cost
The sum of the cost of FIFO and LRU.
The 2Q algorithm is similar to the LRU-2 algorithm hit rate and memory consumption is close, but for the last cached data, 2Q reduces the amount of time it takes to read from or compute data from the original storage.
4. Multi Queue (MQ)
4.1. Principle
The MQ algorithm divides the data into multiple queues according to the frequency of access, and the different queues have different access priorities, and the core idea is to prioritize cache access to more data.
4.2. Implement
The MQ algorithm divides the cache into multiple LRU queues, each of which corresponds to a different access priority. Access priority is calculated based on the number of accesses, such as
The detailed algorithm structure diagram is as follows, q0,q1 .... QK represents a different priority queue, Q-history represents a queue that eliminates data from the cache, but records the index and number of citations of the data:
For example, the algorithm is described in detail as follows:
1. The newly inserted data is put into Q0;
2. Each queue is managed according to LRU data;
3. When the number of accesses to the data reaches a certain number of times, it is necessary to increase the priority, delete the data from the current queue, add to the head of the high-level queue;
4. In order to prevent high-priority data from ever being eliminated, when the data is not accessed during the specified time, the priority needs to be lowered, the data removed from the current queue and added to the lower-level queue header;
5. When data is required to be phased out, the lowest-level queue begins with LRU elimination; When each queue is retired, data is removed from the cache, and the data index is added to the q-history header;
6. If the data is re-accessed in Q-history, its priority is recalculated and moved to the head of the target queue;
7. The q-history is indexed according to the LRU phase-out data.
4.3. Analysis
Hit rate
MQ reduces the problem of "cache pollution", which is higher than LRU.
"Complexity"
MQ needs to maintain multiple queues and maintain access times for each data, which is more complex than LRU.
Cost
MQ needs to record the access time for each data, and it needs to scan all queues regularly, at a higher cost than LRU.
Note: Although the MQ queue looks a lot more, the sum of all the queues is limited by the size of the cache capacity, so there is the same number of queue lengths here as an LRU queue, so the queue scan performance is similar.
5. LRU Class Algorithm comparison
Since different access models result in a large variation in hit ratios, the comparison is based only on theoretical qualitative analysis and no quantitative analysis.
Contrast point |
Contrast |
Shooting |
LRU-2 > MQ (2) > 2Q > LRU |
Complexity of |
LRU-2 > MQ (2) > 2Q > LRU |
Price |
LRU-2 > MQ (2) > 2Q > LRU |
The actual application needs to be based on the needs of the business and data access to choose, not the higher the ratio of the better. For example, although LRU appears to be less hit, and there is a "cache pollution" problem, but because of its simplicity and low cost, practical applications instead of more.
1--lru class of "turn" cache elimination algorithm series