Class SimpleLruCache<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - the type of mapped values

    public class SimpleLruCache<K,​V>
    extends java.lang.Object
    Simple limited size cache based on ConcurrentHashMap purging entries in LRU order when reaching size limit
    Since:
    5.1.9
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  SimpleLruCache.Entry<K,​V>  
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleLruCache​(int maxSize, float purgeFactor)
      Create a new cache
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void checkPurgeFactor​(float purgeFactor)  
      void configure​(int maxSize, float purgeFactor)
      Reconfigures the cache.
      V get​(java.lang.Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      private void purge()  
      private static int purgeSize​(int maxSize, float purgeFactor)  
      V put​(K key, V value)
      Maps the specified key to the specified value in this cache.
      int size()
      Returns the current size of this cache
      private long tick()  
      • Methods inherited from class java.lang.Object

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

      • lock

        private java.util.concurrent.locks.Lock lock
      • maximumSize

        private volatile int maximumSize
      • purgeSize

        private int purgeSize
      • time

        private volatile long time
    • Constructor Detail

      • SimpleLruCache

        public SimpleLruCache​(int maxSize,
                              float purgeFactor)
        Create a new cache
        Parameters:
        maxSize - maximum size of the cache, to reduce need for synchronization this is not a hard limit. The real size of the cache could be slightly above this maximum if multiple threads put new values concurrently
        purgeFactor - when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries, purgeFactor is the fraction of maxSize to purge when this happens
    • Method Detail

      • checkPurgeFactor

        private static void checkPurgeFactor​(float purgeFactor)
      • purgeSize

        private static int purgeSize​(int maxSize,
                                     float purgeFactor)
      • get

        public V get​(java.lang.Object key)
        Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

        More formally, if this cache contains a mapping from a key k to a value v such that key.equals(k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

        Parameters:
        key - the key
        Returns:
        value mapped for this key, or null if no value is mapped
        Throws:
        java.lang.NullPointerException - if the specified key is null
      • put

        public V put​(@NonNull
                     K key,
                     @NonNull
                     V value)
        Maps the specified key to the specified value in this cache. Neither the key nor the value can be null.

        The value can be retrieved by calling the get method with a key that is equal to the original key.

        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with key, or null if there was no mapping for key
        Throws:
        java.lang.NullPointerException - if the specified key or value is null
      • tick

        private long tick()
      • size

        public int size()
        Returns the current size of this cache
        Returns:
        the number of key-value mappings in this cache
      • configure

        public void configure​(int maxSize,
                              float purgeFactor)
        Reconfigures the cache. If maxSize is reduced some entries will be purged.
        Parameters:
        maxSize - maximum size of the cache
        purgeFactor - when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries, purgeFactor is the fraction of maxSize to purge when this happens
      • purge

        private void purge()