Package io.github.dhruv1110.jcachex.impl
Class AbstractCacheBase<K,V>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.impl.AbstractCacheBase<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Implemented Interfaces:
Cache<K,V>
- Direct Known Subclasses:
DataBackedCacheBase
public abstract class AbstractCacheBase<K,V> extends Object implements Cache<K,V>
Abstract base class for all cache implementations providing common functionality. This class implements the common patterns found across cache implementations: - Parameter validation - Statistics tracking - Configuration management - Async operation wrappers - Template methods for extension points
-
-
Field Summary
Fields Modifier and Type Field Description protected CacheConfig<K,V>
config
protected boolean
hasExpiration
protected boolean
hasMaximumSize
protected boolean
hasMaximumWeight
protected AtomicLong
hitCount
protected long
maximumSize
protected AtomicLong
missCount
protected CacheStats
stats
protected boolean
statsEnabled
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractCacheBase(CacheConfig<K,V> config)
Constructor for all cache implementations.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
Template method for clear operation with statistics recording.CompletableFuture<Void>
clearAsync()
Asynchronously removes all mappings from this cache.CacheConfig<K,V>
config()
Common implementation of config() method.boolean
containsKey(K key)
Common implementation of containsKey with validation.protected CacheEntry<V>
createCacheEntry(V value)
Creates a cache entry with proper timestamp and expiration settings.protected abstract void
doClear()
Implementation-specific clear operation.protected abstract boolean
doContainsKey(K key)
Implementation-specific containsKey operation.protected abstract V
doGet(K key)
Implementation-specific get operation.protected abstract void
doPut(K key, V value)
Implementation-specific put operation.protected abstract V
doRemove(K key)
Implementation-specific remove operation.protected void
enforceSize()
Hook for subclasses to perform size enforcement.V
get(K key)
Template method for get operation with common validation and statistics.CompletableFuture<V>
getAsync(K key)
Asynchronously returns the value associated with the key in this cache.protected long
getCurrentWeight()
Calculates the current total weight of entries in the cache.protected boolean
isEntryExpired(CacheEntry<V> entry)
Checks if an entry is expired based on cache configuration.protected boolean
isSizeLimitReached()
Checks if the cache has reached its maximum size or weight.void
put(K key, V value)
Template method for put operation with common validation.CompletableFuture<Void>
putAsync(K key, V value)
Asynchronously associates the specified value with the specified key in this cache.protected void
recordClearStatistics(long sizeBefore)
Records statistics for clear operations.protected void
recordGetStatistics(boolean hit)
Records statistics for get operations.protected void
recordPutStatistics()
Records statistics for put operations.protected void
recordRemoveStatistics(boolean removed)
Records statistics for remove operations.V
remove(K key)
Template method for remove operation with common validation.CompletableFuture<V>
removeAsync(K key)
Asynchronously removes the mapping for a key from this cache if it is present.CacheStats
stats()
Common implementation of stats() method.protected void
updateStatsFromCounters()
Updates the stats object from the atomic counters.protected boolean
validateKey(K key)
Validates that a key is not null.protected boolean
validateValue(V value)
Validates that a value is not null.
-
-
-
Field Detail
-
config
protected final CacheConfig<K,V> config
-
stats
protected final CacheStats stats
-
statsEnabled
protected final boolean statsEnabled
-
maximumSize
protected final long maximumSize
-
hitCount
protected final AtomicLong hitCount
-
missCount
protected final AtomicLong missCount
-
hasExpiration
protected final boolean hasExpiration
-
hasMaximumSize
protected final boolean hasMaximumSize
-
hasMaximumWeight
protected final boolean hasMaximumWeight
-
-
Constructor Detail
-
AbstractCacheBase
protected AbstractCacheBase(CacheConfig<K,V> config)
Constructor for all cache implementations.- Parameters:
config
- the cache configuration- Throws:
IllegalArgumentException
- if config is null
-
-
Method Detail
-
get
public final V get(K key)
Template method for get operation with common validation and statistics.
-
put
public final void put(K key, V value)
Template method for put operation with common validation.
-
clear
public final void clear()
Template method for clear operation with statistics recording.
-
containsKey
public final boolean containsKey(K key)
Common implementation of containsKey with validation.- Specified by:
containsKey
in interfaceCache<K,V>
- Parameters:
key
- key whose presence in this cache is to be tested- Returns:
- true if this cache contains a mapping for the specified key
-
stats
public final CacheStats stats()
Common implementation of stats() method.
-
config
public final CacheConfig<K,V> config()
Common implementation of config() method.
-
getAsync
public CompletableFuture<V> getAsync(K key)
Description copied from interface:Cache
Asynchronously returns the value associated with the key in this cache.
-
putAsync
public CompletableFuture<Void> putAsync(K key, V value)
Description copied from interface:Cache
Asynchronously associates the specified value with the specified key in this cache.
-
removeAsync
public CompletableFuture<V> removeAsync(K key)
Description copied from interface:Cache
Asynchronously removes the mapping for a key from this cache if it is present.- Specified by:
removeAsync
in interfaceCache<K,V>
- Parameters:
key
- key whose mapping is to be removed from the cache- Returns:
- a CompletableFuture that will be completed with the previous value associated with key, or null if there was no mapping for key
-
clearAsync
public CompletableFuture<Void> clearAsync()
Description copied from interface:Cache
Asynchronously removes all mappings from this cache.- Specified by:
clearAsync
in interfaceCache<K,V>
- Returns:
- a CompletableFuture that will be completed when the operation is done
-
doGet
protected abstract V doGet(K key)
Implementation-specific get operation.- Parameters:
key
- the key (already validated)- Returns:
- the value or null if not found/expired
-
doPut
protected abstract void doPut(K key, V value)
Implementation-specific put operation.- Parameters:
key
- the key (already validated)value
- the value (already validated)
-
doRemove
protected abstract V doRemove(K key)
Implementation-specific remove operation.- Parameters:
key
- the key (already validated)- Returns:
- the removed value or null if not found
-
doClear
protected abstract void doClear()
Implementation-specific clear operation.
-
doContainsKey
protected abstract boolean doContainsKey(K key)
Implementation-specific containsKey operation.- Parameters:
key
- the key (already validated)- Returns:
- true if key exists and is not expired
-
validateKey
protected boolean validateKey(K key)
Validates that a key is not null.- Parameters:
key
- the key to validate- Returns:
- true if key is valid
-
validateValue
protected boolean validateValue(V value)
Validates that a value is not null.- Parameters:
value
- the value to validate- Returns:
- true if value is valid
-
recordGetStatistics
protected void recordGetStatistics(boolean hit)
Records statistics for get operations.- Parameters:
hit
- true if the operation was a hit
-
recordPutStatistics
protected void recordPutStatistics()
Records statistics for put operations.
-
recordRemoveStatistics
protected void recordRemoveStatistics(boolean removed)
Records statistics for remove operations.- Parameters:
removed
- true if an entry was actually removed
-
recordClearStatistics
protected void recordClearStatistics(long sizeBefore)
Records statistics for clear operations.- Parameters:
sizeBefore
- the size before clearing
-
updateStatsFromCounters
protected void updateStatsFromCounters()
Updates the stats object from the atomic counters.
-
isEntryExpired
protected boolean isEntryExpired(CacheEntry<V> entry)
Checks if an entry is expired based on cache configuration.- Parameters:
entry
- the cache entry to check- Returns:
- true if the entry is expired
-
createCacheEntry
protected CacheEntry<V> createCacheEntry(V value)
Creates a cache entry with proper timestamp and expiration settings.- Parameters:
value
- the value to wrap- Returns:
- a new cache entry
-
isSizeLimitReached
protected boolean isSizeLimitReached()
Checks if the cache has reached its maximum size or weight.- Returns:
- true if size or weight limit is exceeded
-
getCurrentWeight
protected long getCurrentWeight()
Calculates the current total weight of entries in the cache. Default implementation returns size as weight. Subclasses should override if they track weight separately.- Returns:
- the current total weight
-
enforceSize
protected void enforceSize()
Hook for subclasses to perform size enforcement. Called after operations that might exceed size limits.
-
-