Class HierarchicalCache<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 by underlying cache layers
- All Implemented Interfaces:
AutoCloseable,Cache<IdType,ObjectType>
Entries are written to both layers. Lookup operations first consult the primary cache layer (L1). If no entry is found, the secondary cache layer (L2) is consulted. Entries retrieved from L2 are promoted to L1 to improve access performance for subsequent requests.
Eviction and clear operations are applied to both cache layers.
All operations are synchronized on an internal lock, ensuring that
interactions between the two cache layers are performed atomically with
respect to other operations on the same HierarchicalCache instance.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Cache<IdType, ObjectType> protected final Cache<IdType, ObjectType> -
Constructor Summary
ConstructorsConstructorDescriptionHierarchicalCache(Cache<IdType, ObjectType> l1, Cache<IdType, ObjectType> l2) Creates a hierarchical cache consisting of a primary and a secondary cache layer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from both cache layers.voidclose()Releases all resources associated with both cache layers.booleanRemoves the entry associated with the given key from both cache layers.booleanevict(IdType key, ObjectType value) Removes entries associated with the given key if they currently map to the specified value.Retrieves the value associated with the given key.booleanisEmpty()Returnstrueif both cache layers are empty.voidput(IdType key, ObjectType value) Stores the given key-value pair in both cache layers.
-
Field Details
-
l1
-
l2
-
-
Constructor Details
-
HierarchicalCache
Creates a hierarchical cache consisting of a primary and a secondary cache layer.- Parameters:
l1- primary cache layer consulted first during lookupsl2- secondary cache layer consulted when entries are not found in the primary cache layer
-
-
Method Details
-
put
Stores the given key-value pair in both cache layers.Existing entries associated with the key are replaced according to the behavior of the underlying cache layers.
- Specified by:
putin interfaceCache<IdType,ObjectType>
-
get
Retrieves the value associated with the given key.The primary cache layer is consulted first. If no entry is found, the secondary cache layer is consulted. Entries retrieved from the secondary cache layer are promoted to the primary cache layer before being returned.
- Specified by:
getin interfaceCache<IdType,ObjectType> - Returns:
- the cached value, or
nullif no entry exists
-
evict
Removes the entry associated with the given key from both cache layers.- Specified by:
evictin interfaceCache<IdType,ObjectType> - Returns:
trueif at least one cache layer removed an entry
-
evict
Removes entries associated with the given key if they currently map to the specified value.The operation is applied independently to each cache layer and behaves as if
evict(key, value)were invoked directly on each layer. Consequently, an entry may be removed from some layers but not others if the layers use different representations or equality semantics for cached values.- Specified by:
evictin interfaceCache<IdType,ObjectType> - Returns:
trueif at least one cache layer removed an entry
-
isEmpty
public boolean isEmpty()Returnstrueif both cache layers are empty.- Specified by:
isEmptyin interfaceCache<IdType,ObjectType> - Returns:
trueif neither cache layer contains any entries
-
clear
public void clear()Removes all entries from both cache layers.- Specified by:
clearin interfaceCache<IdType,ObjectType>
-
close
public void close()Releases all resources associated with both cache layers.The close operation is propagated to the primary and secondary cache layers. After this method returns, the cache should not be used anymore.
If a cache layer does not manage any external resources, its implementation of
close()may perform no action.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCache<IdType,ObjectType>
-