Class DataBackedCacheBase<K,​V,​E>

  • Type Parameters:
    K - the type of keys
    V - the type of values
    E - the type of entries stored in the data map
    All Implemented Interfaces:
    Cache<K,​V>
    Direct Known Subclasses:
    ConcurrentCacheBase

    public abstract class DataBackedCacheBase<K,​V,​E>
    extends AbstractCacheBase<K,​V>
    Abstract base class for cache implementations that store data in a ConcurrentHashMap. This class eliminates code duplication by providing common implementations for: - Collection view methods (keys, values, entries) - Data storage management - Entry value extraction patterns - Common data access patterns
    • Constructor Detail

      • DataBackedCacheBase

        protected DataBackedCacheBase​(CacheConfig<K,​V> config)
        Constructor for data-backed cache implementations.
        Parameters:
        config - the cache configuration
    • Method Detail

      • keys

        public final Set<K> keys()
        Common implementation of keys() method. Returns the key set from the underlying data map.
        Returns:
        a set view of the keys contained in this cache
      • values

        public final Collection<V> values()
        Common implementation of values() method with filtering. Subclasses provide the value extraction and filtering logic.
        Returns:
        a collection view of the values contained in this cache
      • entries

        public final Set<Map.Entry<K,​V>> entries()
        Common implementation of entries() method with filtering. Subclasses provide the value extraction and filtering logic.
        Returns:
        a set view of the mappings contained in this cache
      • size

        public long size()
        Common implementation of size() method. Can be overridden by subclasses that track size differently.
        Returns:
        the number of entries in this cache
      • doContainsKey

        protected boolean doContainsKey​(K key)
        Common implementation of containsKey with entry validation.
        Specified by:
        doContainsKey in class AbstractCacheBase<K,​V>
        Parameters:
        key - the key (already validated)
        Returns:
        true if key exists and is not expired
      • extractValue

        protected abstract V extractValue​(E entry)
        Extract the value from an entry.
        Parameters:
        entry - the entry
        Returns:
        the extracted value
      • isValidEntry

        protected abstract boolean isValidEntry​(E entry)
        Check if an entry is valid (not expired, not corrupted, etc.).
        Parameters:
        entry - the entry to validate
        Returns:
        true if the entry is valid
      • isValidValue

        protected boolean isValidValue​(V value)
        Check if an extracted value should be included in collection views. Default implementation includes null values.
        Parameters:
        value - the extracted value
        Returns:
        true if the value should be included
      • createMapEntry

        protected Map.Entry<K,​V> createMapEntry​(K key,
                                                      V value)
        Create a Map.Entry for the entries() collection view. Uses a simple immutable entry by default.
        Parameters:
        key - the key
        value - the value
        Returns:
        a Map.Entry
      • beforeClear

        protected void beforeClear()
        Called before clearing the data map. Subclasses can override to perform cleanup operations.
      • afterClear

        protected void afterClear()
        Called after clearing the data map. Subclasses can override to reset additional state.
      • getDataMap

        protected final ConcurrentHashMap<K,​E> getDataMap()
        Get direct access to the underlying data map. Should be used carefully and only when necessary.
        Returns:
        the underlying data map