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

java.lang.Object
  extended by com.google.common.cache.AbstractCache<K,V>
All Implemented Interfaces:
Function<K,V>, Cache<K,V>

@Beta
public abstract class AbstractCache<K,V>
extends Object
implements Cache<K,V>

This class provides a skeletal implementation of the Cache interface to minimize the effort required to implement this interface.

To implement a cache, the programmer needs only to extend this class and provide an implementation for the get method. This implementation throws an UnsupportedOperationException on calls to size(), invalidate(java.lang.Object), invalidateAll(), stats(), and asMap(). The methods getUnchecked(K) and apply(K) are implemented in terms of Cache.get(K). The method cleanUp() is a no-op.

Since:
10.0
Author:
Charles Fry

Nested Class Summary
static class AbstractCache.SimpleStatsCounter
          A thread-safe AbstractCache.StatsCounter implementation for use by Cache implementors.
static interface AbstractCache.StatsCounter
          Accumulates statistics during the operation of a Cache for presentation by Cache.stats().
 
Constructor Summary
protected AbstractCache()
          Constructor for use by subclasses.
 
Method Summary
 V apply(K key)
          Discouraged.
 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.
 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.google.common.cache.Cache
get
 
Methods inherited from interface com.google.common.base.Function
equals
 

Constructor Detail

AbstractCache

protected AbstractCache()
Constructor for use by subclasses.

Method Detail

getUnchecked

public V getUnchecked(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

public final V apply(K key)
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>

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>

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>

invalidate

public void invalidate(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>

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>


Copyright © 2010-2011. All Rights Reserved.