CoherenceTM v3.3
Copyright© 2000-2007 by Oracle Corporation

com.tangosol.net.cache
Class LocalCache

java.lang.Object
  extended by java.util.AbstractMap
      extended by com.tangosol.util.SafeHashMap
          extended by com.tangosol.net.cache.OldCache
              extended by com.tangosol.net.cache.LocalCache
All Implemented Interfaces:
CacheMap, 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

Nested Class Summary
 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.KeySet, OldCache.UnitCalculator, OldCache.ValuesCollection
 
Field Summary
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, m_calculator, m_cCurUnits, m_cExpiryDelay, m_cFlushDelay, m_cMaxUnits, m_cPruneUnits, m_listenerSupport, m_lNextFlush, m_nCalculatorType, m_nEvictionType, 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, PRIME_MODULO, RESIZING
 
Fields inherited from interface com.tangosol.net.cache.CacheMap
EXPIRY_DEFAULT, EXPIRY_NEVER
 
Constructor Summary
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
 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  void removeExpired(OldCache.Entry entry, boolean fRemoveInternal)
          Remove an entry 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, checkSize, configureEviction, configureUnitCalculator, containsKey, deferFlush, dispatchEvent, evict, evict, evictAll, getCacheHits, getCacheMisses, getCacheStatistics, getEntryInternal, getEvictionPolicy, getEvictionType, getExpiryDelay, getFlushDelay, getFlushTime, getHighUnits, getHitProbability, getLowUnits, getMapListenerSupport, getUnitCalculator, getUnitCalculatorType, getUnits, hasListeners, instantiateEntrySet, instantiateKeySet, instantiateValuesCollection, prune, put, put, removeMapListener, removeMapListener, removeMapListener, resetHitStatistics, scheduleFlush, setEvictionPolicy, setEvictionType, setExpiryDelay, setFlushDelay, setFlushTime, setHighUnits, setLowUnits, setUnitCalculator, setUnitCalculatorType, size, toString
 
Methods inherited from class com.tangosol.util.SafeHashMap
clone, cloneEntryList, entrySet, getBucketIndex, getStableBucketArray, grow, isActiveIterator, isEmpty, iteratorActivated, iteratorDeactivated, keySet, removeEntryInternal, values
 
Methods inherited from class java.util.AbstractMap
containsValue, equals, hashCode, putAll
 
Methods inherited from interface com.tangosol.net.cache.CacheMap
put, put
 
Methods inherited from interface com.tangosol.util.ObservableMap
addMapListener, addMapListener, addMapListener, removeMapListener, removeMapListener, removeMapListener
 
Methods inherited from interface java.util.Map
containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, putAll, size, values
 

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 cache
cExpiryMillis - 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 cache
cExpiryMillis - the number of milliseconds that each cache entry lives before being automatically expired
loader - the CacheLoader or CacheStore to use
Method Detail

clear

public void clear()
Removes all mappings from this map.

Specified by:
clear in interface Map
Overrides:
clear in class OldCache

remove

public Object remove(Object oKey)
Removes the mapping for this key from this map if present.

Specified by:
remove in interface Map
Overrides:
remove in class OldCache
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.

Specified by:
get in interface Map
Overrides:
get in class OldCache
Parameters:
oKey - the key to the desired cached item
Returns:
the value corresponding to the specified key, otherwise null

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.

Overrides:
getEntry in class OldCache
Parameters:
oKey - the key to the desired cached Entry
Returns:
the Entry corresponding to the specified key, otherwise null

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;
 

Specified by:
getAll in interface CacheMap
Parameters:
colKeys - a collection of keys that may be in the named cache
Returns:
a Map of keys to values for the specified keys passed in col

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

removeExpired

protected void removeExpired(OldCache.Entry entry,
                             boolean fRemoveInternal)
Remove an entry because it has expired.

Overrides:
removeExpired in class OldCache
Parameters:
entry - the expired cache entry
fRemoveInternal - true if the cache entry still needs to be removed from the cache

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 class OldCache
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 class OldCache
Returns:
an instance of Entry that holds the passed cache value

CoherenceTM v3.3
Copyright© 2000-2007 by Oracle Corporation