com.google.common.cache
Class ForwardingCache<K,V>

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.cache.ForwardingCache<K,V>
All Implemented Interfaces:
Function<K,V>, Cache<K,V>
Direct Known Subclasses:
ForwardingCache.SimpleForwardingCache

@Beta
public abstract class ForwardingCache<K,V>
extends ForwardingObject
implements Cache<K,V>

A cache which forwards all its method calls to another cache. Subclasses should override one or more methods to modify the behavior of the backing cache as desired per the decorator pattern.

Note that get(K), getUnchecked(K), and apply(K) all expose the same underlying functionality, so should probably be overridden as a group.

Since:
10.0
Author:
Charles Fry

Nested Class Summary
static class ForwardingCache.SimpleForwardingCache<K,V>
          A simplified version of ForwardingCache where subclasses can pass in an already constructed Cache as the delegete.
 
Constructor Summary
protected ForwardingCache()
          Constructor for use by subclasses.
 
Method Summary
 V apply(K key)
          Deprecated. 
 ConcurrentMap<K,V> asMap()
          Returns a view of the entries stored in this cache as a thread-safe map.
 void cleanUp()
          Performs any pending maintenance operations needed by the cache.
protected abstract  Cache<K,V> delegate()
          Returns the backing delegate instance that methods are forwarded to.
 V get(K key)
          Returns the value associated with the given key, creating or retrieving that value if necessary, and throwing an execution exception on failure.
 V getUnchecked(K key)
          Returns the value associated with the given key, loading that value if necessary.
 void invalidate(Object key)
          Discards any cached value for key key, possibly asynchronously, so that a future invocation of get(key) will result in a cache miss and reload.
 void invalidateAll()
          Discards all entries in the cache, possibly asynchronously.
 long size()
          Returns the approximate number of entries in this cache.
 CacheStats stats()
          Returns a current snapshot of this cache's cumulative statistics.
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.google.common.base.Function
equals
 

Constructor Detail

ForwardingCache

protected ForwardingCache()
Constructor for use by subclasses.

Method Detail

delegate

protected abstract Cache<K,V> delegate()
Description copied from class: ForwardingObject
Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.

Specified by:
delegate in class ForwardingObject

get

@Nullable
public V get(@Nullable
                      K key)
      throws ExecutionException
Description copied from interface: Cache
Returns the value associated with the given key, creating or retrieving that value if necessary, and throwing an execution exception on failure. No state associated with this cache is modified until loading completes. Note that this method will never return null.

Specified by:
get in interface Cache<K,V>
Throws:
ExecutionException - if a checked exception was thrown while loading the response

getUnchecked

@Nullable
public V getUnchecked(@Nullable
                               K key)
Description copied from interface: Cache
Returns the value associated with the given key, loading that value if necessary. No state associated with this cache is modified until computation completes. Unlike Cache.get(K), this method does not throw a checked exception, and thus should only be used in situations where checked exceptions are not thrown by the cache loader. Note that this method will never return null.

Warning: this method silently converts checked exceptions to unchecked exceptions. The Cache.get(K) method should be preferred for cache loaders which throw checked exceptions.

Specified by:
getUnchecked in interface Cache<K,V>

apply

@Deprecated
@Nullable
public V apply(@Nullable
                                   K key)
Deprecated. 

Description copied from interface: Cache
Discouraged. Provided to satisfy the Function interface; use Cache.get(K) or Cache.getUnchecked(K) instead.

Specified by:
apply in interface Function<K,V>
Specified by:
apply in interface Cache<K,V>

invalidate

public void invalidate(@Nullable
                       Object key)
Description copied from interface: Cache
Discards any cached value for key key, possibly asynchronously, so that a future invocation of get(key) will result in a cache miss and reload.

Specified by:
invalidate in interface Cache<K,V>

invalidateAll

public void invalidateAll()
Description copied from interface: Cache
Discards all entries in the cache, possibly asynchronously.

Specified by:
invalidateAll in interface Cache<K,V>

size

public long size()
Description copied from interface: Cache
Returns the approximate number of entries in this cache.

Specified by:
size in interface Cache<K,V>

stats

public CacheStats stats()
Description copied from interface: Cache
Returns a current snapshot of this cache's cumulative statistics. All stats are initialized to zero, and are monotonically increasing over the lifetime of the cache.

Specified by:
stats in interface Cache<K,V>

asMap

public ConcurrentMap<K,V> asMap()
Description copied from interface: Cache
Returns a view of the entries stored in this cache as a thread-safe map. Assume that none of the returned map's optional operations will be implemented, unless otherwise specified.

Operations on the returned map will never cause new values to be loaded into the cache. So, unlike Cache.get(K) and Cache.getUnchecked(K), this map's get method will always return null for a key that is not already cached.

Specified by:
asMap in interface Cache<K,V>

cleanUp

public void cleanUp()
Description copied from interface: Cache
Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.

Specified by:
cleanUp in interface Cache<K,V>


Copyright © 2010-2011. All Rights Reserved.