Interface EvictableCache<K,​V>

    • Field Detail

      • CACHE_TIMEOUT_MINUTES

        static final long CACHE_TIMEOUT_MINUTES
        Default timeout of records in minutes (inactivity timeout). Default values are valid for default implementation, custom implementations may not support such features.
        See Also:
        Constant Field Values
      • CACHE_EVICT_PERIOD_MINUTES

        static final long CACHE_EVICT_PERIOD_MINUTES
        Default eviction period in minutes (how often to evict records). Default values are valid for default implementation, custom implementations may not support such features.
        See Also:
        Constant Field Values
      • CACHE_EVICT_DELAY_MINUTES

        static final long CACHE_EVICT_DELAY_MINUTES
        Default eviction delay in minutes (how long to wait after the cache is started). Default values are valid for default implementation, custom implementations may not support such features.
        See Also:
        Constant Field Values
      • CACHE_MAX_SIZE

        static final long CACHE_MAX_SIZE
        Maximal number of records in the cache. If the cache is full, no caching is done and the supplier of value is called for every uncached value. Default values are valid for default implementation, custom implementations may not support such features.
        See Also:
        Constant Field Values
    • Method Detail

      • builder

        static <K,​V> EvictableCache.Builder<K,​V> builder()
        Create a new builder for a cache that uses the default implementation.
        Type Parameters:
        K - type of keys in the cache
        V - type of values in the cache
        Returns:
        a builder to build the cache
      • create

        static <K,​V> EvictableCache<K,​V> create()
        Create a new cache with default values using the default implementation.
        Type Parameters:
        K - type of keys in the cache
        V - type of values in the cache
        Returns:
        new cache built with default values
      • create

        static <K,​V> EvictableCache<K,​V> create​(Config config)
        Create a new cache and configure it from the provided configuration. See EvictableCache.Builder.config(Config) for the list of configuration keys. This will use the default implementation.
        Type Parameters:
        K - type of keys in the cache
        V - type of values in the cache
        Parameters:
        config - config to read configuration of this cache from
        Returns:
        new cache configured from config
      • noCache

        static <K,​V> EvictableCache<K,​V> noCache()
        Create a new cache that is not a cache (e.g. never caches, delegates all calls to the Supplier in computeValue(Object, Supplier).
        Type Parameters:
        K - Type of keys
        V - Type of values
        Returns:
        a new instance that is not caching
      • remove

        default Optional<V> remove​(K key)
        Remove a key from the cache. Return the value if it was cached and valid.
        Parameters:
        key - key to remove
        Returns:
        value if it was removed and valid, empty otherwise
      • get

        default Optional<V> get​(K key)
        Get current cached value if valid.
        Parameters:
        key - key to use
        Returns:
        current value in the cache or empty if not present (or invalid)
      • size

        default int size()
        Current size of the cache. This value may not represent exact current size, as the implementation is expected to be thread safe and accessed from multiple threads, that may change the size in parallel.
        Returns:
        current size of the cache (including valid and invalid - not yet evicted - values)
      • computeValue

        default Optional<V> computeValue​(K key,
                                         Supplier<Optional<V>> valueSupplier)
        Either return a cached value or compute it and cache it.
        Parameters:
        key - key to check/insert value for
        valueSupplier - supplier called if the value is not yet cached, or is invalid
        Returns:
        current value from the cache, or computed value from the supplier
      • close

        default void close()
        Close this cache. Carry out shutdown tasks (e.g. shutting down eviction thread).