In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Java code to achieve the least use of caching recently. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
How to implement a recently least used (LRU) cache. Caching can be implemented through hash tables, but adding a size limit to this cache becomes another interesting issue. Now let's see how to do it.
Recycle that uses the least cache recently
In order to achieve cache recycling, we need to do this easily:
Query out the most recently used items
Mark the most recently used item
Linked lists can achieve these two operations. To detect the least recently used items, you only need to return to the end of the linked list. Marking an item that is recently used simply removes it from the current position and places the item in the header. The more difficult thing is how to quickly find the item in the linked list.
Help with hash table
Looking at the data structure in our toolbox, the hash table can index to an object in a constant amount of time. If we create a hash table that looks like a key- > linked list node, we can find the most recently used node in constant time. What's more, we can also judge whether the node exists (or does not exist) in constant time.
Once we find this node, we can move it to the front of the linked list and mark it as the most recently used item.
Shortcuts to Java
As far as I know, there are very few standard libraries of programming languages that have common data structures that can provide these functions. This is a hybrid data structure, and we need to build a linked list based on the hash table. But Java has provided us with this form of data structure-LinkedHashMap! It even provides a way to override the recycling strategy. What we need to pay attention to is that the order of changing the linked list is the order of insertion, not the order of access. However, there is a constructor that provides an option to use the order of access (see documentation).
Needless to say:
Import java.util.LinkedHashMap; import java.util.Map; public LRUCache extends LinkedHashMap {private int cacheSize; public LRUCache (int cacheSize) {super (16,0.75, true); this.cacheSize = cacheSize;} protected boolean removeEldestEntry (Map.Entry eldest) {return size () > = cacheSize }} the above is the editor to share with you how to use Java code to achieve the least use of caching recently, if you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.