Java Simple LRU Cache implementation (development tips)

Source: Internet
Author: User

Brother Wai Says:
The relationship between cache and app application, like Simon Qing and color, is inseparable, and the status of the heaviest, so, small white want to become a veteran, to the cache must be in-depth, the following article introduces you to Java if the implementation of LRU (Least recently used) algorithm, that is, the least recently used algorithm, This algorithm in the Android development is used in the image, the content cache, I in "Android Core technology (next)" In the course of the full implementation of the LRU, interested friends can log in to ding-ting academy learning.


Background
Linkedhashmap inherits from HashMap, which provides a Removeeldestentry method, which is the key to the implementation of the LRU strategy, and HashMap internally provides 3 dedicated callback methods for Linkedhashmap. Afternodeaccess, Afternodeinsertion, Afternoderemoval, the literal meaning of these 3 methods is very easy to understand, is the node after access, after the node is inserted, the node after the deletion of the behavior of the separate execution. Based on the above behavior Linkedhashmap can implement a LRUCache function.


About Linkedhashmap Eldest:eldest literally means the oldest, Linkedhashmap has a field called Accessorder, When Accessorder is true, the LINKEDHASHMAP internal node is sorted by the number of accesses, and the oldest node is the least visited node. When Accessorder is false, the LINKEDHASHMAP internal node is sorted by insertion order, and the oldest node is the oldest inserted node, which defaults to false.


Realize
The implementation of their own LRUCache only need to overwrite removeeldestentry this method, the code is as follows

private static Class lrucache<k, v> extends Linkedhashmap<k, v>
{
Private static final long serialversionuid = -9111855653176630846l;
private static int max_elements;
Public LRUCache (int initcap, int maxSize) throws IllegalArgumentException
{
Super (Initcap, 0.75f, true);
if (MaxSize < 0)
throw new IllegalArgumentException ();
Max_elements = maxSize;
}
@Override
Protected Boolean removeeldestentry (Map.entry<k, v> eldest)
{
return size () > max_elements;
}
}

The above code requires a max_elements variable limit the maximum number of storage nodes, when inserting a node to determine if the current number of nodes has exceeded this value will be based on the LRU policy will be the least access to delete the node, it is important to note that the default linkedhashmap is guaranteed by the insertion order, That is, the nodes are sorted according to the insertion order, so even if the deletion is the first inserted node, but we passed in the constructor of a true, this parameter determines how the nodes inside the Linkedhashmap are sorted, When the argument is true, the internal node is sorted by the time of the most recent visit, and false when the description is in the order of insertion. A simple LRUCache implementation has been completed.


Attention
Because the linkedhahsmap itself is not thread-safe, that is, the LRUCache is not thread-safe, and if you want to be able to access multiple threads, you can use it this way: LRUCache cache = Collections.synchronizedmap (New LRUCache (10, 10)). This allows the cache to perform operations such as get\put under multiple threads, but the caches obtained in this way are still unsafe for multithreaded traversal. So it is not possible to traverse the cache under multiple threads, and the official documentation suggests using map itself to synchronize when traversing Synchronizedmap.

Java Simple LRU Cache implementation (development tips)

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.