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.
| Modifier and Type | Class and Description |
|---|---|
| public static class | AbstractCache.SimpleStatsCounter
A thread-safe |
| public static interface | AbstractCache.StatsCounter
Accumulates statistics during the operation of a |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public ConcurrentMap | asMap()
Implements com. |
| public void | cleanUp()
Implements com. |
| public V | get(K key, Callable<? extends V> valueLoader)
Implements com. key in this cache, obtaining that value from loader if necessary.
|
| public ImmutableMap | getAllPresent(Iterable<? extends Object> keys)
Implements com. keys in this cache.
|
| public void | invalidate(Object key)
Implements com. key.
|
| public void | invalidateAll(Iterable<? extends Object> keys)
Implements com. keys.
|
| public void | invalidateAll()
Implements com. |
| public void | put(K key, V value)
Implements com. value with key in this cache.
|
| public void | putAll(Map<? extends K, ? extends V> m)
Implements com. |
| public long | size()
Implements com. |
| public CacheStats | stats()
Implements com. |
| AbstractCache | back to summary |
|---|---|
| protected AbstractCache() Constructor for use by subclasses. | |
| asMap | back to summary |
|---|---|
| public ConcurrentMap Implements com. Doc from com. 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.
| |
| cleanUp | back to summary |
|---|---|
| public void cleanUp() Implements com. Doc from com. Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.
| |
| get | back to summary |
|---|---|
| public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException Implements com. Doc from com. Returns the value associated with Among the improvements that this method and
Among the further improvements that
Warning For any given key, every Warning as with No observable state associated with this cache is modified until loading completes.
| |
| getAllPresent | back to summary |
|---|---|
| public ImmutableMap Implements com. Doc from com. Returns a map of the values associated with This implementation of
| |
| invalidate | back to summary |
|---|---|
| public void invalidate(Object key) Implements com. Doc from com. Discards any cached value for key
| |
| invalidateAll | back to summary |
|---|---|
| public void invalidateAll(Iterable<? extends Object> keys) Implements com. Doc from com. Discards any cached values for keys
| |
| invalidateAll | back to summary |
|---|---|
| public void invalidateAll() Implements com. Doc from com. Discards all entries in the cache.
| |
| put | back to summary |
|---|---|
| public void put(K key, V value) Implements com. Doc from com. Associates Prefer
| |
| putAll | back to summary |
|---|---|
| public void putAll(Map<? extends K, ? extends V> m) Implements com. Doc from com. Copies all of the mappings from the specified map to the cache. The effect of this call is
equivalent to that of calling
| |
| size | back to summary |
|---|---|
| public long size() Implements com. Doc from com. Returns the approximate number of entries in this cache.
| |
| stats | back to summary |
|---|---|
| public CacheStats stats() Implements com. Doc from com. 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
| |
StatsCounter implementation for use by Cache implementors.
| Modifier and Type | Field and Description |
|---|---|
| private final LongAddable | |
| private final LongAddable | |
| private final LongAddable | |
| private final LongAddable | |
| private final LongAddable | |
| private final LongAddable |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| private static long | |
| public void | recordEviction()
Implements com. |
| public void | recordHits(int
the number of hits to record count)Implements com. |
| 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. |
| public void | recordLoadSuccess(long
the number of nanoseconds the cache spent computing or retrieving the new
value loadTime)Implements com. |
| public void | recordMisses(int
the number of misses to record count)Implements com. |
| public CacheStats | snapshot()
Implements com. |
| evictionCount | back to summary |
|---|---|
| private final LongAddable evictionCount | |
| hitCount | back to summary |
|---|---|
| private final LongAddable hitCount | |
| loadExceptionCount | back to summary |
|---|---|
| private final LongAddable loadExceptionCount | |
| loadSuccessCount | back to summary |
|---|---|
| private final LongAddable loadSuccessCount | |
| missCount | back to summary |
|---|---|
| private final LongAddable missCount | |
| totalLoadTime | back to summary |
|---|---|
| private final LongAddable totalLoadTime | |
| SimpleStatsCounter | back to summary |
|---|---|
| public SimpleStatsCounter() Constructs an instance with all counts initialized to zero. | |
| incrementBy | back to summary |
|---|---|
| public void incrementBy(AbstractCache. Increments all counters by the values in | |
| negativeToMaxValue | back to summary |
|---|---|
| private static long negativeToMaxValue(long value) Returns | |
| recordEviction | back to summary |
|---|---|
| public void recordEviction() Implements com. Doc from com. 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.
| |
| recordHits | back to summary |
|---|---|
| public void recordHits(int count) Implements com. Doc from com. Records cache hits. This should be called when a cache request returns a cached value.
| |
| recordLoadException | back to summary |
|---|---|
| public void recordLoadException(long loadTime) Implements com. Doc from com. 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
| |
| recordLoadSuccess | back to summary |
|---|---|
| public void recordLoadSuccess(long loadTime) Implements com. Doc from com. 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 | back to summary |
|---|---|
| public void recordMisses(int count) Implements com. Doc from com. 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
| |
| snapshot | back to summary |
|---|---|
| public CacheStats snapshot() Implements com. Doc from com. Returns a snapshot of this counter's values. Note that this may be an inconsistent view, as it may be interleaved with update operations.
| |
Cache for presentation by Cache#stats. This is solely intended for consumption by Cache implementors.
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| 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 | |
| public CacheStats |
| recordEviction | back 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. | |
| recordHits | back to summary |
|---|---|
| public void recordHits(int count) Records cache hits. This should be called when a cache request returns a cached value.
| |
| recordLoadException | back 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
| |
| recordLoadSuccess | back 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 | back 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
| |
| snapshot | back 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. | |