Class LRUCache<K,​V>


  • public class LRUCache<K,​V>
    extends java.lang.Object
    A non-blocking cache where entries are indexed by a key.

    To reduce contention, entry allocation and eviction execute in a sampling fashion (entry hits modulo N). Eviction follows an LRU approach (oldest sampled entries are removed first) when the cache is out of capacity.

    This cache can also be configured to run in FIFO mode, rather than LRU.

    Author:
    Jason T. Greene, Stuart Douglas
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  LRUCache.CacheEntry<K,​V>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_AGE_NO_CACHING
      Max age 0, indicating that entries expire upon creation and are not retained;
      static int MAX_AGE_NO_EXPIRY
      Mage age -1, entries dont expire
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCache​(int maxEntries, int maxAge)  
      LRUCache​(int maxEntries, int maxAge, boolean fifo)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(K key, V newValue)  
      void clear()  
      V get​(K key)  
      java.util.Set<K> keySet()  
      V remove​(K key)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAX_AGE_NO_CACHING

        public static final int MAX_AGE_NO_CACHING
        Max age 0, indicating that entries expire upon creation and are not retained;
        See Also:
        Constant Field Values
      • MAX_AGE_NO_EXPIRY

        public static final int MAX_AGE_NO_EXPIRY
        Mage age -1, entries dont expire
        See Also:
        Constant Field Values
    • Constructor Detail

      • LRUCache

        public LRUCache​(int maxEntries,
                        int maxAge)
      • LRUCache

        public LRUCache​(int maxEntries,
                        int maxAge,
                        boolean fifo)
    • Method Detail

      • add

        public void add​(K key,
                        V newValue)
      • get

        public V get​(K key)
      • keySet

        public java.util.Set<K> keySet()
      • remove

        public V remove​(K key)
      • clear

        public void clear()