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 of
ICache and IMutableCache.- Since:
- 9.3.8 generalized from the existing
Cacheclass. - Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SimpleReadWriteLockstatic final intA constant indicating, that a cache has no max sizestatic final StringThe prefix to be used for statistics elements -
Constructor Summary
ConstructorsConstructorDescriptionMappedCache(Function<KEYTYPE, KEYSTORETYPE> aCacheKeyProvider, Function<KEYTYPE, VALUETYPE> aValueProvider, int nMaxSize, String sCacheName, boolean bAllowNullValues) Constructor -
Method Summary
Modifier and TypeMethodDescriptionRemove all cached elements.protected ICommonsMap<KEYSTORETYPE, Wrapper<VALUETYPE>> Create a new cache map.protected final Function<KEYTYPE, KEYSTORETYPE> getFromCache(KEYTYPE aKey) Get the cached value associated with the passed key.getFromCacheNoStats(KEYSTORETYPE aCacheKey) getFromCacheNoStatsNotLocked(KEYSTORETYPE aCacheKey) final intfinal StringgetName()final booleanfinal booleanbooleanisEmpty()final booleanCheck if the passed key is already in the cache or not.booleanprotected final voidputInCache(KEYTYPE aKey, VALUETYPE aValue) Put a new value into the cache.protected final voidputInCacheNotLocked(KEYSTORETYPE aCacheKey, Wrapper<VALUETYPE> aCacheValue) Put a new value into the cache.removeFromCache(KEYTYPE aKey) Remove the given key from the cache.intsize()toString()
-
Field Details
-
STATISTICS_PREFIX
The prefix to be used for statistics elements- See Also:
-
NO_MAX_SIZE
public static final int NO_MAX_SIZEA constant indicating, that a cache has no max size- See Also:
-
m_aRWLock
-
-
Constructor Details
-
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 Details
-
getCacheKeyProvider
- Returns:
- The cache key provider from the constructor. Never
null. - Since:
- 9.3.8
-
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
public final boolean hasMaxSize()- Returns:
trueif this cache has a size limit,falseif not.- See Also:
-
getName
-
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
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
-
isInCache
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
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
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
Description copied from interface:IMutableCacheRemove all cached elements.- Specified by:
clearCachein interfaceIMutableCache<KEYTYPE,KEYSTORETYPE> - Returns:
EChange.
-
size
-
isEmpty
public boolean isEmpty() -
isNotEmpty
public boolean isNotEmpty()- Specified by:
isNotEmptyin interfaceIHasSize- Returns:
trueif at least one item is present,falseif no item is present.- See Also:
-
toString
-