Package com.helger.commons.cache
Class MappedCache<KEYTYPE,KEYSTORETYPE,VALUETYPE>
- java.lang.Object
-
- com.helger.commons.cache.MappedCache<KEYTYPE,KEYSTORETYPE,VALUETYPE>
-
- Type Parameters:
KEYTYPE- The cache source typeKEYSTORETYPE- The internal storage key typeVALUETYPE- The cache value type
- All Implemented Interfaces:
ICache<KEYTYPE,VALUETYPE>,IMutableCache<KEYTYPE,VALUETYPE>,IHasSize,IHasName
- Direct Known Subclasses:
Cache
@ThreadSafe public class MappedCache<KEYTYPE,KEYSTORETYPE,VALUETYPE> extends Object implements IMutableCache<KEYTYPE,VALUETYPE>
Base implementation ofICacheandIMutableCache.- Since:
- 9.3.8 generalized from the existing
Cacheclass. - Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description protected SimpleReadWriteLockm_aRWLockstatic intNO_MAX_SIZEA constant indicating, that a cache has no max sizestatic StringSTATISTICS_PREFIXThe prefix to be used for statistics elements
-
Constructor Summary
Constructors Constructor Description MappedCache(Function<KEYTYPE,KEYSTORETYPE> aCacheKeyProvider, Function<KEYTYPE,VALUETYPE> aValueProvider, int nMaxSize, String sCacheName, boolean bAllowNullValues)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EChangeclearCache()Remove all cached elements.protected ICommonsMap<KEYSTORETYPE,Wrapper<VALUETYPE>>createCache()Create a new cache map.protected Function<KEYTYPE,KEYSTORETYPE>getCacheKeyProvider()VALUETYPEgetFromCache(KEYTYPE aKey)Get the cached value associated with the passed key.protected Wrapper<VALUETYPE>getFromCacheNoStats(KEYSTORETYPE aCacheKey)protected Wrapper<VALUETYPE>getFromCacheNoStatsNotLocked(KEYSTORETYPE aCacheKey)intgetMaxSize()StringgetName()protected Function<KEYTYPE,VALUETYPE>getValueProvider()booleanhasMaxSize()booleanisAllowNullValues()booleanisEmpty()booleanisInCache(KEYTYPE aKey)Check if the passed key is already in the cache or not.booleanisNotEmpty()protected voidputInCache(KEYTYPE aKey, VALUETYPE aValue)Put a new value into the cache.protected voidputInCacheNotLocked(KEYSTORETYPE aCacheKey, Wrapper<VALUETYPE> aCacheValue)Put a new value into the cache.EChangeremoveFromCache(KEYTYPE aKey)Remove the given key from the cache.intsize()StringtoString()
-
-
-
Field Detail
-
STATISTICS_PREFIX
public static final String STATISTICS_PREFIX
The prefix to be used for statistics elements- See Also:
- Constant Field Values
-
NO_MAX_SIZE
public static final int NO_MAX_SIZE
A constant indicating, that a cache has no max size- See Also:
- Constant Field Values
-
m_aRWLock
protected final SimpleReadWriteLock m_aRWLock
-
-
Constructor Detail
-
MappedCache
public MappedCache(@Nonnull Function<KEYTYPE,KEYSTORETYPE> aCacheKeyProvider, @Nonnull Function<KEYTYPE,VALUETYPE> aValueProvider, int nMaxSize, @Nonnull @Nonempty String sCacheName, boolean bAllowNullValues)
Constructor- Parameters:
aCacheKeyProvider- The cache key provider, that takes any KEYTYPE and creates a non-nullKEYSTORETYPE instance. May not benull.aValueProvider- The cache value provider. The value to be cached may benulldepending on the parameterbAllowNullValues. May not benull.nMaxSize- The maximum size of the cache. All values ≤ 0 indicate an unlimited size.sCacheName- The internal name of the cache. May neither benullnor empty. This name is NOT checked for uniqueness.bAllowNullValues-trueifnullvalues are allowed to be in the cache,falseif not.
-
-
Method Detail
-
getCacheKeyProvider
@Nonnull protected final Function<KEYTYPE,KEYSTORETYPE> getCacheKeyProvider()
- Returns:
- The cache key provider from the constructor. Never
null. - Since:
- 9.3.8
-
getValueProvider
@Nonnull protected final Function<KEYTYPE,VALUETYPE> getValueProvider()
- Returns:
- The cache value provider from the constructor. Never
null. - Since:
- 9.3.8
-
getMaxSize
public final int getMaxSize()
- Returns:
- The maximum number of entries allowed in this cache. Values ≤ 0 indicate that the cache size is not limited at all.
- See Also:
hasMaxSize()
-
hasMaxSize
public final boolean hasMaxSize()
- Returns:
trueif this cache has a size limit,falseif not.- See Also:
getMaxSize()
-
isAllowNullValues
public final boolean isAllowNullValues()
- Returns:
trueifnullcan be in the cache,falseif not.- Since:
- 9.3.8
-
createCache
@Nonnull @ReturnsMutableCopy @OverrideOnDemand @CodingStyleguideUnaware protected ICommonsMap<KEYSTORETYPE,Wrapper<VALUETYPE>> createCache()
Create a new cache map. This is the internal map that is used to store the items.- Returns:
- Never
null.
-
putInCacheNotLocked
@MustBeLocked(WRITE) protected final void putInCacheNotLocked(@Nonnull KEYSTORETYPE aCacheKey, @Nonnull Wrapper<VALUETYPE> aCacheValue)
Put a new value into the cache.- Parameters:
aCacheKey- The cache key. May not benull.aCacheValue- The cache value. May not benull.
-
putInCache
@IsLocked(WRITE) protected final void putInCache(KEYTYPE aKey, VALUETYPE aValue)
Put a new value into the cache. Use this in derived classes to e.g. prefill the cache with existing values.- Parameters:
aKey- The cache key. May benulldepending on the cache key provider.aValue- The cache value. May benulldepending on the settings.
-
getFromCacheNoStatsNotLocked
@Nullable @MustBeLocked(READ) protected final Wrapper<VALUETYPE> getFromCacheNoStatsNotLocked(@Nullable KEYSTORETYPE aCacheKey)
-
getFromCacheNoStats
@Nullable @IsLocked(READ) protected final Wrapper<VALUETYPE> getFromCacheNoStats(@Nullable KEYSTORETYPE aCacheKey)
-
isInCache
public final boolean isInCache(KEYTYPE aKey)
Check if the passed key is already in the cache or not.- Parameters:
aKey- The key to check. May benull.- Returns:
trueif the value is already in the cache,falseif not.- Since:
- 9.3.8
-
getFromCache
public VALUETYPE getFromCache(KEYTYPE aKey)
Description copied from interface:ICacheGet the cached value associated with the passed key. If the value is not in the cache, it might be automatically retrieved from a respective provider.- Specified by:
getFromCachein interfaceICache<KEYTYPE,KEYSTORETYPE>- Parameters:
aKey- The key to be looked up. May benullable or not - depends upon the implementation.- Returns:
nullif no such value is in the cache.
-
removeFromCache
@Nonnull @OverridingMethodsMustInvokeSuper public EChange removeFromCache(KEYTYPE aKey)
Description copied from interface:IMutableCacheRemove the given key from the cache.- Specified by:
removeFromCachein interfaceIMutableCache<KEYTYPE,KEYSTORETYPE>- Parameters:
aKey- The key to be removed. May benullable or not - depends upon the implementation.- Returns:
EChange.CHANGEDupon success,EChange.UNCHANGEDif the key was not within the cache,
-
clearCache
@Nonnull @OverridingMethodsMustInvokeSuper public EChange clearCache()
Description copied from interface:IMutableCacheRemove all cached elements.- Specified by:
clearCachein interfaceIMutableCache<KEYTYPE,KEYSTORETYPE>- Returns:
EChange.
-
size
@Nonnegative public int size()
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceIHasSize- Returns:
trueif no items are present,falseif at least a single item is present.- See Also:
IHasSize.size(),IHasSize.isNotEmpty()
-
isNotEmpty
public boolean isNotEmpty()
- Specified by:
isNotEmptyin interfaceIHasSize- Returns:
trueif at least one item is present,falseif no item is present.- See Also:
IHasSize.size(),IHasSize.isEmpty()
-
-