Interface Cache<Key,Value>

All Known Implementing Classes:
Cache0, Cache1, CacheCaffeine, CacheSimple, CacheWrapper

public interface Cache<Key,Value>
An abstraction of a cache for basic use. This cache does not support null as keys or values.

For more complex configuration of the cache, use the cache builder of the implementation of choice.

See Also:
  • Method Details

    • containsKey

      boolean containsKey(Key key)
      Does the cache contain the key?
      Parameters:
      key - The key to find. The key must not be null.
      Returns:
      True, if the cache contains the key, otherwise false.
    • getIfPresent

      Value getIfPresent(Key key)
      Get from cache - or return null.
      Parameters:
      key - The key for which the value is requested. The key must not be null.
      Returns:
      If the cache contains an entry for the given key, the value is returned, otherwise null.
    • getOrFill

      @Deprecated(forRemoval=true) default Value getOrFill(Key key, Callable<Value> callable)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Get from cache; if not present, call the Callable to try to fill the cache. This operation should be atomic. The 'key' and 'callcable' must not be null.
    • get

      Value get(Key key, Function<Key,Value> callable)
      Get from cache; if not present, call the Function to fill the cache slot. This operation should be atomic.
      Parameters:
      key - The key, for which the value should be returned or calculated. The key must not be null.
      callable - If the cache does not contain the key, the callable is called to calculate a value. If the callable returns null, the key is not associated with hat value, as nulls are not accepted as values. The callable must not be null.
      Returns:
      Returns either the existing value or the calculated value. If callable is called and returns null, then null is returned.
    • put

      void put(Key key, Value thing)
      Insert into the cache
      Parameters:
      key - The key for the 'thing' to store. The key must not be null.
      thing - If 'thing' is null, it will not be used as value, instead any existing entry with the same key will be removed.
    • remove

      void remove(Key key)
      Remove from cache - return true if key referenced an entry
      Parameters:
      key - The key, which shall be removed along with its value. The key must not be null.
    • keys

      Iterator<Key> keys()
      Iterate over all keys. Iterating over the keys requires the caller be thread-safe.
    • isEmpty

      boolean isEmpty()
    • clear

      void clear()
    • size

      long size()
      Current size of cache
    • stats

      default CacheInfo stats()
      Cache statistics (not supported by all caches)