Class ChronicleMapCache
- All Implemented Interfaces:
AutoCloseable,Cache<PersistentCacheKey,CompletableFuture<? extends DataRetrievalResponse<?>>>
ChronicleMap.
This class is thread-safe for concurrent access through its public methods on
a single ChronicleMapCache instance. All access is guarded by
synchronization on map.
Cache entry creation, invalidation, and replacement behavior are delegated to the configured cache policies.
The cache enforces a maximum capacity. If the persisted map contains more entries than allowed when opened, entries are evicted according to the configured replacement policy until the capacity constraint is satisfied.
Caveats:
Thread safety is provided only at the level of this cache instance. If two ChronicleMapCache objects point to the same persisted file, this class does not coordinate:
- replacement policy state
- capacity enforcement
- invalidation/eviction decisions
-
Field Summary
FieldsFields inherited from class se.liu.ida.hefquin.federation.access.impl.cache.CacheLayer
capacity, entryFactory, invalidationPolicy, lock, map, replacementPolicy -
Constructor Summary
ConstructorsConstructorDescriptionChronicleMapCache(int capacity, String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the given capacity and file path.ChronicleMapCache(int capacity, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the given capacity and the default file path.ChronicleMapCache(String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Constructs a newChronicleMapCachewith a custom file path and the default cache capacity.ChronicleMapCache(CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the default capacity and default file path. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlyingChronicleMapand releases associated resources.protected static Map<PersistentCacheKey, PersistentCacheEntry> createChronicleMap(String filename, int capacity) protected static FileensureParentDirectoryExists(String filename) Ensures that the parent directory of the given file path exists.
-
Field Details
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY- See Also:
-
DEFAULT_FILENAME
- See Also:
-
-
Constructor Details
-
ChronicleMapCache
public ChronicleMapCache(CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the default capacity and default file path.- Parameters:
policies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
ChronicleMapCache
public ChronicleMapCache(int capacity, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the given capacity and the default file path.- Parameters:
capacity- the maximum number of cache entriespolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
ChronicleMapCache
public ChronicleMapCache(String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Constructs a newChronicleMapCachewith a custom file path and the default cache capacity.- Parameters:
filename- the path to the cache filepolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
ChronicleMapCache
public ChronicleMapCache(int capacity, String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the given capacity and file path.If the persisted map already contains entries, the replacement policy is initialized accordingly and eviction is performed if necessary to satisfy the capacity constraint.
- Parameters:
capacity- the maximum number of cache entriesfilename- the path to the cache filepolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
-
Method Details
-
ensureParentDirectoryExists
Ensures that the parent directory of the given file path exists.- Parameters:
filename- the path to the file- Returns:
- a
Fileobject for the given path - Throws:
IllegalStateException- if the parent directory does not exist and cannot be created
-
createChronicleMap
protected static Map<PersistentCacheKey,PersistentCacheEntry> createChronicleMap(String filename, int capacity) throws IOException - Throws:
IOException
-
close
public void close()Closes the underlyingChronicleMapand releases associated resources.Closing the map is not required but ensures that all data is properly flushed to disk and that off-heap resources are released.
-