Package io.github.dhruv1110.jcachex.impl
Class ConcurrentCacheBase<K,V>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.impl.AbstractCacheBase<K,V>
-
- io.github.dhruv1110.jcachex.impl.DataBackedCacheBase<K,V,CacheEntry<V>>
-
- io.github.dhruv1110.jcachex.impl.ConcurrentCacheBase<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:
DefaultCache
,OptimizedCacheBase
public abstract class ConcurrentCacheBase<K,V> extends DataBackedCacheBase<K,V,CacheEntry<V>>
Base class for concurrent cache implementations providing thread-safety primitives. This class extends DataBackedCacheBase and adds: - Striped locking for reduced contention - Eviction strategy management - Event listener notification system - Scheduled cleanup operations
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicLong
currentSize
protected AtomicLong
currentWeight
protected EvictionStrategy<K,V>
evictionStrategy
protected ScheduledExecutorService
scheduler
protected static int
STRIPE_COUNT
protected ReentrantReadWriteLock[]
stripes
-
Fields inherited from class io.github.dhruv1110.jcachex.impl.DataBackedCacheBase
data
-
Fields inherited from class io.github.dhruv1110.jcachex.impl.AbstractCacheBase
config, hasExpiration, hasMaximumSize, hasMaximumWeight, hitCount, maximumSize, missCount, stats, statsEnabled
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ConcurrentCacheBase(CacheConfig<K,V> config)
Constructor for concurrent cache implementations.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
cleanupExpiredEntries()
Clean up expired entries.protected CacheEntry<V>
createCacheEntry(K key, V value)
Creates a cache entry with proper weight calculation.protected CacheEntry<V>
createCacheEntry(V value)
Creates a cache entry with proper weight calculation.protected void
doClear()
Common clear implementation.protected boolean
doContainsKey(K key)
Common implementation of containsKey with entry validation.protected V
doGet(K key)
Implementation-specific get operation.protected void
doPut(K key, V value)
Implementation-specific put operation.protected V
doRemove(K key)
Implementation-specific remove operation.protected void
enforceSize()
Override size enforcement to use eviction strategy.protected void
evictEntries()
protected void
evictEntries(K keyToAvoid)
Evict entries based on the eviction strategy, avoiding a specific key.protected V
extractValue(CacheEntry<V> entry)
Extract the value from an entry.protected long
getCurrentWeight()
Calculates the current total weight of entries in the cache.protected ReentrantReadWriteLock.ReadLock
getReadLock(K key)
Get read lock for a specific key.protected int
getStripeIndex(K key)
Get the stripe index for a key to enable striped locking.protected ReentrantReadWriteLock.WriteLock
getWriteLock(K key)
Get write lock for a specific key.protected boolean
isValidEntry(CacheEntry<V> entry)
Check if an entry is valid (not expired, not corrupted, etc.).protected V
loadValue(K key)
Load value using configured loader.protected void
notifyListeners(Consumer<CacheEventListener<K,V>> action)
Notify all registered event listeners.protected void
refreshEntries()
Refresh entries that need refreshing.protected void
scheduleCleanup()
Schedule periodic cleanup of expired entries.protected void
scheduleRefresh()
Schedule refresh operations for entries.protected boolean
shouldAvoidEvictingNewEntries()
Determines if we should avoid evicting newly added entries.void
shutdown()
Shutdown the cache and clean up resources.long
size()
Common implementation of size() method.-
Methods inherited from class io.github.dhruv1110.jcachex.impl.DataBackedCacheBase
afterClear, beforeClear, createMapEntry, entries, getDataMap, isValidValue, keys, values
-
Methods inherited from class io.github.dhruv1110.jcachex.impl.AbstractCacheBase
clear, clearAsync, config, containsKey, get, getAsync, isEntryExpired, isSizeLimitReached, put, putAsync, recordClearStatistics, recordGetStatistics, recordPutStatistics, recordRemoveStatistics, remove, removeAsync, stats, updateStatsFromCounters, validateKey, validateValue
-
-
-
-
Field Detail
-
evictionStrategy
protected final EvictionStrategy<K,V> evictionStrategy
-
STRIPE_COUNT
protected static final int STRIPE_COUNT
- See Also:
- Constant Field Values
-
stripes
protected final ReentrantReadWriteLock[] stripes
-
scheduler
protected final ScheduledExecutorService scheduler
-
currentSize
protected final AtomicLong currentSize
-
currentWeight
protected final AtomicLong currentWeight
-
-
Constructor Detail
-
ConcurrentCacheBase
protected ConcurrentCacheBase(CacheConfig<K,V> config)
Constructor for concurrent cache implementations.- Parameters:
config
- the cache configuration
-
-
Method Detail
-
doGet
protected V doGet(K key)
Description copied from class:AbstractCacheBase
Implementation-specific get operation.- Specified by:
doGet
in classAbstractCacheBase<K,V>
- Parameters:
key
- the key (already validated)- Returns:
- the value or null if not found/expired
-
doPut
protected void doPut(K key, V value)
Description copied from class:AbstractCacheBase
Implementation-specific put operation.- Specified by:
doPut
in classAbstractCacheBase<K,V>
- Parameters:
key
- the key (already validated)value
- the value (already validated)
-
doRemove
protected V doRemove(K key)
Description copied from class:AbstractCacheBase
Implementation-specific remove operation.- Specified by:
doRemove
in classAbstractCacheBase<K,V>
- Parameters:
key
- the key (already validated)- Returns:
- the removed value or null if not found
-
doClear
protected void doClear()
Description copied from class:DataBackedCacheBase
Common clear implementation.- Overrides:
doClear
in classDataBackedCacheBase<K,V,CacheEntry<V>>
-
doContainsKey
protected boolean doContainsKey(K key)
Description copied from class:DataBackedCacheBase
Common implementation of containsKey with entry validation.- Overrides:
doContainsKey
in classDataBackedCacheBase<K,V,CacheEntry<V>>
- Parameters:
key
- the key (already validated)- Returns:
- true if key exists and is not expired
-
size
public long size()
Description copied from class:DataBackedCacheBase
Common implementation of size() method. Can be overridden by subclasses that track size differently.- Specified by:
size
in interfaceCache<K,V>
- Overrides:
size
in classDataBackedCacheBase<K,V,CacheEntry<V>>
- Returns:
- the number of entries in this cache
-
extractValue
protected V extractValue(CacheEntry<V> entry)
Description copied from class:DataBackedCacheBase
Extract the value from an entry.- Specified by:
extractValue
in classDataBackedCacheBase<K,V,CacheEntry<V>>
- Parameters:
entry
- the entry- Returns:
- the extracted value
-
isValidEntry
protected boolean isValidEntry(CacheEntry<V> entry)
Description copied from class:DataBackedCacheBase
Check if an entry is valid (not expired, not corrupted, etc.).- Specified by:
isValidEntry
in classDataBackedCacheBase<K,V,CacheEntry<V>>
- Parameters:
entry
- the entry to validate- Returns:
- true if the entry is valid
-
getStripeIndex
protected int getStripeIndex(K key)
Get the stripe index for a key to enable striped locking.
-
getReadLock
protected ReentrantReadWriteLock.ReadLock getReadLock(K key)
Get read lock for a specific key.
-
getWriteLock
protected ReentrantReadWriteLock.WriteLock getWriteLock(K key)
Get write lock for a specific key.
-
enforceSize
protected void enforceSize()
Override size enforcement to use eviction strategy.- Overrides:
enforceSize
in classAbstractCacheBase<K,V>
-
shouldAvoidEvictingNewEntries
protected boolean shouldAvoidEvictingNewEntries()
Determines if we should avoid evicting newly added entries. FILO and FIFO strategies should not avoid evicting new entries.
-
evictEntries
protected void evictEntries()
-
evictEntries
protected void evictEntries(K keyToAvoid)
Evict entries based on the eviction strategy, avoiding a specific key.
-
notifyListeners
protected void notifyListeners(Consumer<CacheEventListener<K,V>> action)
Notify all registered event listeners.
-
scheduleCleanup
protected void scheduleCleanup()
Schedule periodic cleanup of expired entries.
-
scheduleRefresh
protected void scheduleRefresh()
Schedule refresh operations for entries.
-
cleanupExpiredEntries
protected void cleanupExpiredEntries()
Clean up expired entries.
-
refreshEntries
protected void refreshEntries()
Refresh entries that need refreshing.
-
shutdown
public void shutdown()
Shutdown the cache and clean up resources.
-
getCurrentWeight
protected long getCurrentWeight()
Description copied from class:AbstractCacheBase
Calculates the current total weight of entries in the cache. Default implementation returns size as weight. Subclasses should override if they track weight separately.- Overrides:
getCurrentWeight
in classAbstractCacheBase<K,V>
- Returns:
- the current total weight
-
createCacheEntry
protected CacheEntry<V> createCacheEntry(K key, V value)
Creates a cache entry with proper weight calculation. Overrides the parent method to pass the key to the weigher.
-
createCacheEntry
protected CacheEntry<V> createCacheEntry(V value)
Creates a cache entry with proper weight calculation. Uses null as key - this is called from parent class.- Overrides:
createCacheEntry
in classAbstractCacheBase<K,V>
- Parameters:
value
- the value to wrap- Returns:
- a new cache entry
-
-