Two kinds of cache elimination algorithms LFU&LRU

Source: Internet
Author: User

The LRU full name is least recently used, the most recent unused meaning.

The design principle of the LRU algorithm is that if a data is not accessed in the most recent time, it is also very unlikely to be accessed in the future. That is, when the confined space is filled with data, the data that has not been accessed for the longest time should be eliminated. Implement LRU1. Use an array to store the data, mark each data item with an access timestamp, and each time the new item is inserted, the timestamp of the data item that exists in the array is increased, and the time stamp of the new data item is set to 0 and inserted into the array. Each time the data item in the array is accessed, the timestamp of the data item being accessed is set to 0. When the array space is full, the data item with the largest timestamp is retired. 2. Using a linked list, each time new data is inserted into the head of the linked list, each time the cache hits (that is, data is accessed), the data is moved to the list header, and when the list is full, the data at the end of the list is discarded. 3. Use linked lists and HashMap. When a new data item needs to be inserted, if the new item exists in the list (commonly called hit), the node is moved to the head of the list, and if it does not exist, a new node is placed on the list head, and if the cache is full, the last node of the linked list is deleted. When accessing the data, if the data item exists in the linked list, the node is moved to the list header, otherwise 1 is returned. This way, the node at the end of the list is the last data item that has been accessed for the longest time. For the first method, it is necessary to maintain the access timestamp of the data items continuously, and in addition, the time complexity is O (n) when inserting data, deleting data, and accessing data. For the second method, the time complexity of the linked list when locating data is O (n). So in general use the third way to be implemented LRU algorithm.

The LFU (Least frequently used) algorithm eliminates data based on the historical frequency of data access, and its core idea is "if the data has been accessed many times in the past, it will be accessed more frequently in the future."

1. Insert the new data into the tail of the queue (because the reference count is 1);

2. When the data in the queue is accessed, the reference count increases and the queue is reordered;

3. When you need to retire data, delete the last chunk of the sorted list.

3. lru-k3.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".

3.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.

3.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.

Two kinds of cache elimination algorithms LFU&LRU

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.