Class LruCache<K,​V>

  • Type Parameters:
    K - the type of the keys of this cache.
    V - the type of the values this cache caches.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>, Cache<K,​V>

    public class LruCache<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    implements Cache<K,​V>
    A specialized Map that is size-limited (using an LRU algorithm). The Map is thread-safe.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      LruCache​(int maxSize)
      Create a new cache and specify the maximum size of for the cache in bytes, and the maximum lifetime of objects.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)  
      long getCacheHits()
      Get the number of cache hits.
      long getCacheMisses()
      Get the number of cache misses.
      int getMaxCacheSize()
      Return the maximum cache Size.
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V lookup​(K key)
      Returns the value of the specified key, or null.
      V put​(K key, V value)
      Put a value in the cache.
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)  
      protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)  
      void setMaxCacheSize​(int maxCacheSize)
      Set the maximum cache size.
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.util.LinkedHashMap

        forEach, getOrDefault, replaceAll
      • Methods inherited from class java.util.HashMap

        clone, compute, computeIfAbsent, computeIfPresent, merge, putIfAbsent, remove, replace, replace
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, hashCode, merge, putIfAbsent, remove, replace, replace
    • Constructor Detail

      • LruCache

        public LruCache​(int maxSize)
        Create a new cache and specify the maximum size of for the cache in bytes, and the maximum lifetime of objects.
        Parameters:
        maxSize - the maximum number of objects the cache will hold. -1 means the cache has no max size.
    • Method Detail

      • removeEldestEntry

        protected final boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap<K,​V>
      • put

        public final V put​(K key,
                           V value)
        Description copied from interface: Cache
        Put a value in the cache.
        Specified by:
        put in interface Cache<K,​V>
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.HashMap<K,​V>
        Parameters:
        key - the key of the value.
        value - the value.
        Returns:
        the previous value or null.
      • lookup

        public final V lookup​(K key)
        Description copied from interface: Cache
        Returns the value of the specified key, or null.
        Specified by:
        lookup in interface Cache<K,​V>
        Parameters:
        key - the key.
        Returns:
        the value found in the cache, or null.
      • get

        public final V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.LinkedHashMap<K,​V>
      • remove

        public final V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.HashMap<K,​V>
      • clear

        public final void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.LinkedHashMap<K,​V>
      • size

        public final int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.HashMap<K,​V>
      • isEmpty

        public final boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.HashMap<K,​V>
      • values

        public final java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.LinkedHashMap<K,​V>
      • containsKey

        public final boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.HashMap<K,​V>
      • putAll

        public final void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.HashMap<K,​V>
      • containsValue

        public final boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class java.util.LinkedHashMap<K,​V>
      • entrySet

        public final java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Overrides:
        entrySet in class java.util.LinkedHashMap<K,​V>
      • keySet

        public final java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.LinkedHashMap<K,​V>
      • getCacheHits

        public final long getCacheHits()
        Get the number of cache hits.
        Returns:
        the number of cache hits.
      • getCacheMisses

        public final long getCacheMisses()
        Get the number of cache misses.
        Returns:
        the number of cache misses.
      • getMaxCacheSize

        public final int getMaxCacheSize()
        Description copied from interface: Cache
        Return the maximum cache Size.
        Specified by:
        getMaxCacheSize in interface Cache<K,​V>
        Returns:
        the maximum cache size.
      • setMaxCacheSize

        public final void setMaxCacheSize​(int maxCacheSize)
        Description copied from interface: Cache
        Set the maximum cache size.
        Specified by:
        setMaxCacheSize in interface Cache<K,​V>
        Parameters:
        maxCacheSize - the new maximum cache size.