Package io.github.dhruv1110.jcachex.impl
Class OptimizedCacheBase<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>
-
- io.github.dhruv1110.jcachex.impl.OptimizedCacheBase<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:
AllocationOptimizedCache
,JITOptimizedCache
,OptimizedCache
public abstract class OptimizedCacheBase<K,V> extends ConcurrentCacheBase<K,V>
Base class for optimized cache implementations providing performance-oriented features. This class extends ConcurrentCacheBase and adds: - Lock-free read operations where possible - Frequency sketching for access patterns - Batched operations to reduce contention - Adaptive eviction strategies - Performance monitoring and optimization
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
OptimizedCacheBase.State
Internal state for operation coordination.
-
Field Summary
Fields Modifier and Type Field Description protected AccessBuffer<K>
accessBuffer
protected static long
DEFAULT_DRAIN_INTERVAL_NANOS
protected static int
DEFAULT_DRAIN_THRESHOLD
protected static int
FREQUENCY_SKETCH_SIZE
protected FrequencySketch
frequencySketch
protected ReentrantReadWriteLock
maintenanceLock
protected AtomicReference<OptimizedCacheBase.State>
operationState
protected AtomicLong
version
protected WindowTinyLFUEvictionStrategy<K,V>
windowTinyLFUStrategy
-
Fields inherited from class io.github.dhruv1110.jcachex.impl.ConcurrentCacheBase
currentSize, currentWeight, evictionStrategy, scheduler, STRIPE_COUNT, 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
OptimizedCacheBase(CacheConfig<K,V> config)
Constructor for optimized cache implementations.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doClear()
Enhanced clear operation with versioning.protected V
doGet(K key)
Optimized get operation with lock-free reads where possible.protected void
doPut(K key, V value)
Optimized put operation with batched processing.protected void
drainAccessBuffer()
Drains the access buffer and updates eviction state.protected void
enforceSize()
Optimized size enforcement using advanced eviction strategies.protected OptimizedCacheBase.State
getOperationState()
Returns the current operation state.String
getPerformanceMetrics()
Returns performance metrics for monitoring.protected long
getVersion()
Returns the current version for optimistic concurrency control.protected void
performCustomOptimization()
Hook for subclasses to perform custom optimization.protected void
performPeriodicMaintenance()
Performs periodic maintenance and optimization.protected void
processAccessRecord(AccessBuffer.AccessRecord<K> record)
Processes an access record for eviction strategy updates.protected void
recordAccess(K key, AccessBuffer.AccessType type, int frequency)
Records access for frequency analysis and batched processing.protected void
scheduleEviction()
Schedules eviction using the optimized strategy.protected void
scheduleOptimizationTasks()
Schedules periodic optimization tasks.protected boolean
shouldAvoidEvictingNewEntries()
Determines if we should avoid evicting newly added entries.void
shutdown()
Shuts down the optimized cache and cleans up resources.-
Methods inherited from class io.github.dhruv1110.jcachex.impl.ConcurrentCacheBase
cleanupExpiredEntries, createCacheEntry, createCacheEntry, doContainsKey, doRemove, evictEntries, evictEntries, extractValue, getCurrentWeight, getReadLock, getStripeIndex, getWriteLock, isValidEntry, loadValue, notifyListeners, refreshEntries, scheduleCleanup, scheduleRefresh, size
-
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
-
windowTinyLFUStrategy
protected final WindowTinyLFUEvictionStrategy<K,V> windowTinyLFUStrategy
-
accessBuffer
protected final AccessBuffer<K> accessBuffer
-
frequencySketch
protected final FrequencySketch frequencySketch
-
operationState
protected final AtomicReference<OptimizedCacheBase.State> operationState
-
version
protected final AtomicLong version
-
maintenanceLock
protected final ReentrantReadWriteLock maintenanceLock
-
DEFAULT_DRAIN_THRESHOLD
protected static final int DEFAULT_DRAIN_THRESHOLD
- See Also:
- Constant Field Values
-
DEFAULT_DRAIN_INTERVAL_NANOS
protected static final long DEFAULT_DRAIN_INTERVAL_NANOS
- See Also:
- Constant Field Values
-
FREQUENCY_SKETCH_SIZE
protected static final int FREQUENCY_SKETCH_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OptimizedCacheBase
protected OptimizedCacheBase(CacheConfig<K,V> config)
Constructor for optimized cache implementations.- Parameters:
config
- the cache configuration
-
-
Method Detail
-
doGet
protected V doGet(K key)
Optimized get operation with lock-free reads where possible.- Overrides:
doGet
in classConcurrentCacheBase<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)
Optimized put operation with batched processing.- Overrides:
doPut
in classConcurrentCacheBase<K,V>
- Parameters:
key
- the key (already validated)value
- the value (already validated)
-
doClear
protected void doClear()
Enhanced clear operation with versioning.- Overrides:
doClear
in classConcurrentCacheBase<K,V>
-
enforceSize
protected void enforceSize()
Optimized size enforcement using advanced eviction strategies.- Overrides:
enforceSize
in classConcurrentCacheBase<K,V>
-
recordAccess
protected void recordAccess(K key, AccessBuffer.AccessType type, int frequency)
Records access for frequency analysis and batched processing.
-
scheduleEviction
protected void scheduleEviction()
Schedules eviction using the optimized strategy.
-
drainAccessBuffer
protected void drainAccessBuffer()
Drains the access buffer and updates eviction state.
-
processAccessRecord
protected void processAccessRecord(AccessBuffer.AccessRecord<K> record)
Processes an access record for eviction strategy updates.
-
scheduleOptimizationTasks
protected void scheduleOptimizationTasks()
Schedules periodic optimization tasks.
-
performPeriodicMaintenance
protected void performPeriodicMaintenance()
Performs periodic maintenance and optimization.
-
getPerformanceMetrics
public String getPerformanceMetrics()
Returns performance metrics for monitoring.
-
shutdown
public void shutdown()
Shuts down the optimized cache and cleans up resources.- Overrides:
shutdown
in classConcurrentCacheBase<K,V>
-
performCustomOptimization
protected void performCustomOptimization()
Hook for subclasses to perform custom optimization.
-
getOperationState
protected OptimizedCacheBase.State getOperationState()
Returns the current operation state.
-
getVersion
protected long getVersion()
Returns the current version for optimistic concurrency control.
-
shouldAvoidEvictingNewEntries
protected boolean shouldAvoidEvictingNewEntries()
Determines if we should avoid evicting newly added entries. FILO and FIFO strategies should not avoid evicting new entries.- Overrides:
shouldAvoidEvictingNewEntries
in classConcurrentCacheBase<K,V>
-
-