KEYTYPE - The cache source typeKEYSTORETYPE - The internal storage key typeVALUETYPE - The cache value type@ThreadSafe public class MappedCache<KEYTYPE,KEYSTORETYPE,VALUETYPE> extends Object implements IMutableCache<KEYTYPE,VALUETYPE>
ICache and IMutableCache.Cache class.| Modifier and Type | Field and Description |
|---|---|
protected SimpleReadWriteLock |
m_aRWLock |
static int |
NO_MAX_SIZE
A constant indicating, that a cache has no max size
|
static String |
STATISTICS_PREFIX
The prefix to be used for statistics elements
|
| Constructor and Description |
|---|
MappedCache(Function<KEYTYPE,KEYSTORETYPE> aCacheKeyProvider,
Function<KEYTYPE,VALUETYPE> aValueProvider,
int nMaxSize,
String sCacheName,
boolean bAllowNullValues)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
EChange |
clearCache()
Remove all cached elements.
|
protected ICommonsMap<KEYSTORETYPE,Wrapper<VALUETYPE>> |
createCache()
Create a new cache map.
|
protected Function<KEYTYPE,KEYSTORETYPE> |
getCacheKeyProvider() |
VALUETYPE |
getFromCache(KEYTYPE aKey)
Get the cached value associated with the passed key.
|
protected Wrapper<VALUETYPE> |
getFromCacheNoStats(KEYSTORETYPE aCacheKey) |
protected Wrapper<VALUETYPE> |
getFromCacheNoStatsNotLocked(KEYSTORETYPE aCacheKey) |
int |
getMaxSize() |
String |
getName() |
protected Function<KEYTYPE,VALUETYPE> |
getValueProvider() |
boolean |
hasMaxSize() |
boolean |
isAllowNullValues() |
boolean |
isEmpty() |
boolean |
isInCache(KEYTYPE aKey)
Check if the passed key is already in the cache or not.
|
boolean |
isNotEmpty() |
protected void |
putInCache(KEYTYPE aKey,
VALUETYPE aValue)
Put a new value into the cache.
|
protected void |
putInCacheNotLocked(KEYSTORETYPE aCacheKey,
Wrapper<VALUETYPE> aCacheValue)
Put a new value into the cache.
|
EChange |
removeFromCache(KEYTYPE aKey)
Remove the given key from the cache.
|
int |
size() |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetComparatorCollating, getComparatorNamepublic static final String STATISTICS_PREFIX
public static final int NO_MAX_SIZE
protected final SimpleReadWriteLock m_aRWLock
public MappedCache(@Nonnull Function<KEYTYPE,KEYSTORETYPE> aCacheKeyProvider, @Nonnull Function<KEYTYPE,VALUETYPE> aValueProvider, int nMaxSize, @Nonnull @Nonempty String sCacheName, boolean bAllowNullValues)
aCacheKeyProvider - The cache key provider, that takes any KEYTYPE and creates a
non-null KEYSTORETYPE instance. May not be
null.aValueProvider - The cache value provider. The value to be cached may be
null depending on the parameter
bAllowNullValues. May not be null.nMaxSize - The maximum size of the cache. All values ≤ 0 indicate an
unlimited size.sCacheName - The internal name of the cache. May neither be null nor
empty. This name is NOT checked for uniqueness.bAllowNullValues - true if null values are allowed to be in
the cache, false if not.@Nonnull protected final Function<KEYTYPE,KEYSTORETYPE> getCacheKeyProvider()
null.@Nonnull protected final Function<KEYTYPE,VALUETYPE> getValueProvider()
null.public final int getMaxSize()
hasMaxSize()public final boolean hasMaxSize()
true if this cache has a size limit,
false if not.getMaxSize()public final boolean isAllowNullValues()
true if null can be in the cache,
false if not.@Nonnull @ReturnsMutableCopy @OverrideOnDemand @CodingStyleguideUnaware protected ICommonsMap<KEYSTORETYPE,Wrapper<VALUETYPE>> createCache()
null.@MustBeLocked(value=WRITE) protected final void putInCacheNotLocked(@Nonnull KEYSTORETYPE aCacheKey, @Nonnull Wrapper<VALUETYPE> aCacheValue)
aCacheKey - The cache key. May not be null.aCacheValue - The cache value. May not be null.@IsLocked(value=WRITE) protected final void putInCache(KEYTYPE aKey, VALUETYPE aValue)
aKey - The cache key. May be null depending on the cache key
provider.aValue - The cache value. May be null depending on the settings.@Nullable @MustBeLocked(value=READ) protected final Wrapper<VALUETYPE> getFromCacheNoStatsNotLocked(@Nullable KEYSTORETYPE aCacheKey)
@Nullable @IsLocked(value=READ) protected final Wrapper<VALUETYPE> getFromCacheNoStats(@Nullable KEYSTORETYPE aCacheKey)
public final boolean isInCache(KEYTYPE aKey)
aKey - The key to check. May be null.true if the value is already in the cache,
false if not.public VALUETYPE getFromCache(KEYTYPE aKey)
ICachegetFromCache in interface ICache<KEYTYPE,VALUETYPE>aKey - The key to be looked up. May be nullable or not -
depends upon the implementation.null if no such value is in the cache.@Nonnull @OverridingMethodsMustInvokeSuper public EChange removeFromCache(KEYTYPE aKey)
IMutableCacheremoveFromCache in interface IMutableCache<KEYTYPE,VALUETYPE>aKey - The key to be removed. May be nullable or not - depends
upon the implementation.EChange.CHANGED upon success, EChange.UNCHANGED if
the key was not within the cache,@Nonnull @OverridingMethodsMustInvokeSuper public EChange clearCache()
IMutableCacheclearCache in interface IMutableCache<KEYTYPE,VALUETYPE>EChange.@Nonnegative public int size()
public boolean isEmpty()
isEmpty in interface IHasSizetrue if no items are present, false if at
least a single item is present.IHasSize.size(),
IHasSize.isNotEmpty()public boolean isNotEmpty()
isNotEmpty in interface IHasSizetrue if at least one item is present,
false if no item is present.IHasSize.size(),
IHasSize.isEmpty()Copyright © 2014–2022 Philip Helger. All rights reserved.