Interface Cache<K,​V>

  • Type Parameters:
    K - the Key parameter
    V - the Value parameter
    All Known Implementing Classes:
    AutoCalculatingLRUCache, LRUCache

    public interface Cache<K,​V>

    A generic cache interface. A cache represents a memorized key-value pair.

    Author:
    David B. Bracewell
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      boolean containsKey​(K key)
      Determines if a key is in the cache or not
      static <K,​V>
      Cache<K,​V>
      create​(int maxSize)
      Creates an LRU cache.
      static <K,​V>
      Cache<K,​V>
      create​(int maxSize, @NonNull SerializableFunction<K,​V> valueCalculator)
      Creates an Auto Calculating LRU cache.
      V get​(K key)
      Gets the value associated with a key
      V get​(K key, SerializableSupplier<? extends V> supplier)
      Gets the value associated with the given key when available and if not available calculates and stores the value using the given supplier.
      void invalidate​(K key)
      Removes a single key
      void invalidateAll()
      Clears the cache
      default void invalidateAll​(Iterable<? extends K> keys)
      Clears the cache of all given keys
      default boolean isEmpty()
      Determines if the cache is empty or not
      void put​(K key, V value)
      Adds a key value pair to the cache overwriting any value that is there
      long size()
      The number of items cached.
    • Method Detail

      • create

        static <K,​V> Cache<K,​V> create​(int maxSize)
        Creates an LRU cache.
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        maxSize - the max size of the cache
        Returns:
        the cache
      • create

        static <K,​V> Cache<K,​V> create​(int maxSize,
                                                   @NonNull
                                                   @NonNull SerializableFunction<K,​V> valueCalculator)
        Creates an Auto Calculating LRU cache.
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        maxSize - the max size of the cache
        valueCalculator - the value calculator to use when getting the value for a key
        Returns:
        the cache
      • containsKey

        boolean containsKey​(K key)
        Determines if a key is in the cache or not
        Parameters:
        key - The key to check
        Returns:
        True if the key is in the cache, False if not
      • get

        V get​(K key)
        Gets the value associated with a key
        Parameters:
        key - The key
        Returns:
        The value associated with the key or null
      • get

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

        void invalidate​(K key)
        Removes a single key
        Parameters:
        key - The key to remove
      • invalidateAll

        void invalidateAll()
        Clears the cache
      • invalidateAll

        default void invalidateAll​(Iterable<? extends K> keys)
        Clears the cache of all given keys
        Parameters:
        keys - The keys to remove
      • isEmpty

        default boolean isEmpty()
        Determines if the cache is empty or not
        Returns:
        True if empty, False if not
      • put

        void put​(K key,
                 V value)
        Adds a key value pair to the cache overwriting any value that is there
        Parameters:
        key - The key
        value - The value
      • size

        long size()
        The number of items cached.
        Returns:
        The current size of the cache