Interface Cache
- All Known Implementing Classes:
DefaultCache
,NamedCache
,NoOpCache
,SelectiveCache
public interface Cache
A generic cache interface for storing and managing objects identified by unique keys.
The Cache
interface is primarily used by Flux Capacitor to store entities or other frequently accessed
objects.
The interface supports standard cache operations such as put, get, remove, compute, and clear, and also allows for registering eviction listeners to track when items are removed.
Note: All keys and values in this cache are treated as Object
; callers are responsible
for type safety when retrieving values.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from the cache.void
close()
Closes the cache and releases all associated resources.<T> T
compute
(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) Computes and stores a new value for the givenid
using the provided function.<T> T
computeIfAbsent
(Object id, Function<? super Object, T> mappingFunction) If a value is not already associated with the givenid
, computes and stores one using the given function.<T> T
computeIfPresent
(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) If a value is already associated with the givenid
, computes a new value using the provided function and replaces the old one.boolean
containsKey
(Object id) Checks whether the cache contains an entry for the givenid
.<T> T
Retrieves the value associated with the givenid
, ornull
if not found.default <T> T
getOrDefault
(Object id, T defaultValue) Retrieves the value associated with the givenid
, or returns the specified default if not present.default boolean
isEmpty()
Returns whether the cache is empty.<T> void
modifyEach
(BiFunction<? super Object, ? super T, ? extends T> modifierFunction) Applies the given modifier function to all values currently in the cache.Puts a value in the cache for the givenid
, overwriting any existing value.putIfAbsent
(Object id, Object value) Associates the specified value with the givenid
only if no value is currently associated.registerEvictionListener
(Consumer<CacheEviction> listener) Registers a listener to be notified whenever a cache entry is evicted or removed.<T> T
Removes the entry associated with the givenid
, if present.int
size()
Returns the number of entries currently stored in the cache.
-
Method Details
-
put
Puts a value in the cache for the givenid
, overwriting any existing value.- Parameters:
id
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key- Returns:
- the previous value associated with the
id
, ornull
if none
-
putIfAbsent
Associates the specified value with the givenid
only if no value is currently associated.- Parameters:
id
- the key to check for presencevalue
- the value to associate if absent- Returns:
- the existing value associated with the key, or
null
if the new value was successfully put
-
computeIfAbsent
If a value is not already associated with the givenid
, computes and stores one using the given function.- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to check or computemappingFunction
- the function to compute a value if absent- Returns:
- the current or newly computed value
-
computeIfPresent
If a value is already associated with the givenid
, computes a new value using the provided function and replaces the old one.- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to compute formappingFunction
- the function to compute a new value from the current one- Returns:
- the newly computed value, or
null
if the mapping function returnednull
-
compute
Computes and stores a new value for the givenid
using the provided function.The previous value (if any) is provided to the function. The result is stored in the cache.
- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to compute formappingFunction
- the function to compute a new value- Returns:
- the newly computed value, or
null
if the mapping function returnednull
-
modifyEach
Applies the given modifier function to all values currently in the cache.This is useful for bulk modifications, e.g. adjusting internal state after a system-wide change.
- Type Parameters:
T
- the expected type of the values- Parameters:
modifierFunction
- the function to apply to each entry
-
get
Retrieves the value associated with the givenid
, ornull
if not found.- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to retrieve- Returns:
- the cached value, or
null
if absent
-
getOrDefault
Retrieves the value associated with the givenid
, or returns the specified default if not present.- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to retrievedefaultValue
- the value to return if the key is not found- Returns:
- the cached value or the default
-
containsKey
Checks whether the cache contains an entry for the givenid
.- Parameters:
id
- the key to check- Returns:
true
if the key exists in the cache,false
otherwise
-
remove
Removes the entry associated with the givenid
, if present.- Type Parameters:
T
- the expected type of the removed value- Parameters:
id
- the key to remove- Returns:
- the removed value, or
null
if no value was associated with the key
-
clear
void clear()Removes all entries from the cache. -
size
int size()Returns the number of entries currently stored in the cache.- Returns:
- the number of entries
-
isEmpty
default boolean isEmpty()Returns whether the cache is empty.- Returns:
true
if the cache is empty; otherwisefalse
-
registerEvictionListener
Registers a listener to be notified whenever a cache entry is evicted or removed.- Parameters:
listener
- a function that consumesCacheEviction
s- Returns:
- a registration that can be used to cancel the listener
-
close
void close()Closes the cache and releases all associated resources.
-