I saw this algorithm a long time ago when I was studying operating systems. Later, I saw it more and more, and even saw it when I was reviewing the interview experience. To summarize: 1. What is LRU LRU stands for Least Recently Used, which means the data that has not been used for the longest time. In other words, if a piece of data has not been used in the recent period, the chance of it being used in the future is also relatively small. The common usage scenario is caching, such as the page replacement algorithm in the operating system. There are many implementation schemes. I have read many blogs, and most of them give four or five schemes. For the sake of simplicity, only one scheme with an expiration time is given here. Other implementations are similar, so I will leave it to you who are smart!! Solution: Use linked list plus HashMap Every time a new data comes, first check whether it is contained in the map. If so, move it to the head of the queue. If not, create a new node and put it in. For functions with expiration time, you only need to set an expiration time for each node, and delete it directly when this time is reached. There is another problem: locks should be added in a multi-threaded environment. In order to ensure the flexibility of locks, we use ConcurrentHashMap. OK, let's start implementing it: 2. Code Implementation 1. Define nodes
2. LRU Implementation
Now we have defined several variables and a constructor, which means that once the LRU is started, it will start clearing. The clearing thread is ExpiredNode. Let's take a look: 3. Expired thread clearing method This method is ExpiredNode, which is treated as an inner class in LRU.
Now that you know how to clear expired data, let's see how to add data. 4. set method
5. get method This method is relatively simple, you can get it directly.
Note that the above 345 codes are all stored in LRU. We already know the expiration time. In fact, we just add an expiration time queue and an expiration clearing thread. When clearing, use while(true) to determine whether the head of the queue is expired, and then determine whether to return and clear. When setting the method, we also need to add the new node to the queue and remove the old one. And we use ConcurrentHashMap to ensure thread safety. OK, that’s all for today’s code. This article is reprinted from the WeChat public account "Yugong Yaoyishan". You can follow it through the following QR code. To reprint this article, please contact the public account "Yugong Yaoyishan". |
<<: Can the United States' 6G layout surpass 5G and surpass my country?
>>: Verizon installs 5G equipment outside homeowners' homes, sparking complaints
IoT provides businesses with greater visibility, ...
Solidly carrying out the "six stability"...
In the rapidly evolving IT field, professionals a...
question The company uses Alibaba Cloud infrastru...
Through the connection of virtual network devices...
At the same time, with the large-scale commercial...
[[274498]] Overview SSL (Secure Socket Layer) is ...
The tech world has long embraced high-risk, high-...
In production environments, we often configure VL...
The construction and development of 5G has gone t...
The optical module power is abnormal! What should...
Just for record, I found that there are still hug...
Last month, I shared information about JuHost, a ...
Recently, the security operation and maintenance ...
According to the established plan, the three majo...