Class LocalCache
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.tangosol.util.SafeHashMap
-
- com.tangosol.net.cache.OldCache
-
- com.tangosol.net.cache.LocalCache
-
- All Implemented Interfaces:
CacheMap
,ConfigurableCacheMap
,ObservableMap
,Serializable
,Cloneable
,Map
public class LocalCache extends OldCache implements CacheMap
A LocalCache implementation that supports the JCache API, CacheLoader and CacheStore objects.- Since:
- Coherence 2.2
- Author:
- cp 2003.05.30
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LocalCache.Entry
A holder for a cached value.protected class
LocalCache.InternalListener
An internal MapListener that listens to this cache and reports changes to the CacheStore.protected class
LocalCache.KeyMask
A class that masks certain changes so that they are not reported back to the CacheStore.-
Nested classes/interfaces inherited from class com.tangosol.net.cache.OldCache
OldCache.EntrySet, OldCache.EvictionPolicy, OldCache.InternalEvictionPolicy, OldCache.InternalUnitCalculator, OldCache.KeySet, OldCache.UnitCalculator, OldCache.ValuesCollection
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
Nested classes/interfaces inherited from interface com.tangosol.net.cache.ConfigurableCacheMap
ConfigurableCacheMap.EvictionApprover
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_EXPIRE
By default, the cache entries never expire.protected LocalCache.KeyMask
DEFAULT_KEY_MASK
The default key mask that ignores nothing.static int
DEFAULT_UNITS
By default, the cache size (in units) is infinite.-
Fields inherited from class com.tangosol.net.cache.OldCache
DEFAULT_FLUSH, DEFAULT_PRUNE, EVICTION_POLICY_EXTERNAL, EVICTION_POLICY_HYBRID, EVICTION_POLICY_LFU, EVICTION_POLICY_LRU, INSTANCE_BINARY, INSTANCE_FIXED, INSTANCE_HYBRID, INSTANCE_LFU, INSTANCE_LRU, m_apprvrEvict, m_arrayExpiry, m_calculator, m_cAvgTouch, m_cCurUnits, m_cExpiryDelay, m_cMaxUnits, m_cPruneUnits, m_dflPruneLevel, m_fIncrementalEvict, m_fOptimizeGetTime, m_iterEvict, m_listenerSupport, m_lLastPrune, m_lNextFlush, m_nCalculatorType, m_nEvictionType, m_nUnitFactor, m_policy, m_stats, UNIT_CALCULATOR_BINARY, UNIT_CALCULATOR_EXTERNAL, UNIT_CALCULATOR_FIXED
-
Fields inherited from class com.tangosol.util.SafeHashMap
BIGGEST_MODULO, DEFAULT_GROWTHRATE, DEFAULT_INITIALSIZE, DEFAULT_LOADFACTOR, m_aeBucket, m_cCapacity, m_cEntries, m_colValues, m_flGrowthRate, m_flLoadFactor, m_oIterActive, m_setEntries, m_setKeys, RESIZING
-
Fields inherited from interface com.tangosol.net.cache.CacheMap
EXPIRY_DEFAULT, EXPIRY_NEVER
-
-
Constructor Summary
Constructors Constructor Description LocalCache()
Construct the cache manager.LocalCache(int cUnits)
Construct the cache manager.LocalCache(int cUnits, int cExpiryMillis)
Construct the cache manager.LocalCache(int cUnits, int cExpiryMillis, CacheLoader loader)
Construct the cache manager.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all mappings from this map.Object
get(Object oKey)
If the specified item is in the cache, return it.Map
getAll(Collection colKeys)
Get all the specified keys, if they are in the cache.CacheLoader
getCacheLoader()
Determine the loader used by this LocalCache, if any.protected CacheStore
getCacheStore()
Determine the store used by this LocalCache, if any.SafeHashMap.Entry
getEntry(Object oKey)
Locate an Entry in the hash map based on its key.protected LocalCache.KeyMask
getKeyMask()
ThreadLocal: Get the current key mask for the current thread.protected SafeHashMap.Entry
instantiateEntry()
Factory method.protected MapListener
instantiateInternalListener()
Factory pattern: Instantiate an internal MapListener to listen to this cache and report changes to the CacheStore.protected MapEvent
instantiateMapEvent(int nId, Object oKey, Object oValueOld, Object oValueNew)
Factory pattern: instantiate a new CacheEvent corresponding to the specified parameters.void
load(Object oKey)
Indicates to the cache that the specified key should be loaded into the cache, if it is not already in the cache.void
loadAll()
Indicates to the cache that it should load data from its loader to fill the cache; this is sometimes referred to as "pre-loading" or "warming" a cache.void
loadAll(Collection colKeys)
Indicates to the cache that the specified keys should be loaded into the cache, if they are not already in the cache.Object
peek(Object oKey)
Checks for a valid entry corresponding to the specified key in the cache, and returns the corresponding value if it is.Map
peekAll(Collection colKeys)
Checks for a valid entry corresponding to each specified key in the cache, and places the corresponding value in the returned map if it is.Object
remove(Object oKey)
Removes the mapping for this key from this map if present.protected boolean
removeEvicted(OldCache.Entry entry)
Remove an entry (if it is eligible for eviction) because it has expired.void
setCacheLoader(CacheLoader loader)
Specify the loader used by this LocalCache.protected void
setKeyMask(LocalCache.KeyMask mask)
ThreadLocal: Set the key mask for the current thread.-
Methods inherited from class com.tangosol.net.cache.OldCache
addMapListener, addMapListener, addMapListener, adjustUnits, checkFlush, configureEviction, configureUnitCalculator, containsKey, dispatchEvent, evict, evict, evictAll, getCacheEntry, getCacheHits, getCacheMisses, getCacheStatistics, getCurrentTimeMillis, getEntryInternal, getEvictionApprover, getEvictionPolicy, getEvictionType, getExpiryDelay, getFlushTime, getHighUnits, getHitProbability, getLowUnits, getMapListenerSupport, getNextExpiryTime, getUnitCalculator, getUnitCalculatorType, getUnitFactor, getUnits, hasListeners, instantiateEntrySet, instantiateKeySet, instantiateValuesCollection, isEmpty, isIncrementalEviction, prune, put, put, removeExpired, removeIfExpired, removeMapListener, removeMapListener, removeMapListener, resetHitStatistics, setEvictionApprover, setEvictionPolicy, setEvictionType, setExpiryDelay, setFlushTime, setHighUnits, setIncrementalEviction, setLowUnits, setOptimizeGetTime, setUnitCalculator, setUnitCalculatorType, setUnitFactor, size, toExternalUnits, toInternalUnits, toString, tryEvict
-
Methods inherited from class com.tangosol.util.SafeHashMap
clone, cloneEntryList, entrySet, getBucketIndex, getStableBucketArray, grow, instantiateEntry, isActiveIterator, iteratorActivated, iteratorDeactivated, keySet, removeEntryInternal, values
-
Methods inherited from class java.util.AbstractMap
containsValue, equals, hashCode, putAll
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, size, values
-
Methods inherited from interface com.tangosol.util.ObservableMap
addMapListener, addMapListener, addMapListener, removeMapListener, removeMapListener, removeMapListener
-
-
-
-
Field Detail
-
DEFAULT_UNITS
public static final int DEFAULT_UNITS
By default, the cache size (in units) is infinite.- See Also:
- Constant Field Values
-
DEFAULT_EXPIRE
public static final int DEFAULT_EXPIRE
By default, the cache entries never expire.- See Also:
- Constant Field Values
-
DEFAULT_KEY_MASK
protected final LocalCache.KeyMask DEFAULT_KEY_MASK
The default key mask that ignores nothing.
-
-
Constructor Detail
-
LocalCache
public LocalCache()
Construct the cache manager.
-
LocalCache
public LocalCache(int cUnits)
Construct the cache manager.- Parameters:
cUnits
- the number of units that the cache manager will cache before pruning the cache
-
LocalCache
public LocalCache(int cUnits, int cExpiryMillis)
Construct the cache manager.- Parameters:
cUnits
- the number of units that the cache manager will cache before pruning the cachecExpiryMillis
- the number of milliseconds that each cache entry lives before being automatically expired
-
LocalCache
public LocalCache(int cUnits, int cExpiryMillis, CacheLoader loader)
Construct the cache manager.- Parameters:
cUnits
- the number of units that the cache manager will cache before pruning the cachecExpiryMillis
- the number of milliseconds that each cache entry lives before being automatically expiredloader
- the CacheLoader or CacheStore to use
-
-
Method Detail
-
clear
public void clear()
Removes all mappings from this map.
-
remove
public Object remove(Object oKey)
Removes the mapping for this key from this map if present.- Specified by:
remove
in interfaceMap
- Overrides:
remove
in classOldCache
- Parameters:
oKey
- key whose mapping is to be removed from the map- Returns:
- previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
-
getCacheLoader
public CacheLoader getCacheLoader()
Determine the loader used by this LocalCache, if any.- Returns:
- the loader used by this LocalCache, or null if none
-
setCacheLoader
public void setCacheLoader(CacheLoader loader)
Specify the loader used by this LocalCache.- Parameters:
loader
- loader to use, or null
-
get
public Object get(Object oKey)
If the specified item is in the cache, return it. Otherwise, load the value for the specified key and return it.
-
getEntry
public SafeHashMap.Entry getEntry(Object oKey)
Locate an Entry in the hash map based on its key. If the Entry is not in the cache, load the Entry for the specified key and return it.
-
getAll
public Map getAll(Collection colKeys)
Get all the specified keys, if they are in the cache. For each key that is in the cache, that key and its corresponding value will be placed in the map that is returned by this method. The absence of a key in the returned map indicates that it was not in the cache, which may imply (for caches that can load behind the scenes) that the requested data could not be loaded.The result of this method is defined to be semantically the same as the following implementation, without regards to threading issues:
Map map = new AnyMap(); // could be hash map or ... for (Iterator iter = col.iterator(); iter.hasNext(); ) { Object oKey = iter.next(); Object oVal = get(oKey); if (oVal != null || containsKey(oKey)) { map.put(oKey, oVal); } } return map;
-
load
public void load(Object oKey)
Indicates to the cache that the specified key should be loaded into the cache, if it is not already in the cache. This provides a means to "pre-load" a single entry into the cache using the cache's loader.If a valid entry with the specified key already exists in the cache, or if the cache does not have a loader, then this method has no effect.
An implementation may perform the load operation asynchronously.
- Parameters:
oKey
- the key to request to be loaded
-
loadAll
public void loadAll()
Indicates to the cache that it should load data from its loader to fill the cache; this is sometimes referred to as "pre-loading" or "warming" a cache.The specific set of data that will be loaded is unspecified. The implementation may choose to load all data, some specific subset of the data, or no data. An implementation may require that the loader implement the IterableCacheLoader interface in order for this method to load any data.
An implementation may perform the load operation asynchronously.
-
loadAll
public void loadAll(Collection colKeys)
Indicates to the cache that the specified keys should be loaded into the cache, if they are not already in the cache. This provides a means to "pre-load" entries into the cache using the cache's loader.The result of this method is defined to be semantically the same as the following implementation:
CacheLoader loader = getCacheLoader(); if (loader != null && !colKeys.isEmpty()) { Set setRequest = new HashSet(colKeys); setRequest.removeAll(peekAll(colKeys).keySet()); if (!setRequest.isEmpty()) { Map map = loader.loadAll(colKeys); if (!map.isEmpty()) { putAll(map); } } }
- Parameters:
colKeys
- a collection of keys to request to be loaded
-
peek
public Object peek(Object oKey)
Checks for a valid entry corresponding to the specified key in the cache, and returns the corresponding value if it is. If it is not in the cache, returns null, and does not attempt to load the value using its cache loader.- Parameters:
oKey
- the key to "peek" into the cache for- Returns:
- the value corresponding to the specified key
-
peekAll
public Map peekAll(Collection colKeys)
Checks for a valid entry corresponding to each specified key in the cache, and places the corresponding value in the returned map if it is. For each key that is not in the cache, no entry is placed into the returned map. The cache does not attempt to load any values using its cache loader.The result of this method is defined to be semantically the same as the following implementation, without regards to threading issues:
Map map = new HashMap(); for (Iterator iter = colKeys.iterator(); iter.hasNext(); ) { Object oKey = iter.next(); Object oValue = peek(oKey); if (oValue != null || containsKey(oKey)) { map.put(oKey, oValue); } } return map;
- Parameters:
colKeys
- a collection of keys to "peek" into the cache for- Returns:
- a Map of keys that were found in the cache and their values
-
getCacheStore
protected CacheStore getCacheStore()
Determine the store used by this LocalCache, if any.- Returns:
- the CacheStore used by this LocalCache, or null if none
-
getKeyMask
protected LocalCache.KeyMask getKeyMask()
ThreadLocal: Get the current key mask for the current thread.- Returns:
- the current key mask
-
setKeyMask
protected void setKeyMask(LocalCache.KeyMask mask)
ThreadLocal: Set the key mask for the current thread.- Parameters:
mask
- the new key mask, or null to clear the mask
-
removeEvicted
protected boolean removeEvicted(OldCache.Entry entry)
Remove an entry (if it is eligible for eviction) because it has expired.- Overrides:
removeEvicted
in classOldCache
- Parameters:
entry
- the expired cache entry- Returns:
- true iff the entry was removed
-
instantiateMapEvent
protected MapEvent instantiateMapEvent(int nId, Object oKey, Object oValueOld, Object oValueNew)
Factory pattern: instantiate a new CacheEvent corresponding to the specified parameters.- Overrides:
instantiateMapEvent
in classOldCache
- Parameters:
nId
- the event IdoKey
- the keyoValueOld
- the old valueoValueNew
- the new value- Returns:
- a new instance of the CacheEvent class (or a subclass thereof)
-
instantiateInternalListener
protected MapListener instantiateInternalListener()
Factory pattern: Instantiate an internal MapListener to listen to this cache and report changes to the CacheStore.- Returns:
- a new MapListener instance
-
instantiateEntry
protected SafeHashMap.Entry instantiateEntry()
Factory method. This method exists to allow the OldCache class to be easily inherited from by allowing the Entry class to be easily sub-classed.- Overrides:
instantiateEntry
in classOldCache
- Returns:
- an instance of Entry that holds the passed cache value
-
-