Oracle® Coherence Java API Reference
v3.5

E14977-01

com.tangosol.net.cache
Class NearCache

java.lang.Object
  extended by com.tangosol.util.Base
      extended by com.tangosol.net.cache.CachingMap
          extended by com.tangosol.net.cache.NearCache
All Implemented Interfaces:
CacheMap, NamedCache, ConcurrentMap, InvocableMap, ObservableMap, QueryMap, java.util.Map
Direct Known Subclasses:
VersionedNearCache

public class NearCache
extends CachingMap
implements NamedCache

A "near cache" is a CachingMap whose front map is a size-limited and/or auto-expiring local cache, and whose back map is a distributed cache. (A CachingMap is a map that has a "front" map and a "back" map; the front map is assumed to be low latency but incomplete, and the back map is assumed to be complete but high latency.)

Author:
ag/cp 2002.10.20, gg 2003.10.16
See Also:
Invalidation strategies

Nested Class Summary
protected  class NearCache.BackServiceListener
          MemberListener for the back cache's service.
 
Nested classes/interfaces inherited from class com.tangosol.net.cache.CachingMap
CachingMap.BackMapListener, CachingMap.FrontMapListener
 
Nested classes/interfaces inherited from interface com.tangosol.util.QueryMap
QueryMap.Entry
 
Nested classes/interfaces inherited from interface com.tangosol.util.InvocableMap
InvocableMap.Entry, InvocableMap.EntryAggregator, InvocableMap.EntryProcessor, InvocableMap.ParallelAwareAggregator
 
Field Summary
protected  MemberListener m_listenerBackService
          The back cache service MemberListener.
protected  java.lang.String m_sName
          The cache name.
 
Fields inherited from class com.tangosol.net.cache.CachingMap
LISTEN_ALL, LISTEN_AUTO, LISTEN_NONE, LISTEN_PRESENT, m_nStrategyCurrent, m_nStrategyTarget
 
Fields inherited from interface com.tangosol.net.cache.CacheMap
EXPIRY_DEFAULT, EXPIRY_NEVER
 
Fields inherited from interface com.tangosol.util.ConcurrentMap
LOCK_ALL
 
Constructor Summary
NearCache(java.util.Map mapFront, NamedCache mapBack)
          Construct a NearCache, using a mapBack NamedCache as the complete (back) storage and mapFront Map as a near (front) storage using the CachingMap.LISTEN_AUTO invalidation strategy.
NearCache(java.util.Map mapFront, NamedCache mapBack, int nStrategy)
          Construct a NearCache, using a mapBack NamedCache as the complete (back) storage and mapFront Map as a near (front) storage.
 
Method Summary
 void addIndex(ValueExtractor extractor, boolean fOrdered, java.util.Comparator comparator)
          Add an index to this QueryMap.
 void addMapListener(MapListener listener)
          Add a standard map listener.
 void addMapListener(MapListener listener, Filter filter, boolean fLite)
          Add a map listener that receives events based on a filter evaluation.
 void addMapListener(MapListener listener, java.lang.Object oKey, boolean fLite)
          Add a map listener for a specific key.
 java.lang.Object aggregate(java.util.Collection collKeys, InvocableMap.EntryAggregator agent)
          Perform an aggregating operation against the entries specified by the passed keys.
 java.lang.Object aggregate(Filter filter, InvocableMap.EntryAggregator agent)
          Perform an aggregating operation against the set of entries that are selected by the given Filter.
 void destroy()
          Release and destroy this instance of NamedCache.
 java.util.Set entrySet(Filter filter)
          Return a set view of the entries contained in this map that satisfy the criteria expressed by the filter.
 java.util.Set entrySet(Filter filter, java.util.Comparator comparator)
          Return a set view of the entries contained in this map that satisfy the criteria expressed by the filter.
 NamedCache getBackCache()
          Obtain the NamedCache object that sits behind this NearCache.
 java.lang.String getCacheName()
          Return the cache name.
 CacheService getCacheService()
          Return the CacheService that this NamedCache is a part of.
 java.lang.Object invoke(java.lang.Object oKey, InvocableMap.EntryProcessor agent)
          Invoke the passed EntryProcessor against the Entry specified by the passed key, returning the result of the invocation.
 java.util.Map invokeAll(java.util.Collection collKeys, InvocableMap.EntryProcessor agent)
          Invoke the passed EntryProcessor against the entries specified by the passed keys, returning the result of the invocation for each.
 java.util.Map invokeAll(Filter filter, InvocableMap.EntryProcessor agent)
          Invoke the passed EntryProcessor against the set of entries that are selected by the given Filter, returning the result of the invocation for each.
 boolean isActive()
          Specifies whether or not the NamedCache is active.
 java.util.Set keySet(Filter filter)
          Return a set view of the keys contained in this map for entries that satisfy the criteria expressed by the filter.
 boolean lock(java.lang.Object oKey)
          Attempt to lock the specified item and return immediately.
 boolean lock(java.lang.Object oKey, long lMillis)
          Attempt to lock the specified item within the specified period of time.
 java.lang.Object put(java.lang.Object oKey, java.lang.Object oValue, long cMillis)
          Associates the specified value with the specified key in this cache and allows to specify an expiry for the cache entry.
protected  MemberListener registerBackServiceListener()
          Instantiate and register a MemberListener with the back caches's service.
 void release()
          Release resources associated with this instance of NamedCache.
 void removeIndex(ValueExtractor extractor)
          Remove an index from this QueryMap.
 void removeMapListener(MapListener listener)
          Remove a standard map listener.
 void removeMapListener(MapListener listener, Filter filter)
          Remove a map listener that previously signed up for events based on a filter evaluation.
 void removeMapListener(MapListener listener, java.lang.Object oKey)
          Remove a map listener that previously signed up for events about a specific key.
 boolean unlock(java.lang.Object oKey)
          Unlock the specified item.
protected  void unregisterBackServiceListener()
          Unregister back caches's service member listener.
 
Methods inherited from class com.tangosol.net.cache.CachingMap
clear, containsKey, containsValue, ensureInvalidationStrategy, entrySet, get, getAll, getBackMap, getCacheStatistics, getControlMap, getFrontMap, getInvalidationHits, getInvalidationMisses, getInvalidationStrategy, getTotalRegisterListener, instantiateBackMapListener, instantiateFrontMapListener, invalidateFront, isCoherent, isEmpty, keySet, put, put, putAll, registerFrontListener, registerListener, registerListener, remove, resetInvalidationStrategy, size, toString, unregisterFrontListener, unregisterListener, unregisterListener, validate, values
 
Methods inherited from interface com.tangosol.net.cache.CacheMap
getAll, put
 
Methods inherited from interface com.tangosol.util.ConcurrentMap
clear, containsKey, containsValue, get, isEmpty, put, putAll, remove, size
 

Field Detail

m_listenerBackService

protected MemberListener m_listenerBackService
The back cache service MemberListener.


m_sName

protected java.lang.String m_sName
The cache name.

Constructor Detail

NearCache

public NearCache(java.util.Map mapFront,
                 NamedCache mapBack)
Construct a NearCache, using a mapBack NamedCache as the complete (back) storage and mapFront Map as a near (front) storage using the CachingMap.LISTEN_AUTO invalidation strategy.

Parameters:
mapFront - Map to put in front of the back cache
mapBack - NamedCache to put behind the front cache

NearCache

public NearCache(java.util.Map mapFront,
                 NamedCache mapBack,
                 int nStrategy)
Construct a NearCache, using a mapBack NamedCache as the complete (back) storage and mapFront Map as a near (front) storage.

Parameters:
mapFront - Map to put in front of the back cache
mapBack - NamedCache to put behind the front cache
nStrategy - specifies the strategy used for the front map invalidation; valid values are:
LISTEN_NONE, LISTEN_PRESENT, LISTEN_ALL, LISTEN_AUTO
Since:
Coherence 2.3
Method Detail

getBackCache

public NamedCache getBackCache()
Obtain the NamedCache object that sits behind this NearCache.

Returns:
the NamedCache object, which is the back map of this NearCache
Throws:
java.lang.IllegalStateException - if this NamedCache has been released

getCacheName

public java.lang.String getCacheName()
Return the cache name.

Specified by:
getCacheName in interface NamedCache
Returns:
the cache name

getCacheService

public CacheService getCacheService()
Return the CacheService that this NamedCache is a part of.

Specified by:
getCacheService in interface NamedCache
Returns:
the CacheService

isActive

public boolean isActive()
Specifies whether or not the NamedCache is active.

Specified by:
isActive in interface NamedCache
Returns:
true if the NamedCache is active; false otherwise

release

public void release()
Release resources associated with this instance of NamedCache.

Specified by:
release in interface NamedCache
Overrides:
release in class CachingMap
See Also:
CacheService.releaseCache(NamedCache)

destroy

public void destroy()
Release and destroy this instance of NamedCache.

Specified by:
destroy in interface NamedCache
See Also:
CacheService.destroyCache(NamedCache)

addMapListener

public void addMapListener(MapListener listener)
Add a standard map listener. Expensive: Listening always occurs on the back cache.

Specified by:
addMapListener in interface ObservableMap
Parameters:
listener - the MapEvent listener to add

removeMapListener

public void removeMapListener(MapListener listener)
Remove a standard map listener.

Specified by:
removeMapListener in interface ObservableMap
Parameters:
listener - the MapEvent listener to remove

addMapListener

public void addMapListener(MapListener listener,
                           java.lang.Object oKey,
                           boolean fLite)
Add a map listener for a specific key. Expensive: Listening always occurs on the back cache.

Specified by:
addMapListener in interface ObservableMap
Parameters:
listener - the listener to add
oKey - the key that identifies the entry for which to raise events
fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
Since:
Coherence 2.3

removeMapListener

public void removeMapListener(MapListener listener,
                              java.lang.Object oKey)
Remove a map listener that previously signed up for events about a specific key.

Specified by:
removeMapListener in interface ObservableMap
Parameters:
listener - the listener to remove
oKey - the key that identifies the entry for which to raise events
Since:
Coherence 2.3

addMapListener

public void addMapListener(MapListener listener,
                           Filter filter,
                           boolean fLite)
Add a map listener that receives events based on a filter evaluation. Expensive: Listening always occurs on the back cache.

Specified by:
addMapListener in interface ObservableMap
Parameters:
listener - the listener to add
filter - a filter that will be passed MapEvent objects to select from; a MapEvent will be delivered to the listener only if the filter evaluates to true for that MapEvent; null is equivalent to a filter that alway returns true
fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
Since:
Coherence 2.3

removeMapListener

public void removeMapListener(MapListener listener,
                              Filter filter)
Remove a map listener that previously signed up for events based on a filter evaluation.

Specified by:
removeMapListener in interface ObservableMap
Parameters:
listener - the listener to remove
filter - a filter used to evaluate events
Since:
Coherence 2.3

put

public java.lang.Object put(java.lang.Object oKey,
                            java.lang.Object oValue,
                            long cMillis)
Associates the specified value with the specified key in this cache and allows to specify an expiry for the cache entry. If the cache previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface CacheMap
Specified by:
put in interface NamedCache
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
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
Throws:
java.lang.UnsupportedOperationException - if the requested expiry is a positive value and either the front map or the back map implementations do not support the expiration functionality
See Also:
CacheMap.put(Object oKey, Object oValue, long cMillis)

lock

public boolean lock(java.lang.Object oKey)
Attempt to lock the specified item and return immediately. Expensive: Locking always occurs on the back cache.

Specified by:
lock in interface ConcurrentMap
Parameters:
oKey - key being locked
Returns:
true if the item was successfully locked; false otherwise

lock

public boolean lock(java.lang.Object oKey,
                    long lMillis)
Attempt to lock the specified item within the specified period of time. Expensive: Locking always occurs on the back cache.

Specified by:
lock in interface ConcurrentMap
Parameters:
oKey - key being locked
lMillis - the number of milliseconds to continue trying to obtain a lock; pass zero to return immediately; pass -1 to block the calling thread until the lock could be obtained
Returns:
true if the item was successfully locked within the specified time; false otherwise

unlock

public boolean unlock(java.lang.Object oKey)
Unlock the specified item.

Specified by:
unlock in interface ConcurrentMap
Parameters:
oKey - key being unlocked
Returns:
true if the item was successfully unlocked; false otherwise

keySet

public java.util.Set keySet(Filter filter)
Return a set view of the keys contained in this map for entries that satisfy the criteria expressed by the filter.

Unlike the Map.keySet() method, the set returned by this method may not be backed by the map, so changes to the set may not reflected in the map, and vice-versa.

Note: When using the Coherence Enterprise Edition or Grid Edition, the Partitioned Cache implements the QueryMap interface using the Parallel Query feature. When using Coherence Standard Edition, the Parallel Query feature is not available, resulting in lower performance for most queries, and particularly when querying large data sets.

The operation always executes against the back cache.

Specified by:
keySet in interface QueryMap
Parameters:
filter - the Filter object representing the criteria that the entries of this map should satisfy
Returns:
a set of keys for entries that satisfy the specified criteria

entrySet

public java.util.Set entrySet(Filter filter)
Return a set view of the entries contained in this map that satisfy the criteria expressed by the filter. Each element in the returned set is a Map.Entry.

Unlike the Map.entrySet() method, the set returned by this method may not be backed by the map, so changes to the set may not be reflected in the map, and vice-versa.

Note: When using the Coherence Enterprise Edition or Grid Edition, the Partitioned Cache implements the QueryMap interface using the Parallel Query feature. When using Coherence Standard Edition, the Parallel Query feature is not available, resulting in lower performance for most queries, and particularly when querying large data sets.

The operation always executes against the back cache.

Specified by:
entrySet in interface QueryMap
Parameters:
filter - the Filter object representing the criteria that the entries of this map should satisfy
Returns:
a set of entries that satisfy the specified criteria

entrySet

public java.util.Set entrySet(Filter filter,
                              java.util.Comparator comparator)
Return a set view of the entries contained in this map that satisfy the criteria expressed by the filter. Each element in the returned set is a Map.Entry. It is further guaranteed that its iterator will traverse the set in such a way that the entry values come up in ascending order, sorted by the specified Comparator or according to the natural ordering (see Comparable).

Unlike the Map.entrySet() method, the set returned by this method may not be backed by the map, so changes to the set may not be reflected in the map, and vice-versa.

Note: When using the Coherence Enterprise Edition or Grid Edition, the Partitioned Cache implements the QueryMap interface using the Parallel Query feature. When using Coherence Standard Edition, the Parallel Query feature is not available, resulting in lower performance for most queries, and particularly when querying large data sets.

The operation always executes against the back cache.

Specified by:
entrySet in interface QueryMap
Parameters:
filter - the Filter object representing the criteria that the entries of this map should satisfy
comparator - the Comparator object which imposes an ordering on entries in the resulting set; or null if the entries' values natural ordering should be used
Returns:
a set of entries that satisfy the specified criteria
See Also:
ChainedComparator

addIndex

public void addIndex(ValueExtractor extractor,
                     boolean fOrdered,
                     java.util.Comparator comparator)
Add an index to this QueryMap. This allows to correlate values stored in this indexed Map (or attributes of those values) to the corresponding keys in the indexed Map and increase the performance of keySet and entrySet methods.

This method is only intended as a hint to the cache implementation, and as such it may be ignored by the cache if indexes are not supported or if the desired index (or a similar index) already exists. It is expected that an application will call this method to suggest an index even if the index may already exist, just so that the application is certain that index has been suggested. For example in a distributed environment, each server will likely suggest the same set of indexes when it starts, and there is no downside to the application blindly requesting those indexes regardless of whether another server has already requested the same indexes.

Note: Indexes are a feature of Coherence Enterprise Edition and Coherence Grid Edition. This method will have no effect when using Coherence Standard Edition.

The operation always executes against the back cache.

Specified by:
addIndex in interface QueryMap
Parameters:
extractor - the ValueExtractor object that is used to extract an indexable Object from a value stored in the indexed Map. Must not be null.
fOrdered - true iff the contents of the indexed information should be ordered; false otherwise
comparator - the Comparator object which imposes an ordering on entries in the indexed map; or null if the entries' values natural ordering should be used
See Also:
ReflectionExtractor, ChainedComparator

removeIndex

public void removeIndex(ValueExtractor extractor)
Remove an index from this QueryMap.

The operation always executes against the back cache.

Specified by:
removeIndex in interface QueryMap
Parameters:
extractor - the ValueExtractor object that is used to extract an indexable Object from a value stored in the Map.

invoke

public java.lang.Object invoke(java.lang.Object oKey,
                               InvocableMap.EntryProcessor agent)
Invoke the passed EntryProcessor against the Entry specified by the passed key, returning the result of the invocation.

The operation always executes against the back cache.

Specified by:
invoke in interface InvocableMap
Parameters:
oKey - the key to process; it is not required to exist within the Map
agent - the EntryProcessor to use to process the specified key
Returns:
the result of the invocation as returned from the EntryProcessor

invokeAll

public java.util.Map invokeAll(java.util.Collection collKeys,
                               InvocableMap.EntryProcessor agent)
Invoke the passed EntryProcessor against the entries specified by the passed keys, returning the result of the invocation for each.

The operation always executes against the back cache.

Specified by:
invokeAll in interface InvocableMap
Parameters:
collKeys - the keys to process; these keys are not required to exist within the Map
agent - the EntryProcessor to use to process the specified keys
Returns:
a Map containing the results of invoking the EntryProcessor against each of the specified keys

invokeAll

public java.util.Map invokeAll(Filter filter,
                               InvocableMap.EntryProcessor agent)
Invoke the passed EntryProcessor against the set of entries that are selected by the given Filter, returning the result of the invocation for each.

Unless specified otherwise, InvocableMap implementations will perform this operation in two steps: (1) use the filter to retrieve a matching entry set; (2) apply the agent to every filtered entry. This algorithm assumes that the agent's processing does not affect the result of the specified filter evaluation, since the filtering and processing could be performed in parallel on different threads. If this assumption does not hold, the processor logic has to be idempotent, or at least re-evaluate the filter. This could be easily accomplished by wrapping the processor with the ConditionalProcessor.

The operation always executes against the back cache.

Specified by:
invokeAll in interface InvocableMap
Parameters:
filter - a Filter that results in the set of keys to be processed
agent - the EntryProcessor to use to process the specified keys
Returns:
a Map containing the results of invoking the EntryProcessor against the keys that are selected by the given Filter

aggregate

public java.lang.Object aggregate(java.util.Collection collKeys,
                                  InvocableMap.EntryAggregator agent)
Perform an aggregating operation against the entries specified by the passed keys.

The operation always executes against the back cache.

Specified by:
aggregate in interface InvocableMap
Parameters:
collKeys - the Collection of keys that specify the entries within this Map to aggregate across
agent - the EntryAggregator that is used to aggregate across the specified entries of this Map
Returns:
the result of the aggregation

aggregate

public java.lang.Object aggregate(Filter filter,
                                  InvocableMap.EntryAggregator agent)
Perform an aggregating operation against the set of entries that are selected by the given Filter.

The operation always executes against the back cache.

Specified by:
aggregate in interface InvocableMap
Parameters:
filter - the Filter that is used to select entries within this Map to aggregate across
agent - the EntryAggregator that is used to aggregate across the selected entries of this Map
Returns:
the result of the aggregation

registerBackServiceListener

protected MemberListener registerBackServiceListener()
Instantiate and register a MemberListener with the back caches's service.

The primary goal of that listener is invalidation of the front map in case of the service [automatic] restart.

Returns:
the instantiated and registered MemberListener object

unregisterBackServiceListener

protected void unregisterBackServiceListener()
Unregister back caches's service member listener.


Oracle® Coherence Java API Reference
v3.5

E14977-01

Copyright © 2000, 2009, Oracle. All rights reserved.