Class CacheLayer<IdType,ObjectType,EntryType extends CacheEntry<ObjectType>>
- Type Parameters:
IdType- key type used to identify cached objectsObjectType- type of objects stored in the cacheEntryType- cache entry type used internally
- All Implemented Interfaces:
AutoCloseable,Cache<IdType,ObjectType>
- Direct Known Subclasses:
ChronicleMapCache,MapDBCache
A cache layer stores objects identified by keys and delegates cache management behavior to configurable policies.
- A
CacheReplacementPolicydetermines which entries should be evicted when the cache exceeds its capacity. - A
CacheInvalidationPolicydetermines whether entries are still valid when accessed. - A
CacheEntryFactorycreates cache entry instances for objects.
This class is designed to support thread-safe cache operations by synchronizing access to the underlying cache map. As a result, updates to the cache contents and interactions with the configured policies are performed atomically with respect to other cache operations.
Thread safety depends on subclasses providing a map instance that is used consistently as the synchronization monitor and on the configured policies behaving correctly when invoked under this synchronization scheme.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intprotected final CacheEntryFactory<EntryType, ObjectType> protected final CacheInvalidationPolicy<EntryType, ObjectType> protected final Objectprotected final CacheReplacementPolicy<IdType, ObjectType, EntryType> -
Constructor Summary
ConstructorsConstructorDescriptionCacheLayer(Map<IdType, EntryType> map, int capacity, CachePolicies<IdType, ObjectType, EntryType> policies) Creates a cache layer with the specified capacity and cache policies. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all cache entries and resets replacement policy state.booleanRemoves the cache entry associated with the given key.booleanevict(IdType key, ObjectType value) Removes the cache entry associated with the given key if the cached object equals the provided value.final ObjectTypeRetrieves the cached object associated with the given key.booleanisEmpty()Returns whether the cache currently contains any entries.voidput(IdType key, ObjectType value) Inserts or updates a cache entry.
-
Field Details
-
lock
-
map
-
capacity
protected final int capacity -
entryFactory
-
replacementPolicy
protected final CacheReplacementPolicy<IdType,ObjectType, replacementPolicyEntryType extends CacheEntry<ObjectType>> -
invalidationPolicy
protected final CacheInvalidationPolicy<EntryType extends CacheEntry<ObjectType>,ObjectType> invalidationPolicy
-
-
Constructor Details
-
CacheLayer
public CacheLayer(Map<IdType, EntryType> map, int capacity, CachePolicies<IdType, ObjectType, EntryType> policies) Creates a cache layer with the specified capacity and cache policies.The provided policies define how cache entries are created, invalidated, and selected for eviction. The cache capacity represents the maximum number of entries that may be stored simultaneously.
- Parameters:
map- backing map used to store cache entries and synchronize cache operationscapacity- maximum number of entries that can be storedpolicies- cache policies used by this cache layer
-
-
Method Details
-
put
Inserts or updates a cache entry.If the key is not present, a new entry is added and the replacement policy is notified via
entryWasAdded. If the key already exists, its value is replaced and the replacement policy is notified viaentryWasRewritten.After insertion, the cache capacity is enforced. If the number of entries exceeds the configured capacity, one or more entries are evicted according to the replacement policy.
- Specified by:
putin interfaceCache<IdType,ObjectType> - Parameters:
key- cache key, must not benullvalue- object to cache, must not benull- Throws:
IllegalArgumentException- ifkeyorvalueisnull
-
get
Retrieves the cached object associated with the given key.If the key is present, the corresponding entry is first validated using the configured invalidation policy. Invalid entries are automatically evicted and treated as cache misses.
Successful lookups notify the replacement policy via
entryWasRequested, allowing policies such as LRU to update their internal state.- Specified by:
getin interfaceCache<IdType,ObjectType> - Parameters:
key- cache key- Returns:
- the cached object if a valid entry exists, otherwise
null
-
evict
Removes the cache entry associated with the given key.If an entry is removed, the replacement policy is notified via
entryWasEvictedso that any policy-specific state can be updated.- Specified by:
evictin interfaceCache<IdType,ObjectType> - Parameters:
key- key of the entry to remove- Returns:
trueif an entry was present and removed,falseotherwise
-
evict
Removes the cache entry associated with the given key if the cached object equals the provided value.If an entry is removed, the replacement policy is notified via
entryWasEvictedso that any policy-specific state can be updated.- Specified by:
evictin interfaceCache<IdType,ObjectType> - Parameters:
key- key of the entry to removevalue- expected value associated with the key- Returns:
trueif an entry was present and removed,falseotherwise
-
isEmpty
public boolean isEmpty()Returns whether the cache currently contains any entries.- Specified by:
isEmptyin interfaceCache<IdType,ObjectType> - Returns:
trueif the cache contains no entries,falseotherwise
-
clear
public void clear()Removes all cache entries and resets replacement policy state.After this method returns, the cache contains no entries and the replacement policy behaves as if it were newly created.
- Specified by:
clearin interfaceCache<IdType,ObjectType>
-