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

com.tangosol.net.cache
Class SerializationCache

java.lang.Object
  extended by com.tangosol.util.Base
      extended by com.tangosol.util.AbstractKeyBasedMap
          extended by com.tangosol.util.AbstractKeySetBasedMap
              extended by com.tangosol.net.cache.SerializationMap
                  extended by com.tangosol.net.cache.AbstractSerializationCache
                      extended by com.tangosol.net.cache.SerializationCache
All Implemented Interfaces:
CacheMap, ObservableMap, Map

public class SerializationCache
extends AbstractSerializationCache
implements CacheMap

A version of SerializationMap that implements an LRU policy.

This implementation is partially thread safe. It assumes that multiple threads will not be accessing the same keys at the same time, nor would any other thread be accessing this cache while a clear() operation were going on, for example. In other words, this implementation assumes that access to this cache is either single-threaded or gated through an object like WrapperConcurrentMap.

The primary reason why SerializationCache is a sub-class of SerializationMap instead of combining their functionality is that SerializationMap represents a passive store, and thus does not implement the ObservableMap interface, while SerializationCache represents an active store, and thus clients of it would have to always handle potential events, even if it were not size-limited.

Since:
Coherence 2.2
Author:
cp 2003.05.28

Nested Class Summary
protected  class SerializationCache.EntryAttributes
          A class that holds on to the expiry time and touch order for an entry.
 
Nested classes/interfaces inherited from class com.tangosol.util.AbstractKeySetBasedMap
AbstractKeySetBasedMap.EntrySet, AbstractKeySetBasedMap.KeyIterator, AbstractKeySetBasedMap.KeySet, AbstractKeySetBasedMap.ValuesCollection
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry
 
Field Summary
 
Fields inherited from interface com.tangosol.net.cache.CacheMap
EXPIRY_DEFAULT, EXPIRY_NEVER
 
Constructor Summary
SerializationCache(BinaryStore store, int cMax)
          Construct a SerializationCache on top of a BinaryStore.
SerializationCache(BinaryStore store, int cMax, boolean fBinaryMap)
          Construct a SerializationCache on top of a BinaryStore.
SerializationCache(BinaryStore store, int cMax, ClassLoader loader)
          Construct a SerializationCache on top of a BinaryStore.
 
Method Summary
protected  void checkExpiry()
          Make sure that the cache does not contain expired items.
protected  void checkSize()
          Make sure the size of the cache isn't too big.
 void clear()
          Clear all key/value mappings.
 boolean containsKey(Object oKey)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(Object oValue)
          Returns true if this Map maps one or more keys to the specified value.
 void evict()
          Flush items that have expired.
 void evict(Object oKey)
          Evict the specified item from the cache.
 void flush()
          Deprecated. use evict()
 Object get(Object oKey)
          Returns the value to which this map maps the specified key.
protected  String getDescription()
          Assemble a human-readable description.
protected  LongArray getExpiryArray()
          Get the Expiry data structure.
 int getExpiryDelay()
          Determine the default "time to live" for cache entries.
protected  LongArray getLruArray()
          Get the LRU data structure.
 int getMaximumSize()
          Determine the maximum number of entries that the cache will hold.
protected  long getTouchCounter()
          Determine the next value from the touch counter.
 SerializationCache.EntryAttributes instantiateEntryAttributes(SerializationCache.EntryAttributes attrOrig, long ldtExpires, long nTouch)
          Factory pattern: Construct an attribute holder for an entry.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 Object put(Object oKey, Object oValue)
          Associates the specified value with the specified key in this map.
 Object put(Object oKey, Object oValue, long cMillis)
          Associates the specified value with the specified key in this cache.
 void putAll(Map map)
          Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling AbstractKeyBasedMap.put(java.lang.Object, java.lang.Object) on this map once for each mapping in the passed map. The behavior of this operation is unspecified if the passed map is modified while the operation is in progress.
protected  void registerKey(Object oKey)
          Register a new key for the SerializationMap.
protected  void registerKey(Object oKey, int cMillis)
          Register a new key for the SerializationMap.
 Object remove(Object oKey)
          Removes the mapping for this key from this map if present. Expensive: updates both the underlying cache and the local cache.
protected  boolean removeBlind(Object oKey)
          Removes the mapping for this key from this map if present. This method exists to allow sub-classes to optmiize remove functionalitly for situations in which the original value is not required.
protected  void setBinaryStore(BinaryStore store)
          Configures the BinaryStore that this map will use for its storage.
 void setExpiryDelay(int cMillis)
          Specify the "time to live" for cache entries.
 void setMaximumSize(int cMax)
          Change the maximum number of entries that the cache will hold.
 int size()
          Returns the number of key-value mappings in this map.
 String toString()
          Returns a string representation of this Map. The string representation consists of a list of key-value mappings in the order returned by the Map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).
protected  void touch(Object oKey)
          Touch an object to pop it to the top of the LRU
protected  void unregisterKey(Object oKey)
          Unregister a key from the SerializationMap.
 
Methods inherited from class com.tangosol.net.cache.AbstractSerializationCache
addMapListener, addMapListener, addMapListener, dispatchEvent, dispatchPendingEvent, getMapListenerSupport, hasListeners, removeMapListener, removeMapListener, removeMapListener
 
Methods inherited from class com.tangosol.net.cache.SerializationMap
eraseStore, fromBinary, getBinaryStore, getCacheStatistics, getClassLoader, getInternalKeySet, getKeyMap, isBinaryMap, setBinaryMap, setClassLoader, toBinary
 
Methods inherited from class com.tangosol.util.AbstractKeySetBasedMap
instantiateEntrySet, instantiateKeyIterator, instantiateKeySet, instantiateValues, isInternalKeySetIteratorMutable, iterateKeys
 
Methods inherited from class com.tangosol.util.AbstractKeyBasedMap
clone, entrySet, equals, getAll, hashCode, keySet, values
 
Methods inherited from interface com.tangosol.net.cache.CacheMap
getAll
 
Methods inherited from interface com.tangosol.util.ObservableMap
addMapListener, addMapListener, addMapListener, removeMapListener, removeMapListener, removeMapListener
 
Methods inherited from interface java.util.Map
entrySet, equals, hashCode, keySet, values
 

Constructor Detail

SerializationCache

public SerializationCache(BinaryStore store,
                          int cMax)
Construct a SerializationCache on top of a BinaryStore.

Parameters:
store - the BinaryStore to use to write the serialized objects to
cMax - the maximum number of items to store in the binary store

SerializationCache

public SerializationCache(BinaryStore store,
                          int cMax,
                          ClassLoader loader)
Construct a SerializationCache on top of a BinaryStore.

Parameters:
store - the BinaryStore to use to write the serialized objects to
cMax - the maximum number of items to store in the binary store
loader - the ClassLoader to use for deserialization

SerializationCache

public SerializationCache(BinaryStore store,
                          int cMax,
                          boolean fBinaryMap)
Construct a SerializationCache on top of a BinaryStore.

Parameters:
store - the BinaryStore to use to write the serialized objects to
cMax - the maximum number of items to store in the binary store
fBinaryMap - true indicates that this map will only manage binary keys and values
Since:
Coherence 2.4
Method Detail

clear

public void clear()
Clear all key/value mappings.

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

containsKey

public boolean containsKey(Object oKey)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface Map
Overrides:
containsKey in class AbstractKeySetBasedMap
Returns:
true if this map contains a mapping for the specified key, false otherwise.

containsValue

public boolean containsValue(Object oValue)
Returns true if this Map maps one or more keys to the specified value.

Specified by:
containsValue in interface Map
Overrides:
containsValue in class SerializationMap
Returns:
true if this Map maps one or more keys to the specified value, false otherwise

get

public Object get(Object oKey)
Returns the value to which this map maps the specified key.

Specified by:
get in interface Map
Overrides:
get in class SerializationMap
Parameters:
oKey - the key object
Returns:
the value to which this map maps the specified key, or null if the map contains no mapping for this key

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface Map
Overrides:
isEmpty in class AbstractKeySetBasedMap
Returns:
true if this map contains no key-value mappings

put

public Object put(Object oKey,
                  Object oValue)
Associates the specified value with the specified key in this map.

Specified by:
put in interface CacheMap
Specified by:
put in interface Map
Overrides:
put in class AbstractSerializationCache
Parameters:
oKey - key with which the specified value is to be associated
oValue - value to be associated with the specified key
Returns:
previous value associated with specified key, or null if there was no mapping for key

putAll

public void putAll(Map map)
Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling AbstractKeyBasedMap.put(java.lang.Object, java.lang.Object) on this map once for each mapping in the passed map. The behavior of this operation is unspecified if the passed map is modified while the operation is in progress.

Specified by:
putAll in interface Map
Overrides:
putAll in class AbstractSerializationCache
Parameters:
map - the Map containing the key/value pairings to put into this Map

remove

public Object remove(Object oKey)
Removes the mapping for this key from this map if present. Expensive: updates both the underlying cache and the local cache.

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

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface Map
Overrides:
size in class AbstractKeySetBasedMap
Returns:
the number of key-value mappings in this map

removeBlind

protected boolean removeBlind(Object oKey)
Removes the mapping for this key from this map if present. This method exists to allow sub-classes to optmiize remove functionalitly for situations in which the original value is not required.

Overrides:
removeBlind in class AbstractSerializationCache
Parameters:
oKey - key whose mapping is to be removed from the map
Returns:
true iff the Map changed as the result of this operation

put

public Object put(Object oKey,
                  Object oValue,
                  long cMillis)
Associates the specified value with the specified key in this cache. If the cache previously contained a mapping for this key, the old value is replaced. This variation of the CacheMap.put(Object oKey, Object oValue) method allows the caller to specify an expiry (or "time to live") for the cache entry.

Specified by:
put in interface CacheMap
Parameters:
oKey - key with which the specified value is to be associated
oValue - value to be associated with the specified key
cMillis - the number of milliseconds until the cache entry will expire, also referred to as the entry's "time to live"; pass CacheMap.EXPIRY_DEFAULT to use the cache's default time-to-live setting; pass CacheMap.EXPIRY_NEVER to indicate that the cache entry should never expire; this milliseconds value is not a date/time value, such as is returned from System.currentTimeMillis()
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

flush

public void flush()
Deprecated. use evict()

Flush items that have expired.


evict

public void evict()
Flush items that have expired.

Specified by:
evict in class AbstractSerializationCache
Since:
Coherence 3.2

evict

public void evict(Object oKey)
Evict the specified item from the cache.

Parameters:
oKey - the key of the entry to evict

toString

public String toString()
Returns a string representation of this Map. The string representation consists of a list of key-value mappings in the order returned by the Map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).

Overrides:
toString in class SerializationMap
Returns:
a String representation of this Map

setBinaryStore

protected void setBinaryStore(BinaryStore store)
Configures the BinaryStore that this map will use for its storage.

Overrides:
setBinaryStore in class SerializationMap
Parameters:
store - the BinaryStore to use

getMaximumSize

public int getMaximumSize()
Determine the maximum number of entries that the cache will hold.

Returns:
the maximum number of entries that the cache will hold

setMaximumSize

public void setMaximumSize(int cMax)
Change the maximum number of entries that the cache will hold.

Parameters:
cMax - the new maximum number of entries that the cache will hold

getExpiryDelay

public int getExpiryDelay()
Determine the default "time to live" for cache entries.

Returns:
the number of milliseconds that a cache entry value will live, by default, or zero if cache entries are not automatically expired by default

setExpiryDelay

public void setExpiryDelay(int cMillis)
Specify the "time to live" for cache entries. This does not affect the already-scheduled expiry of existing entries.

Parameters:
cMillis - the number of milliseconds that cache entries will live, or zero to disable automatic expiry

getTouchCounter

protected long getTouchCounter()
Determine the next value from the touch counter.

Returns:
the next value from the touch counter

getLruArray

protected LongArray getLruArray()
Get the LRU data structure. The key is a touch count, the value is the Object key for the cache.

Returns:
the the LRU data structure

getExpiryArray

protected LongArray getExpiryArray()
Get the Expiry data structure. The key is a date/time value, the value is a set of Object keys for the cache that expire at that time.

Returns:
the the Expiry data structure

getDescription

protected String getDescription()
Assemble a human-readable description.

Overrides:
getDescription in class AbstractSerializationCache
Returns:
a description of this Map

registerKey

protected void registerKey(Object oKey)
Register a new key for the SerializationMap. This method maintains the internal key Set for the SerializationMap.

Overrides:
registerKey in class SerializationMap
Parameters:
oKey - the key that has been added to the map

registerKey

protected void registerKey(Object oKey,
                           int cMillis)
Register a new key for the SerializationMap. This method maintains the internal key Set for the SerializationMap.

Parameters:
oKey - the key that has been added to the map
cMillis - the TTL value (in milliseconds) for the entry

touch

protected void touch(Object oKey)
Touch an object to pop it to the top of the LRU

Parameters:
oKey - the key of the object to touch

unregisterKey

protected void unregisterKey(Object oKey)
Unregister a key from the SerializationMap. This method maintains the internal key Set for the SerializationMap.

Overrides:
unregisterKey in class SerializationMap
Parameters:
oKey - the key that has been removed from the map

checkSize

protected void checkSize()
Make sure the size of the cache isn't too big.


checkExpiry

protected void checkExpiry()
Make sure that the cache does not contain expired items.


instantiateEntryAttributes

public SerializationCache.EntryAttributes instantiateEntryAttributes(SerializationCache.EntryAttributes attrOrig,
                                                                     long ldtExpires,
                                                                     long nTouch)
Factory pattern: Construct an attribute holder for an entry.

Parameters:
attrOrig - the previous attributes for this entry, if any
ldtExpires - the date/time at which the entry expires, or zero
nTouch - the touch counter assigned to the entry

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