Java implements LRU cache algorithm

1. What is LRU

LRU ( Least Recently Used ) is a cache algorithm whose core idea is to remove the least recently used cache items to make room for more commonly used cache items .

In practical applications, the LRU algorithm is widely used in caching and page replacement.


2. Java implements LRU cache algorithm

In Java, you can use LinkedHashMap to implement the LRU cache algorithm.
LinkedHashMap is a subclass of HashMap, which internally uses a doubly linked list to maintain the order of elements.

The specific implementation ideas are as follows:

  1. Inherit LinkedHashMap and rewrite the removeEldestEntry method, which returns true to indicate that the oldest cache entry needs to be removed;
  2. Specify accessOrder as true in the construction method, so that the element will be moved to the end of the linked list when accessing the element, which is convenient for subsequent search and removal;
  3. When accessing a cache item, use the get method to get the element, and use the removeEldestEntry method to determine whether the oldest cache item needs to be removed;
  4. When adding a cache item, use the put method to add elements to LinkedHashMap.

The sample code for implementing the LRU cache algorithm using LinkedHashMap is as follows:

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache<K, V> extends LinkedHashMap<K, V> {
    
    
    private final int capacity;

    public LRUCache(int capacity) {
    
    
        super(capacity, 0.75f, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
    
    
        return size() > capacity;
    }

    public static void main(String[] args) {
    
    
        LRUCache<Integer, String> cache = new LRUCache<>(3);
        cache.put(1, "one");
        cache.put(2, "two");
        cache.put(3, "three");
        System.out.println(cache); // {1=one, 2=two, 3=three}

        cache.get(2);
        System.out.println(cache); // {1=one, 3=three, 2=two}

        cache.put(4, "four");
        System.out.println(cache); // {3=three, 2=two, 4=four}
    }
}

In the sample code above, we created a LRUCache class, inherited LinkedHashMap, and specified accessOrder as true in the construction method.
In the removeEldestEntry method, when the number of cache entries exceeds the capacity, true is returned, indicating that the oldest cache entry needs to be removed.
When accessing cache items, use the get method to get elements. If the number of cache items exceeds the capacity, the oldest cache item will be removed.
When adding a cache item, use the put method to add elements to LinkedHashMap.
Finally, the cache is tested in the main method.

It should be noted that when using LinkedHashMap to implement LRU cache, accessOrder must be specified as true, otherwise LinkedHashMap will maintain the order of elements according to the order of insertion, not the order of access.



Guess you like

Origin blog.csdn.net/aikudexiaohai/article/details/130554371