Class LRUCache<K,​V>

  • Type Parameters:
    K - The type of the map key.
    V - The type of the map value.
    All Implemented Interfaces:
    Serializable, Cloneable, Map<K,​V>

    public class LRUCache<K,​V>
    extends LinkedHashMap<K,​V>
    A Simple LRU Cache based on a LinkedHashMap. Not thread-safe.
    See Also:
    Serialized Form
    • Field Detail

      • maxCacheSize

        protected int maxCacheSize
    • Constructor Detail

      • LRUCache

        public LRUCache()
        Default constructor for an LRU Cache The default capacity is 10000
      • LRUCache

        public LRUCache​(int maximumCacheSize)
        Constructs a LRUCache with a maximum capacity
        Parameters:
        maximumCacheSize - The maximum number of elements to keep in the Cache before eviction starts.
      • LRUCache

        public LRUCache​(int initialCapacity,
                        int maximumCacheSize,
                        float loadFactor,
                        boolean accessOrder)
        Constructs an empty LRUCache instance with the specified initial capacity, maximumCacheSize,load factor and ordering mode.
        Parameters:
        initialCapacity - The initial capacity.
        maximumCacheSize - The maximum number of elements to keep in the Cache before eviction starts.
        loadFactor - The load factor to configure on the underlying map.
        accessOrder - the ordering mode true for access-order, false for insertion-order.
        Throws:
        IllegalArgumentException - if the initial capacity is negative or the load factor is non-positive.
    • Method Detail

      • getMaxCacheSize

        public int getMaxCacheSize()
        Returns:
        Returns the maxCacheSize.
      • setMaxCacheSize

        public void setMaxCacheSize​(int maxCacheSize)
        Parameters:
        maxCacheSize - The maxCacheSize to set.
      • onCacheEviction

        protected void onCacheEviction​(Map.Entry<K,​V> eldest)
        Event point used by subclasses to perform some cleanup action when an element is evicted from the cache.
        Parameters:
        eldest - the item being evicted from the LRUCache.