Class LruCache<K,​V>


  • public class LruCache<K,​V>
    extends Object
    LRU cache.
    • Constructor Summary

      Constructors 
      Constructor Description
      LruCache​(int maxSize)
      Constructor with the maximal size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected V create​(K key)
      Called after a cache miss to compute a value for the corresponding key.
      int createCount()
      Returns the number of times create(Object) returned a value.
      protected void entryRemoved​(boolean evicted, K key, V oldValue, V newValue)
      Called for entries that have been evicted or removed.
      void evictAll()
      Clear the cache, calling entryRemoved(boolean, K, V, V) on each removed entry.
      int evictionCount()
      Returns the number of values that have been evicted.
      V get​(K key)
      Returns the value for key if it exists in the cache or can be created by #create.
      int hitCount()
      Returns the number of times get(K) returned a value that was already present in the cache.
      Set<K> keySnapshot()
      Returns a snapshot of the keys of current cache.
      int maxSize()
      For caches that do not override sizeOf(K, V), this returns the maximum number of entries in the cache.
      int missCount()
      Returns the number of times get(K) returned null or required a new value to be created.
      V put​(K key, V value)
      Caches value for key.
      int putCount()
      Returns the number of times put(K, V) was called.
      V remove​(K key)
      Removes the entry for key if it exists.
      void resize​(int maxSize)
      Sets the size of the cache.
      int size()
      For caches that do not override sizeOf(K, V), this returns the number of entries in the cache.
      protected int sizeOf​(K key, V value)
      Returns the size of the entry for key and value in user-defined units.
      Map<K,​V> snapshot()
      Returns a snapshot the cache.
      String toString()  
      void trimToSize​(int maxSize)
      Remove the eldest entries until the total of remaining entries is at or below the requested size.
    • Constructor Detail

      • LruCache

        public LruCache​(int maxSize)
        Constructor with the maximal size.
        Parameters:
        maxSize - for caches that do not override sizeOf(K, V), this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache.
    • Method Detail

      • resize

        public void resize​(int maxSize)
        Sets the size of the cache.
        Parameters:
        maxSize - the new maximum size.
      • get

        public final V get​(K key)
        Returns the value for key if it exists in the cache or can be created by #create. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and could not be created.
        Parameters:
        key - the key the value for key if it exists in the cache or can be created by #create.
        Returns:
        the
      • put

        public final V put​(K key,
                           V value)
        Caches value for key. The value is moved to the head of the queue.
        Parameters:
        key - the key.
        value - the value.
        Returns:
        the previous value mapped by key.
      • trimToSize

        public void trimToSize​(int maxSize)
        Remove the eldest entries until the total of remaining entries is at or below the requested size.
        Parameters:
        maxSize - the maximum size of the cache before returning. Could be -1 to evict even 0-sized elements.
      • remove

        public final V remove​(K key)
        Removes the entry for key if it exists.
        Parameters:
        key - the key
        Returns:
        the previous value mapped by key.
      • entryRemoved

        protected void entryRemoved​(boolean evicted,
                                    K key,
                                    V oldValue,
                                    V newValue)
        Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to remove(K), or replaced by a call to put(K, V). The default implementation does nothing.

        The method is called without synchronization: other threads may access the cache while this method is executing.

        Parameters:
        evicted - true if the entry is being removed to make space, false if the removal was caused by a put(K, V) or remove(K).
        key - the key
        oldValue - the old value of the key
        newValue - the new value for key, if it exists. If non-null, this removal was caused by a put(K, V); Otherwise it was caused by an eviction or a remove(K).
      • create

        protected V create​(K key)
        Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.

        The method is called without synchronization: other threads may access the cache while this method is executing.

        If a value for key exists in the cache when this method returns, the created value will be released with entryRemoved(boolean, K, V, V) and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread calls put(K, V) while another is creating a value for the same key.

        Parameters:
        key - the key
        Returns:
        the created computed value or null if no value can be computed
      • sizeOf

        protected int sizeOf​(K key,
                             V value)
        Returns the size of the entry for key and value in user-defined units. The default implementation returns 1 so that size is the number of entries and max size is the maximum number of entries.

        An entry's size may not change while it is in the cache.

        Parameters:
        key - the key
        value - the value
        Returns:
        he size of the entry for key and value in user-defined units
      • size

        public final int size()
        For caches that do not override sizeOf(K, V), this returns the number of entries in the cache. For all other caches, this returns the sum of the sizes of the entries in this cache.
        Returns:
        the size
      • maxSize

        public final int maxSize()
        For caches that do not override sizeOf(K, V), this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.
        Returns:
        the maximal size
      • hitCount

        public final int hitCount()
        Returns the number of times get(K) returned a value that was already present in the cache.
        Returns:
        the number of times
      • missCount

        public final int missCount()
        Returns the number of times get(K) returned null or required a new value to be created.
        Returns:
        the number of times returned null or required a new value to be created
      • createCount

        public final int createCount()
        Returns the number of times create(Object) returned a value.
        Returns:
        the number of times create(Object) returned a value.
      • putCount

        public final int putCount()
        Returns the number of times put(K, V) was called.
        Returns:
        the number of times put(K, V) was called.
      • evictionCount

        public final int evictionCount()
        Returns the number of values that have been evicted.
        Returns:
        the number of values that have been evicted.
      • snapshot

        public final Map<K,​V> snapshot()
        Returns a snapshot the cache.
        Returns:
        a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
      • keySnapshot

        public final Set<K> keySnapshot()
        Returns a snapshot of the keys of current cache.
        Returns:
        a copy of the keys of the current contents of the cache.