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 Object
    Simple limited size cache based on ConcurrentHashMap purging entries in LRU order when reaching size limit
    Since:
    5.1.9
    • 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

      • get

        public V get​(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:
        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:
        NullPointerException - if the specified key or value is null
      • 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