public class CachingMap<K,V> extends Object implements Map<K,V>
If the back map implements ObservableMap interface, the CachingMap provides four different strategies of invalidating the front map entries that have changed by other processes in the back map:
The front map implementation is assumed to be thread safe; additionally
any modifications to the front map are allowed only after the corresponding
lock is acquired against the ControlMap
.
Note: null values are not cached in the front map and therefore this implementation is not optimized for maps that allow null values to be stored.
Modifier and Type | Class and Description |
---|---|
protected class |
CachingMap.DeactivationListener
DeactivationListener for the back NamedCache.
|
protected class |
CachingMap.FrontMapListener
MapListener for front map responsible for deregistering back map
listeners upon front map eviction.
|
protected class |
CachingMap.PrimingListener
MapListener for back map responsible for keeping the front map
coherent with the back map.
|
protected class |
CachingMap.SimpleListener
MapListener for back map responsible for keeping the front map
coherent with the back map.
|
Modifier and Type | Field and Description |
---|---|
static String[] |
INVALIDATION_STRATEGY
|
static int |
LISTEN_ALL
Invalidation strategy that instructs the CachingMap to listen to
all back map events; this strategy is preferred when updates to
the back map are frequent and with high probability come from the
outside of this CachingMap; for example multiple CachingMap instances
using the same back map with a large degree of key set overlap between
front maps.
|
static int |
LISTEN_AUTO
Invalidation strategy that instructs the CachingMap implementation to
switch automatically between LISTEN_PRESENT and LISTEN_ALL strategies
based on the cache statistics.
|
static int |
LISTEN_LOGICAL
Invalidation strategy that instructs the CachingMap to listen to all
back map events that are not synthetic deletes.
|
static int |
LISTEN_NONE
No invalidation strategy.
|
static int |
LISTEN_PRESENT
Invalidation strategy that instructs the CachingMap to listen to the
back map events related only to the items currently present
in the front map; this strategy serves best when the changes to the
back map come mostly from the CachingMap itself.
|
protected com.tangosol.internal.net.NamedCacheDeactivationListener |
m_listenerDeactivation
The NamedCache deactivation listener.
|
protected int |
m_nStrategyCurrent
The current invalidation strategy, which at times could be different
from the target strategy.
|
protected int |
m_nStrategyTarget
The invalidation strategy that this map is to use.
|
Constructor and Description |
---|
CachingMap(Map<K,V> mapFront,
Map<K,V> mapBack)
Construct a CachingMap using two specified maps:
FrontMap (aka "cache", "near" or "shallow") and
BackMap (aka "actual", "real" or "deep").
|
CachingMap(Map<K,V> mapFront,
Map<K,V> mapBack,
int nStrategy)
Construct a CachingMap using two specified maps:
FrontMap (aka "cache", "near" or "shallow") and
BackMap (aka "actual", "real" or "deep")
and using the specified front map invalidation strategy.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears both the front and back maps.
|
boolean |
containsKey(Object oKey) |
boolean |
containsValue(Object oValue) |
protected int |
ensureInvalidationStrategy()
Ensure that a strategy has been chosen and that any appropriate global
listeners have been registered.
|
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object oKey) |
Map<K,V> |
getAll(Collection<? extends K> colKeys)
Get all the specified keys, if they are in the cache.
|
protected Map<K,V> |
getAllFromFrontMap(Collection<? extends K> colKeys)
Retrieve entries from the front map.
|
Map<K,V> |
getBackMap()
Obtain the back map reference.
|
CacheStatistics |
getCacheStatistics()
Obtain the CacheStatistics for this cache.
|
ConcurrentMap |
getControlMap()
Obtain the ConcurrentMap that should be used to synchronize
the front map modification access.
|
Map<K,V> |
getFrontMap()
Obtain the front map reference.
|
long |
getInvalidationHits()
Determine the rough number of front map invalidation hits since
the cache statistics were last reset.
|
long |
getInvalidationMisses()
Determine the rough number of front map invalidation misses since
the cache statistics were last reset.
|
int |
getInvalidationStrategy()
Obtain the invalidation strategy used by this CachingMap.
|
String |
getInvalidationStrategy(int value)
Return string representation for invalidation strategy
value . |
long |
getTotalRegisterListener()
Determine the total number of
registerListener(Object oKey)
operations since the cache statistics were last reset. |
protected MapListener |
instantiateBackMapListener(int nStrategy)
Factory pattern: instantiate back map listener.
|
protected CachingMap.FrontMapListener |
instantiateFrontMapListener()
Factory pattern: instantiate front map listener.
|
protected void |
invalidateFront(Object oKey)
Invalidate the key from the front.
|
protected boolean |
isCheckPrimingExclusively(boolean fPriming)
Return true if we can rely on the server emitting priming events (based
on receiving a at least one priming event from a storage node).
|
protected boolean |
isCoherent()
Determine if changes to the back map affect the front map so that data
in the front map stays in sync.
|
boolean |
isEmpty() |
protected boolean |
isPriming(MapEvent evt)
Check if the specified event is a "priming" one.
|
Set<K> |
keySet() |
V |
put(K oKey,
V oValue) |
V |
put(K oKey,
V oValue,
boolean fReturn,
long cMillis)
Implementation of put method that optionally skips the return value
retrieval and allows to specify an expiry for the cache entry.
|
void |
putAll(Map<? extends K,? extends V> map) |
protected void |
registerDeactivationListener()
Instantiate and register a DeactivationListener with the back cache.
|
protected void |
registerFrontListener()
Register the global front map listener.
|
protected void |
registerListener()
Register the global back map listener.
|
protected void |
registerListener(Object oKey)
Register the back map listener for the specified key.
|
protected void |
registerListeners(Set setKeys)
Register the back map listeners for the specified set of keys.
|
void |
release()
Release the CachingMap.
|
V |
remove(Object oKey) |
protected void |
removeKeyHolder()
Remove the key holder from the ThreadLocal object.
|
void |
resetFrontMap()
Reset the front map.
|
protected void |
resetInvalidationStrategy()
Reset the "current invalidation strategy" flag.
|
protected Set |
setKeyHolder()
Set up a thread local Set to hold all the keys that might be evicted
from the front cache.
|
int |
size()
Return the number of key-value mappings in this map.
|
String |
toString()
For debugging purposes, format the contents of the CachingMap
in a human readable format.
|
protected Set<K> |
tryLock(Set<K> setKeys)
Lock the keys in the given set without blocking.
|
protected void |
unregisterDeactivationListener()
Unregister back cache deactivation listener.
|
protected void |
unregisterFrontListener()
Unregister the global front map listener.
|
protected void |
unregisterListener()
Unregister the global back map listener.
|
protected void |
unregisterListener(Object oKey)
Unregister the back map listener for the specified key.
|
protected void |
unregisterListeners(Set<K> setKeys)
Unregister the back map listener for the specified keys.
|
protected void |
unregisterMBean()
Unregister an associated CacheMBean.
|
protected void |
validate(MapEvent evt)
Validate the front map entry for the specified back map event.
|
Collection<V> |
values()
Obtain an collection of the values contained in this map.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
public static final int LISTEN_NONE
public static final int LISTEN_PRESENT
public static final int LISTEN_ALL
public static final int LISTEN_AUTO
public static final int LISTEN_LOGICAL
public static final String[] INVALIDATION_STRATEGY
protected int m_nStrategyTarget
protected int m_nStrategyCurrent
protected com.tangosol.internal.net.NamedCacheDeactivationListener m_listenerDeactivation
public CachingMap(Map<K,V> mapFront, Map<K,V> mapBack)
LISTEN_AUTO
strategy.mapBack
- back mapmapFront
- front mapSeppukuMapListener
public CachingMap(Map<K,V> mapFront, Map<K,V> mapBack, int nStrategy)
mapFront
- front mapmapBack
- back mapnStrategy
- specifies the strategy used for the front map
invalidation; valid values are LISTEN_* constantspublic void release()
public Map<K,V> getFrontMap()
Note: direct modifications of the returned map may cause an unpredictable behavior of the CachingMap.
public Map<K,V> getBackMap()
Note: direct modifications of the returned map may cause an unpredictable behavior of the CachingMap.
public int getInvalidationStrategy()
public ConcurrentMap getControlMap()
protected boolean isCoherent()
public CacheStatistics getCacheStatistics()
public void clear()
public boolean containsKey(Object oKey)
containsKey
in interface Map<K,V>
public boolean containsValue(Object oValue)
containsValue
in interface Map<K,V>
public Map<K,V> getAll(Collection<? extends K> colKeys)
Note: this implementation does not differentiate between missing keys or null values stored in the back map; in both cases the returned map will not contain the corresponding entry.
colKeys
- a collection of keys that may be in the named cacheprotected Map<K,V> getAllFromFrontMap(Collection<? extends K> colKeys)
colKeys
- a collection of keysprotected Set<K> tryLock(Set<K> setKeys)
setKeys
- keys to lock in the control mapprotected boolean isPriming(MapEvent evt)
evt
- the eventtrue
if the specified event is a "priming" oneprotected boolean isCheckPrimingExclusively(boolean fPriming)
fPriming
- whether the event that instigated this check is a priming
eventpublic V put(K oKey, V oValue, boolean fReturn, long cMillis)
oKey
- the keyoValue
- the valuefReturn
- if true, the return value is required; otherwise
the return value will be ignoredcMillis
- the number of milliseconds until the cache entry will
expireUnsupportedOperationException
- if the requested expiry is a
positive value and either the front map or the back map
implementations do not support the expiration functionalityCacheMap.put(Object oKey, Object oValue, long cMillis)
protected void invalidateFront(Object oKey)
oKey
- the key to invalidateprotected void validate(MapEvent evt)
evt
- the MapEvent from the back mappublic int size()
public Collection<V> values()
public long getInvalidationHits()
An invalidation hit is an externally induced map event for an entry that exists in the front map.
public long getInvalidationMisses()
public long getTotalRegisterListener()
registerListener(Object oKey)
operations since the cache statistics were last reset.public String toString()
protected void registerListener()
protected void unregisterListener()
protected void registerListener(Object oKey)
oKey
- the keyprotected void registerListeners(Set setKeys)
setKeys
- the key setprotected void unregisterListener(Object oKey)
oKey
- the keyprotected void unregisterListeners(Set<K> setKeys)
Note: all the keys in the passed-in set must be locked and will be unlocked.
setKeys
- Set of keys to unregister (and unlock)protected Set setKeyHolder()
protected void removeKeyHolder()
protected void registerFrontListener()
protected void unregisterFrontListener()
protected int ensureInvalidationStrategy()
protected void resetInvalidationStrategy()
This method should be called only while the access to the front map is fully synchronised and the front map is empty to prevent stalled data.
protected MapListener instantiateBackMapListener(int nStrategy)
nStrategy
- the strategy to instantiate a back map listener forprotected void registerDeactivationListener()
protected void unregisterDeactivationListener()
public void resetFrontMap()
public String getInvalidationStrategy(int value)
value
.value
- one of LISTEN_*protected void unregisterMBean()
protected CachingMap.FrontMapListener instantiateFrontMapListener()