Top Description Inners Constructors Methods
com.google.common.cache

public abstract Class AbstractCache<K, V>

extends Object
implements Cache<K, V>
Class Inheritance
All Implemented Interfaces
com.google.common.cache.Cache
Known Direct Subclasses
com.google.common.cache.AbstractLoadingCache
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.GwtCompatible, com.google.common.collect.ImmutableMap, .Maps, java.util.Map, .Map.Entry, java.util.concurrent.Callable, .ConcurrentMap, .ExecutionException

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 put and getIfPresent methods. getAllPresent is implemented in terms of getIfPresent; putAll is implemented in terms of put, invalidateAll(Iterable) is implemented in terms of invalidate. The method cleanUp is a no-op. All other methods throw an UnsupportedOperationException.

Author
Charles Fry
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
AbstractCache.SimpleStatsCounter

A thread-safe StatsCounter implementation for use by Cache implementors.

public static interface
AbstractCache.StatsCounter

Accumulates statistics during the operation of a Cache for presentation by Cache#stats.

Constructor Summary

AccessConstructor and Description
protected
AbstractCache()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public ConcurrentMap<K, V>
asMap()

Implements com.google.common.cache.Cache.asMap.

Returns a view of the entries stored in this cache as a thread-safe map.
public void
cleanUp()

Implements com.google.common.cache.Cache.cleanUp.

Performs any pending maintenance operations needed by the cache.
public V
get(K key, Callable<? extends V> valueLoader)

Implements com.google.common.cache.Cache.get.

Returns the value associated with key in this cache, obtaining that value from loader if necessary.
public ImmutableMap<K, V>
getAllPresent(Iterable<? extends Object> keys)

Implements com.google.common.cache.Cache.getAllPresent.

Returns a map of the values associated with keys in this cache.
public void
invalidate(Object key)

Implements com.google.common.cache.Cache.invalidate.

Discards any cached value for key key.
public void
invalidateAll(Iterable<? extends Object> keys)

Implements com.google.common.cache.Cache.invalidateAll.

Discards any cached values for keys keys.
public void
invalidateAll()

Implements com.google.common.cache.Cache.invalidateAll.

Discards all entries in the cache.
public void
put(K key, V value)

Implements com.google.common.cache.Cache.put.

Associates value with key in this cache.
public void
putAll(Map<? extends K, ? extends V> m)

Implements com.google.common.cache.Cache.putAll.

Copies all of the mappings from the specified map to the cache.
public long
size()

Implements com.google.common.cache.Cache.size.

Returns the approximate number of entries in this cache.
public CacheStats
stats()

Implements com.google.common.cache.Cache.stats.

Returns a current snapshot of this cache's cumulative statistics, or a set of default values if the cache is not recording statistics.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AbstractCacheback to summary
protected AbstractCache()

Constructor for use by subclasses.

Method Detail

asMapback to summary
public ConcurrentMap<K, V> asMap()

Implements com.google.common.cache.Cache.asMap.

Doc from com.google.common.cache.Cache.asMap.

Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.

Iterators from the returned map are at least weakly consistent: they are safe for concurrent use, but if the cache is modified (including by eviction) after the iterator is created, it is undefined which of the changes (if any) will be reflected in that iterator.

Annotations
@Override
cleanUpback to summary
public void cleanUp()

Implements com.google.common.cache.Cache.cleanUp.

Doc from com.google.common.cache.Cache.cleanUp.

Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.

Annotations
@Override
getback to summary
public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException

Implements com.google.common.cache.Cache.get.

Doc from com.google.common.cache.Cache.get.

Returns the value associated with key in this cache, obtaining that value from loader if necessary. The method improves upon the conventional "if cached, return; otherwise create, cache and return" pattern. For further improvements, use LoadingCache and its get(K) method instead of this one.

Among the improvements that this method and LoadingCache.get(K) both provide are:

Among the further improvements that LoadingCache can provide but this method cannot:

Warning

For any given key, every loader used with it should compute the same value. Otherwise, a call that passes one loader may return the result of another call with a differently behaving loader. For example, a call that requests a short timeout for an RPC may wait for a similar call that requests a long timeout, or a call by an unprivileged user may return a resource accessible only to a privileged user making a similar call. To prevent this problem, create a key object that includes all values that affect the result of the query. Or use LoadingCache.get(K), which lacks the ability to refer to state other than that in the key.

Warning

as with CacheLoader#load, loader must not return null; it may either return a non-null value or throw an exception.

No observable state associated with this cache is modified until loading completes.

Annotations
@Override
Exceptions
ExecutionException:
if a checked exception was thrown while loading the value
Since
11.0
getAllPresentback to summary
public ImmutableMap<K, V> getAllPresent(Iterable<? extends Object> keys)

Implements com.google.common.cache.Cache.getAllPresent.

Doc from com.google.common.cache.Cache.getAllPresent.

Returns a map of the values associated with keys in this cache. The returned map will only contain entries which are already present in the cache.

This implementation of getAllPresent lacks any insight into the internal cache data structure, and is thus forced to return the query keys instead of the cached keys. This is only possible with an unsafe cast which requires keys to actually be of type K.

Annotations
@Override
Since
11.0
invalidateback to summary
public void invalidate(Object key)

Implements com.google.common.cache.Cache.invalidate.

Doc from com.google.common.cache.Cache.invalidate.

Discards any cached value for key key.

Annotations
@Override
invalidateAllback to summary
public void invalidateAll(Iterable<? extends Object> keys)

Implements com.google.common.cache.Cache.invalidateAll.

Doc from com.google.common.cache.Cache.invalidateAll.

Discards any cached values for keys keys.

Annotations
@Override
Since
11.0
invalidateAllback to summary
public void invalidateAll()

Implements com.google.common.cache.Cache.invalidateAll.

Doc from com.google.common.cache.Cache.invalidateAll.

Discards all entries in the cache.

Annotations
@Override
putback to summary
public void put(K key, V value)

Implements com.google.common.cache.Cache.put.

Doc from com.google.common.cache.Cache.put.

Associates value with key in this cache. If the cache previously contained a value associated with key, the old value is replaced by value.

Prefer get(Object, Callable) when using the conventional "if cached, return; otherwise create, cache and return" pattern.

Annotations
@Override
Since
11.0
putAllback to summary
public void putAll(Map<? extends K, ? extends V> m)

Implements com.google.common.cache.Cache.putAll.

Doc from com.google.common.cache.Cache.putAll.

Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.

Annotations
@Override
Since
12.0
sizeback to summary
public long size()

Implements com.google.common.cache.Cache.size.

Doc from com.google.common.cache.Cache.size.

Returns the approximate number of entries in this cache.

Annotations
@Override
statsback to summary
public CacheStats stats()

Implements com.google.common.cache.Cache.stats.

Doc from com.google.common.cache.Cache.stats.

Returns a current snapshot of this cache's cumulative statistics, or a set of default values if the cache is not recording statistics. All statistics begin at zero and never decrease over the lifetime of the cache.

Warning

this cache may not be recording statistical data. For example, a cache created using CacheBuilder only does so if the CacheBuilder#recordStats method was called. If statistics are not being recorded, a CacheStats instance with zero for all values is returned.

Annotations
@Override
com.google.common.cache back to summary

public final Class AbstractCache.SimpleStatsCounter

extends Object
implements StatsCounter
Class Inheritance
All Implemented Interfaces
com.google.common.cache.AbstractCache.StatsCounter

A thread-safe StatsCounter implementation for use by Cache implementors.
Since
10.0

Field Summary

Modifier and TypeField and Description
private final LongAddable
private final LongAddable
private final LongAddable
private final LongAddable
private final LongAddable
private final LongAddable

Constructor Summary

AccessConstructor and Description
public
SimpleStatsCounter()

Constructs an instance with all counts initialized to zero.

Method Summary

Modifier and TypeMethod and Description
public void
incrementBy(AbstractCache.StatsCounter other)

Increments all counters by the values in other.

private static long
negativeToMaxValue(long value)

Returns value, if non-negative.

public void
recordEviction()

Implements com.google.common.cache.AbstractCache.StatsCounter.recordEviction.

Records the eviction of an entry from the cache.
public void
recordHits(int
the number of hits to record
count
)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordHits.

Records cache hits.
public void
recordLoadException(long
the number of nanoseconds the cache spent computing or retrieving the new value prior to an exception being thrown
loadTime
)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordLoadException.

Records the failed load of a new entry.
public void
recordLoadSuccess(long
the number of nanoseconds the cache spent computing or retrieving the new value
loadTime
)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordLoadSuccess.

Records the successful load of a new entry.
public void
recordMisses(int
the number of misses to record
count
)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordMisses.

Records cache misses.
public CacheStats
snapshot()

Implements com.google.common.cache.AbstractCache.StatsCounter.snapshot.

Returns a snapshot of this counter's values.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

evictionCountback to summary
private final LongAddable evictionCount
hitCountback to summary
private final LongAddable hitCount
loadExceptionCountback to summary
private final LongAddable loadExceptionCount
loadSuccessCountback to summary
private final LongAddable loadSuccessCount
missCountback to summary
private final LongAddable missCount
totalLoadTimeback to summary
private final LongAddable totalLoadTime

Constructor Detail

SimpleStatsCounterback to summary
public SimpleStatsCounter()

Constructs an instance with all counts initialized to zero.

Method Detail

incrementByback to summary
public void incrementBy(AbstractCache.StatsCounter other)

Increments all counters by the values in other.

negativeToMaxValueback to summary
private static long negativeToMaxValue(long value)

Returns value, if non-negative. Otherwise, returns Long#MAX_VALUE.

recordEvictionback to summary
public void recordEviction()

Implements com.google.common.cache.AbstractCache.StatsCounter.recordEviction.

Doc from com.google.common.cache.AbstractCache.StatsCounter.recordEviction.

Records the eviction of an entry from the cache. This should only been called when an entry is evicted due to the cache's eviction strategy, and not as a result of manual invalidations.

Annotations
@Override
recordHitsback to summary
public void recordHits(int count)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordHits.

Doc from com.google.common.cache.AbstractCache.StatsCounter.recordHits.

Records cache hits. This should be called when a cache request returns a cached value.

Parameters
count:int

the number of hits to record

Annotations
@Override
Since
11.0
recordLoadExceptionback to summary
public void recordLoadException(long loadTime)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordLoadException.

Doc from com.google.common.cache.AbstractCache.StatsCounter.recordLoadException.

Records the failed load of a new entry. This should be called when a cache request causes an entry to be loaded, but an exception is thrown while loading the entry. In contrast to recordMisses, this method should only be called by the loading thread.

Parameters
loadTime:long

the number of nanoseconds the cache spent computing or retrieving the new value prior to an exception being thrown

Annotations
@SuppressWarnings:GoodTime
@Override
recordLoadSuccessback to summary
public void recordLoadSuccess(long loadTime)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordLoadSuccess.

Doc from com.google.common.cache.AbstractCache.StatsCounter.recordLoadSuccess.

Records the successful load of a new entry. This should be called when a cache request causes an entry to be loaded, and the loading completes successfully. In contrast to recordMisses, this method should only be called by the loading thread.

Parameters
loadTime:long

the number of nanoseconds the cache spent computing or retrieving the new value

Annotations
@SuppressWarnings:GoodTime
@Override
recordMissesback to summary
public void recordMisses(int count)

Implements com.google.common.cache.AbstractCache.StatsCounter.recordMisses.

Doc from com.google.common.cache.AbstractCache.StatsCounter.recordMisses.

Records cache misses. This should be called when a cache request returns a value that was not found in the cache. This method should be called by the loading thread, as well as by threads blocking on the load. Multiple concurrent calls to Cache lookup methods with the same key on an absent value should result in a single call to either recordLoadSuccess or recordLoadException and multiple calls to this method, despite all being served by the results of a single load operation.

Parameters
count:int

the number of misses to record

Annotations
@Override
Since
11.0
snapshotback to summary
public CacheStats snapshot()

Implements com.google.common.cache.AbstractCache.StatsCounter.snapshot.

Doc from com.google.common.cache.AbstractCache.StatsCounter.snapshot.

Returns a snapshot of this counter's values. Note that this may be an inconsistent view, as it may be interleaved with update operations.

Annotations
@Override
com.google.common.cache back to summary

public Interface AbstractCache.StatsCounter

Known Direct Implementers
com.google.common.cache.AbstractCache.SimpleStatsCounter

Accumulates statistics during the operation of a Cache for presentation by Cache#stats. This is solely intended for consumption by Cache implementors.
Since
10.0

Method Summary

Modifier and TypeMethod and Description
public void
recordEviction()

Records the eviction of an entry from the cache.

public void
recordHits(int
the number of hits to record
count
)

Records cache hits.

public void
recordLoadException(long
the number of nanoseconds the cache spent computing or retrieving the new value prior to an exception being thrown
loadTime
)

Records the failed load of a new entry.

public void
recordLoadSuccess(long
the number of nanoseconds the cache spent computing or retrieving the new value
loadTime
)

Records the successful load of a new entry.

public void
recordMisses(int
the number of misses to record
count
)

Records cache misses.

public CacheStats
snapshot()

Returns a snapshot of this counter's values.

Method Detail

recordEvictionback to summary
public void recordEviction()

Records the eviction of an entry from the cache. This should only been called when an entry is evicted due to the cache's eviction strategy, and not as a result of manual invalidations.

recordHitsback to summary
public void recordHits(int count)

Records cache hits. This should be called when a cache request returns a cached value.

Parameters
count:int

the number of hits to record

Since
11.0
recordLoadExceptionback to summary
public void recordLoadException(long loadTime)

Records the failed load of a new entry. This should be called when a cache request causes an entry to be loaded, but an exception is thrown while loading the entry. In contrast to recordMisses, this method should only be called by the loading thread.

Parameters
loadTime:long

the number of nanoseconds the cache spent computing or retrieving the new value prior to an exception being thrown

Annotations
@SuppressWarnings:GoodTime
recordLoadSuccessback to summary
public void recordLoadSuccess(long loadTime)

Records the successful load of a new entry. This should be called when a cache request causes an entry to be loaded, and the loading completes successfully. In contrast to recordMisses, this method should only be called by the loading thread.

Parameters
loadTime:long

the number of nanoseconds the cache spent computing or retrieving the new value

Annotations
@SuppressWarnings:GoodTime
recordMissesback to summary
public void recordMisses(int count)

Records cache misses. This should be called when a cache request returns a value that was not found in the cache. This method should be called by the loading thread, as well as by threads blocking on the load. Multiple concurrent calls to Cache lookup methods with the same key on an absent value should result in a single call to either recordLoadSuccess or recordLoadException and multiple calls to this method, despite all being served by the results of a single load operation.

Parameters
count:int

the number of misses to record

Since
11.0
snapshotback to summary
public CacheStats snapshot()

Returns a snapshot of this counter's values. Note that this may be an inconsistent view, as it may be interleaved with update operations.