Class DataBackedCacheBase<K,V,E>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.impl.base.AbstractCacheBase<K,V>
-
- io.github.dhruv1110.jcachex.impl.base.DataBackedCacheBase<K,V,E>
-
- Type Parameters:
K
- the type of keysV
- the type of valuesE
- 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
-
-
Field Summary
Fields Modifier and Type Field Description protected ConcurrentHashMap<K,E>
data
-
Fields inherited from class io.github.dhruv1110.jcachex.impl.base.AbstractCacheBase
config, hasExpiration, hasMaximumSize, hasMaximumWeight, hitCount, maximumSize, missCount, stats, statsEnabled
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DataBackedCacheBase(CacheConfig<K,V> config)
Constructor for data-backed cache implementations.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
afterClear()
Called after clearing the data map.protected void
beforeClear()
Called before clearing the data map.protected Map.Entry<K,V>
createMapEntry(K key, V value)
Create a Map.Entry for the entries() collection view.protected void
doClear()
Common clear implementation.protected boolean
doContainsKey(K key)
Common implementation of containsKey with entry validation.Set<Map.Entry<K,V>>
entries()
Common implementation of entries() method with filtering.protected abstract V
extractValue(E entry)
Extract the value from an entry.protected ConcurrentHashMap<K,E>
getDataMap()
Get direct access to the underlying data map.protected abstract boolean
isValidEntry(E entry)
Check if an entry is valid (not expired, not corrupted, etc.).protected boolean
isValidValue(V value)
Check if an extracted value should be included in collection views.Set<K>
keys()
Common implementation of keys() method.long
size()
Common implementation of size() method.Collection<V>
values()
Common implementation of values() method with filtering.-
Methods inherited from class io.github.dhruv1110.jcachex.impl.base.AbstractCacheBase
clear, clearAsync, config, containsKey, createCacheEntry, doGet, doPut, doRemove, enforceSize, get, getAsync, getCurrentWeight, isEntryExpired, isSizeLimitReached, put, putAsync, recordClearStatistics, recordGetStatistics, recordPutStatistics, recordRemoveStatistics, remove, removeAsync, stats, updateStatsFromCounters, validateKey, validateValue
-
-
-
-
Field Detail
-
data
protected final ConcurrentHashMap<K,E> data
-
-
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 classAbstractCacheBase<K,V>
- Parameters:
key
- the key (already validated)- Returns:
- true if key exists and is not expired
-
doClear
protected void doClear()
Common clear implementation.- Specified by:
doClear
in classAbstractCacheBase<K,V>
-
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 keyvalue
- 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
-
-