Cache Algorithm – LRU

LRU

         LRU is the abbreviation of Least Recently Used, which translates as "least recently used", that is to say, the LRU cache removes the least recently used data and gives it to the latest read data. And often the most frequently read is also the most frequently read, so by using LRU cache, we can improve the performance of the system.

   1. Insert new data into the head of the linked list;

   2. Whenever the cache hits (that is, the cached data is accessed), the data is moved to the head of the linked list;

   3. When the linked list is full, discard the data at the end of the linked list.

LRU analysis

   【Hit rate】

   When there is hot data, the efficiency of LRU is very good, but the occasional and periodic batch operations will lead to a sharp drop in the LRU hit rate and serious cache pollution.

   【the complexity】

   Simple to implement.

   【cost】

   When hitting, you need to traverse the linked list, find the index of the hit data block, and then move the data to the head.

LRU-K

   The K in LRU-K represents the number of recent uses, so the LRU can be considered as LRU-1. The main purpose of LRU-K is to solve the problem of "cache pollution" of the LRU algorithm. Its core idea is to extend the criterion of "used 1 time recently" to "used K times recently".

accomplish

   Compared with LRU, LRU-K needs to maintain one more queue to record the history of all cached data being accessed. Only when the number of accesses of the data reaches K times, the data is put into the cache. When data needs to be eliminated, LRU-K will eliminate the data whose K-th access time is the largest from the current time. The detailed implementation is as follows:

   1. When the data is accessed for the first time, it is added to the access history list;

   2. If the data in the access history list does not reach K times of access, it will be eliminated according to certain rules (FIFO, LRU);

   3. When the number of data accesses in the historical queue reaches K times, delete the data index from the historical queue, move the data to the cache queue, and cache the data, and the cache queue is reordered according to time;

   4. After the cached data queue is accessed again, it is reordered;

   5. When the data needs to be eliminated, the data at the end of the cache queue is eliminated, that is, the data whose "last K-th access is the longest from now" is eliminated.

   LRU-K has the advantages of LRU, and can avoid the shortcomings of LRU. In practical applications, LRU-2 is the best choice after combining various factors. LRU-3 or larger K value will have a high hit rate, but poor adaptability. A large amount of data access is required to clear the historical access records.

analyze

   【Hit rate】

   LRU-K reduces the problem of "cache pollution" and has a higher hit rate than LRU.

   【the complexity】

   The LRU-K queue is a priority queue, and the algorithm complexity and cost are relatively high.

   【cost】

   Because LRU-K also needs to record those objects that have been accessed but have not been put into the cache, the memory consumption will be more than LRU; when the amount of data is large, the memory consumption will be considerable.

   LRU-K needs to be sorted based on time (it can be sorted when it needs to be eliminated, or it can be sorted immediately), and the CPU consumption is higher than that of LRU.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325175207&siteId=291194637