Interface Cache<K,V>

Type Parameters:
K - the type of keys maintained by this cache
V - the type of cached values
All Known Implementing Classes:
Cache.RefConcurrentMap

public interface Cache<K,V>
A cache interface that provides configurable reference types for both keys and values, and supports automatic cleanup of garbage-collected entries.

This cache is designed for scenarios where:

  • Values should be eligible for garbage collection when memory is low
  • Concurrent access is required
  • Automatic cleanup of stale entries is desired

The cache can use different reference types (none, soft, weak, hard) for both keys and values, depending on the factory method used to create the cache instance.

Note: All implementations are thread-safe and optimized for concurrent read access.

  • Method Details

    • computeIfAbsent

      V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction, Cache.ReferenceType referenceType)
      Computes a value for the given key if it's not already present, using the specified reference type.

      This method allows fine-grained control over the reference type used for individual entries, overriding the cache's default reference type for this specific key-value pair.

      Parameters:
      key - the key whose associated value is to be returned or computed
      mappingFunction - the function to compute a value
      referenceType - the reference type to use for this entry (null uses cache default)
      Returns:
      the current (existing or computed) value associated with the specified key
    • computeIfAbsent

      default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
    • removeIf

      void removeIf(BiPredicate<K,V> o)
    • get

      V get(K key)
    • size

      int size()
    • clear

      void clear()
    • containsKey

      default boolean containsKey(K key)
    • newCache

      static <K, V> Cache<K,V> newCache(Cache.ReferenceType referenceType)
    • newCache

      static <K, V> Cache<K,V> newCache(Cache.ReferenceType referenceType, String name)
    • newCache

      static <K, V> Cache<K,V> newCache(Cache.ReferenceType keyReferenceType, Cache.ReferenceType valueReferenceType)
      Creates a new cache with separate reference types for keys and values. This allows fine-grained control over eviction behavior and enables better tracking of cache misses caused by key vs value evictions.
      Parameters:
      keyReferenceType - the reference type to use for keys
      valueReferenceType - the reference type to use for values
      Returns:
      a new cache instance
    • newCache

      static <K, V> Cache<K,V> newCache(Cache.ReferenceType keyReferenceType, Cache.ReferenceType valueReferenceType, String name)