Class ConcurrentCacheBase<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - 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
    • 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 class AbstractCacheBase<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 class AbstractCacheBase<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 class AbstractCacheBase<K,​V>
        Parameters:
        key - the key (already validated)
        Returns:
        the removed value or null if not found
      • doContainsKey

        protected boolean doContainsKey​(K key)
        Description copied from class: DataBackedCacheBase
        Common implementation of containsKey with entry validation.
        Overrides:
        doContainsKey in class DataBackedCacheBase<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 interface Cache<K,​V>
        Overrides:
        size in class DataBackedCacheBase<K,​V,​CacheEntry<V>>
        Returns:
        the number of entries in this cache
      • getStripeIndex

        protected int getStripeIndex​(K key)
        Get the stripe index for a key to enable striped locking.
      • enforceSize

        protected void enforceSize()
        Override size enforcement to use eviction strategy.
        Overrides:
        enforceSize in class AbstractCacheBase<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.
      • loadValue

        protected V loadValue​(K key)
        Load value using configured loader.
      • 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 class AbstractCacheBase<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 class AbstractCacheBase<K,​V>
        Parameters:
        value - the value to wrap
        Returns:
        a new cache entry