Class NearCache<K,V>

java.lang.Object
com.tangosol.net.cache.CachingMap<K,V>
com.tangosol.net.cache.NearCache<K,V>
All Implemented Interfaces:
ClassLoaderAware, CacheMap<K,V>, NamedCache<K,V>, NamedCollection, NamedMap<K,V>, Releasable, ConcurrentMap<K,V>, InvocableMap<K,V>, ObservableMap<K,V>, QueryMap<K,V>, AutoCloseable, Map<K,V>
Direct Known Subclasses:
VersionedNearCache

public class NearCache<K,V> extends CachingMap<K,V> implements NamedCache<K,V>, ClassLoaderAware
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:
  • Field Details

    • m_fDestroyed

      protected boolean m_fDestroyed
      True if {#link destroy()} has been called on this cache.
    • f_sName

      protected final String f_sName
      The cache name.
    • f_service

      protected final CacheService f_service
      The back cache service.
    • f_listenerBackService

      protected final MemberListener f_listenerBackService
      The back cache service MemberListener.
    • f_sServiceName

      protected final String f_sServiceName
      The back cache service name.
  • Constructor Details

    • NearCache

      public NearCache(Map<K,V> mapFront, NamedCache<K,V> 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(Map<K,V> mapFront, NamedCache<K,V> 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 Details

    • getBackCache

      public NamedCache<K,V> getBackCache()
      Obtain the NamedCache object that sits behind this NearCache.
      Returns:
      the NamedCache object, which is the back map of this NearCache
      Throws:
      IllegalStateException - if this NamedCache has been released
    • getRegistrationContext

      public String getRegistrationContext()
      Obtain the context (tier) used to register a CacheMBean for this cache.
      Returns:
      the corresponding context string
    • setRegistrationContext

      public void setRegistrationContext(String sCtx)
      Set the context (tier) used to register a CacheMBean for this cache.
      Parameters:
      sCtx - the corresponding context string
    • async

      public AsyncNamedCache<K,V> async(AsyncNamedMap.Option... options)
      Description copied from interface: NamedCache
      Return an asynchronous wrapper for this NamedCache.

      By default, the order of execution of asynchronous operation invoked on the returned AsyncNamedCache will be preserved by ensuring that all operations invoked from the same client thread are executed on the server sequentially, using the same unit-of-order. This tends to provide the best performance for fast, non-blocking operations.

      However, when invoking CPU-intensive or blocking operations, such as read- or write-through operations that access remote database or web service, for example, it may be very beneficial to allow the server to parallelize execution by passing AsyncNamedMap.OrderBy.none() configuration option to this method. Note, that in that case there are no guarantees for the order of execution.

      Specified by:
      async in interface NamedCache<K,V>
      Specified by:
      async in interface NamedMap<K,V>
      Parameters:
      options - the configuration options
      Returns:
      asynchronous wrapper for this NamedCache
    • getCacheName

      public String getCacheName()
      Return the cache name.
      Specified by:
      getCacheName in interface NamedCache<K,V>
      Returns:
      the cache name
    • getCacheService

      public CacheService getCacheService()
      Return the CacheService that this NamedCache is a part of.
      Specified by:
      getCacheService in interface NamedCache<K,V>
      Returns:
      the CacheService
    • isActive

      public boolean isActive()
      Specifies whether or not the NamedCache is active.
      Specified by:
      isActive in interface NamedMap<K,V>
      Specified by:
      isActive in interface Releasable
      Returns:
      true if the NamedCache is active; false otherwise
    • isReady

      public boolean isReady()
      Description copied from interface: NamedMap
      Returns whether this NamedMap is ready to be used.

      An example of when this method would return false would be where a partitioned cache service that owns this cache has no storage-enabled members.
      Specified by:
      isReady in interface NamedMap<K,V>
      Returns:
      return true if the NamedMap may be used otherwise returns false.
    • release

      public void release()
      Release the CachingMap. If the BackMap implements an ObservableMap calling this method is necessary to remove the BackMap listener. Any access to the CachingMap which has been released will cause IllegalStateException.
      Specified by:
      release in interface Releasable
      Overrides:
      release in class CachingMap<K,V>
    • destroy

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

      Warning: This method is used to completely destroy the specified collection across the cluster. All references in the entire cluster to this collection will be invalidated, the collection data will be cleared, and all internal resources will be released.

      Specified by:
      destroy in interface NamedCollection
    • truncate

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

      Note: the removal of entries caused by this truncate operation will not be observable. This includes any registered listeners, triggers, or interceptors. However, a CacheLifecycleEvent is raised to notify subscribers of the execution of this operation.

      Specified by:
      truncate in interface NamedMap<K,V>
    • isDestroyed

      public boolean isDestroyed()
      Specifies whether or not this NamedCollection has been destroyed. Implementations must override this method to provide the necessary information.
      Specified by:
      isDestroyed in interface NamedCollection
      Returns:
      true if the NamedCollection has been destroyed; false otherwise
    • isReleased

      public boolean isReleased()
      Specifies whether or this Releasable has been released.

      Implementations must override this method to provide the necessary information.
      Specified by:
      isReleased in interface Releasable
      Returns:
      true if the Releasable has been released; false otherwise
    • addMapListener

      public void addMapListener(MapListener<? super K,? super V> listener)
      Add a standard map listener. Expensive: Listening always occurs on the back cache.
      Specified by:
      addMapListener in interface ObservableMap<K,V>
      Parameters:
      listener - the MapEvent listener to add
    • removeMapListener

      public void removeMapListener(MapListener<? super K,? super V> listener)
      Remove a standard map listener.
      Specified by:
      removeMapListener in interface ObservableMap<K,V>
      Parameters:
      listener - the MapEvent listener to remove
    • addMapListener

      public void addMapListener(MapListener<? super K,? super V> listener, K 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<K,V>
      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<? super K,? super V> listener, K oKey)
      Remove a map listener that previously signed up for events about a specific key.
      Specified by:
      removeMapListener in interface ObservableMap<K,V>
      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<? super K,? super V> 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<K,V>
      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 always 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<? super K,? super V> listener, Filter filter)
      Remove a map listener that previously signed up for events based on a filter evaluation.
      Specified by:
      removeMapListener in interface ObservableMap<K,V>
      Parameters:
      listener - the listener to remove
      filter - a filter used to evaluate events
      Since:
      Coherence 2.3
    • put

      public V put(K oKey, V 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<K,V>
      Specified by:
      put in interface NamedCache<K,V>
      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:
      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:
    • lock

      public boolean lock(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<K,V>
      Parameters:
      oKey - key being locked
      Returns:
      true if the item was successfully locked; false otherwise
    • lock

      public boolean lock(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<K,V>
      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(Object oKey)
      Unlock the specified item.
      Specified by:
      unlock in interface ConcurrentMap<K,V>
      Parameters:
      oKey - key being unlocked
      Returns:
      true if the item was successfully unlocked; false otherwise
    • keySet

      public Set<K> 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: The Partitioned Cache implements the QueryMap interface using the Parallel Query feature.

      The operation always executes against the back cache.

      Specified by:
      keySet in interface QueryMap<K,V>
      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 Set<Map.Entry<K,V>> 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: The Partitioned Cache implements the QueryMap interface using the Parallel Query feature.

      The operation always executes against the back cache.

      Specified by:
      entrySet in interface QueryMap<K,V>
      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 Set<Map.Entry<K,V>> entrySet(Filter filter, 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: The Partitioned Cache implements the QueryMap interface using the Parallel Query feature.

      The operation always executes against the back cache.

      Specified by:
      entrySet in interface QueryMap<K,V>
      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:
    • addIndex

      public <T, E> void addIndex(ValueExtractor<? super T,? extends E> extractor, boolean fOrdered, Comparator<? super E> 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 methods that use Filters.

      The ordering maintained by this map (as determined by either the specified Comparator or the natural ordering of the indexed values) must be consistent with equals (see Comparable or Comparator for a precise definition of consistent with equals.)

      This method is only intended as a hint to the map implementation, and as such it may be ignored by the map 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.

      The operation always executes against the back cache.

      Specified by:
      addIndex in interface QueryMap<K,V>
      Type Parameters:
      T - the type of the value to extract from
      E - the type of value that will be extracted
      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:
    • removeIndex

      public <T, E> void removeIndex(ValueExtractor<? super T,? extends E> extractor)
      Remove an index from this QueryMap.

      The operation always executes against the back cache.

      Specified by:
      removeIndex in interface QueryMap<K,V>
      Type Parameters:
      T - the type of the value to extract from
      E - the type of value that will be extracted
      Parameters:
      extractor - the ValueExtractor object that is used to extract an indexable Object from a value stored in the Map.
    • computeIfAbsent

      public V computeIfAbsent(K key, Remote.Function<? super K,? extends V> mappingFunction)
      Compute the value using the given mapping function and enter it into this map (unless null), if the specified key is not already associated with a value (or is mapped to null).

      If the mapping function returns null no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.

      The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:

       
       map.computeIfAbsent(key, k -> new Value(f(k)));
       

      Or to implement a multi-value map, Map<K, Collection<V>>, supporting multiple values per key:

       
       map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
       
      Note that the previous example will not work as expected if this method is called on a distributed map, as the add method will be called on the client-side copy of the collection stored on the server.

      The operation executes against the back cache if the value is not in the front.

      Specified by:
      computeIfAbsent in interface InvocableMap<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      mappingFunction - the function to compute a value
      Returns:
      the current (existing or computed) value associated with the specified key, or null if the computed value is null
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
      Note that the previous example will not work as expected if this method is called on a distributed map, as the add method will be called on a client-side copy of the collection stored on the server.

      The operation executes against the back cache if the value is not in the front.

      Specified by:
      computeIfAbsent in interface InvocableMap<K,V>
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • invoke

      public <R> R invoke(K key, InvocableMap.EntryProcessor<K,V,R> processor)
      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<K,V>
      Type Parameters:
      R - the type of value returned by the EntryProcessor
      Parameters:
      key - the key to process; it is not required to exist within the Map
      processor - the EntryProcessor to use to process the specified key
      Returns:
      the result of the invocation as returned from the EntryProcessor
    • invokeAll

      public <R> Map<K,R> invokeAll(Collection<? extends K> collKeys, InvocableMap.EntryProcessor<K,V,R> processor)
      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<K,V>
      Type Parameters:
      R - the type of value returned by the EntryProcessor
      Parameters:
      collKeys - the keys to process; these keys are not required to exist within the Map
      processor - 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 <R> Map<K,R> invokeAll(Filter filter, InvocableMap.EntryProcessor<K,V,R> processor)
      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<K,V>
      Type Parameters:
      R - the type of value returned by the EntryProcessor
      Parameters:
      filter - a Filter that results in the set of keys to be processed
      processor - 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 <R> R aggregate(Collection<? extends K> collKeys, InvocableMap.EntryAggregator<? super K,? super V,R> aggregator)
      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<K,V>
      Type Parameters:
      R - the type of value returned by the EntryAggregator
      Parameters:
      collKeys - the Collection of keys that specify the entries within this Map to aggregate across
      aggregator - the EntryAggregator that is used to aggregate across the specified entries of this Map
      Returns:
      the result of the aggregation
    • aggregate

      public <R> R aggregate(Filter filter, InvocableMap.EntryAggregator<? super K,? super V,R> aggregator)
      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<K,V>
      Type Parameters:
      R - the type of value returned by the EntryAggregator
      Parameters:
      filter - the Filter that is used to select entries within this Map to aggregate across
      aggregator - the EntryAggregator that is used to aggregate across the selected entries of this Map
      Returns:
      the result of the aggregation
    • getOrDefault

      public V getOrDefault(Object oKey, V defaultValue)
      Specified by:
      getOrDefault in interface InvocableMap<K,V>
      Specified by:
      getOrDefault in interface Map<K,V>
    • release

      protected void release(boolean fDestroy)
      Release this cache, optionally destroying it.
      Parameters:
      fDestroy - true to destroy the cache as well
    • registerBackServiceListener

      protected MemberListener registerBackServiceListener()
      Instantiate and register a MemberListener with the back cache 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 cache service member listener.
    • registerMBean

      public void registerMBean()
      Register an MBean representing this NearCache.
    • unregisterMBean

      protected void unregisterMBean()
      Unregister an associated CacheMBean.
      Overrides:
      unregisterMBean in class CachingMap<K,V>
    • getContextClassLoader

      public ClassLoader getContextClassLoader()
      Retrieve the context ClassLoader for this object. The context ClassLoader is provided by the creator of the object for use by the object when loading classes and resources.
      Specified by:
      getContextClassLoader in interface ClassLoaderAware
      Returns:
      the context ClassLoader for this object
      See Also:
    • setContextClassLoader

      public void setContextClassLoader(ClassLoader loader)
      Specify the context ClassLoader for this object. The context ClassLoader can be set when the object is created, and allows the creator to provide the appropriate class loader to be used by the object when when loading classes and resources.
      Specified by:
      setContextClassLoader in interface ClassLoaderAware
      Parameters:
      loader - the context ClassLoader for this object