Class LRUCache<K,​V>

  • Type Parameters:
    K - the key type parameter
    V - the value type parameter
    All Implemented Interfaces:
    Cache<K,​V>
    Direct Known Subclasses:
    AutoCalculatingLRUCache

    public class LRUCache<K,​V>
    extends Object
    implements Cache<K,​V>
    Last-Recently-Used Cache with bounded size.
    Author:
    David B. Bracewell
    • Field Detail

      • cache

        protected final transient Map<K,​V> cache
    • Constructor Detail

      • LRUCache

        public LRUCache​(int maxSize)
        Instantiates a new LRU cache with given max size.
        Parameters:
        maxSize - the max size
    • Method Detail

      • containsKey

        public boolean containsKey​(K key)
        Description copied from interface: Cache
        Determines if a key is in the cache or not
        Specified by:
        containsKey in interface Cache<K,​V>
        Parameters:
        key - The key to check
        Returns:
        True if the key is in the cache, False if not
      • get

        public V get​(K key)
        Description copied from interface: Cache
        Gets the value associated with a key
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - The key
        Returns:
        The value associated with the key or null
      • get

        public V get​(K key,
                     SerializableSupplier<? extends V> supplier)
        Description copied from interface: Cache
        Gets the value associated with the given key when available and if not available calculates and stores the value using the given supplier.
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - The key
        supplier - The supplier to use to generate the value
        Returns:
        The old value if put, null if not
      • invalidate

        public void invalidate​(K key)
        Description copied from interface: Cache
        Removes a single key
        Specified by:
        invalidate in interface Cache<K,​V>
        Parameters:
        key - The key to remove
      • invalidateAll

        public void invalidateAll()
        Description copied from interface: Cache
        Clears the cache
        Specified by:
        invalidateAll in interface Cache<K,​V>
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: Cache
        Adds a key value pair to the cache overwriting any value that is there
        Specified by:
        put in interface Cache<K,​V>
        Parameters:
        key - The key
        value - The value
      • size

        public long size()
        Description copied from interface: Cache
        The number of items cached.
        Specified by:
        size in interface Cache<K,​V>
        Returns:
        The current size of the cache