Interface Cache<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - the type of mapped values
    All Known Implementing Classes:
    CaffeineCache

    public interface Cache<K,​V>
    A semi-persistent mapping from keys to values.
    Author:
    Simon Legner
    See Also:
    Can I provide a custom cache implementation?
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      V get​(K key)
      Fetches the key from the cache
      static <K,​V>
      Cache<K,​V>
      newCache​(int timeout)
      Creates a new cache with the specified timeout.
      void put​(K key, V value)
      Stores a binding for the key and the value in the cache
      void remove​(K key)
      Removes the binding for the key from the cache
      int size()
      Returns the number of bindings in this cache
    • Method Detail

      • newCache

        static <K,​V> Cache<K,​V> newCache​(int timeout)
                                              throws NoSuchElementException
        Creates a new cache with the specified timeout. The cache implementation is obtained using ServiceLoader. To create your own implementation, implement CacheSupplier and register it using the /META-INF/services/waffle.util.cache.CacheSupplier file on your classpath.
        Type Parameters:
        K - the type of keys maintained by this cache
        V - the type of mapped values
        Parameters:
        timeout - timeout in seconds
        Returns:
        a new cache
        Throws:
        NoSuchElementException - if no cache can be instantiated, use Throwable.getSuppressed() to obtain details.
      • get

        V get​(K key)
        Fetches the key from the cache
        Parameters:
        key - the key
        Returns:
        the corresponding value
        See Also:
        Map.get(java.lang.Object)
      • put

        void put​(K key,
                 V value)
        Stores a binding for the key and the value in the cache
        Parameters:
        key - the key
        value - the value
        See Also:
        Map.put(K, V)
      • remove

        void remove​(K key)
        Removes the binding for the key from the cache
        Parameters:
        key - the key
        See Also:
        Map.remove(Object)
      • size

        int size()
        Returns the number of bindings in this cache
        Returns:
        the size
        See Also:
        Map.size()