com.google.common.cache
@Beta @GwtCompatible public interface Cache<K,V> extends Function<K,V>
#get(K, Callable)
or #put(K, V)
, and are stored in the cache until either
evicted or manually invalidated.
Note: in release 12.0, all methods moved from Cache
to LoadingCache
will be deleted from Cache
. As part of this transition Cache
will no longer
extend Function
.
Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.
All methods other than getIfPresent(K)
are optional.
Modifier and Type | Method and Description |
---|---|
V |
apply(K key)
Deprecated.
This method has been split out into the
LoadingCache interface, and will be
removed from Cache in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader) now returns a LoadingCache , so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader) . |
java.util.concurrent.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 |
get(K key)
Deprecated.
This method has been split out into the
LoadingCache interface, and will be
removed from Cache in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader) now returns a LoadingCache , so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader) . |
V |
get(K key,
java.util.concurrent.Callable<? extends V> valueLoader)
Returns the value associated with
key in this cache, obtaining that value from
valueLoader if necessary. |
ImmutableMap<K,V> |
getAllPresent(java.lang.Iterable<? extends K> keys)
Returns a map of the values associated with
keys in this cache. |
V |
getIfPresent(K key)
Returns the value associated with
key in this cache, or null if there is no
cached value for key . |
V |
getUnchecked(K key)
Deprecated.
This method has been split out into the
LoadingCache interface, and will be
removed from Cache in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader) now returns a LoadingCache , so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader) . |
void |
invalidate(java.lang.Object key)
Discards any cached value for key
key . |
void |
invalidateAll()
Discards all entries in the cache.
|
void |
invalidateAll(java.lang.Iterable<?> keys)
Discards any cached values for keys
keys . |
void |
put(K key,
V value)
Associates
value with key in this cache. |
long |
size()
Returns the approximate number of entries in this cache.
|
CacheStats |
stats()
Returns a current snapshot of this cache's cumulative statistics.
|
@Nullable V getIfPresent(K key)
key
in this cache, or null
if there is no
cached value for key
.V get(K key, java.util.concurrent.Callable<? extends V> valueLoader) throws java.util.concurrent.ExecutionException
key
in this cache, obtaining that value from
valueLoader
if necessary. No observable state associated with this cache is modified
until loading completes. This method provides a simple substitute for the conventional
"if cached, return; otherwise create, cache and return" pattern.
Warning: as with CacheLoader.load(K)
, valueLoader
must not return
null
; it may either return a non-null value or throw an exception.
java.util.concurrent.ExecutionException
- if a checked exception was thrown while loading the valueUncheckedExecutionException
- if an unchecked exception was thrown while loading the
valueExecutionError
- if an error was thrown while loading the valueImmutableMap<K,V> getAllPresent(java.lang.Iterable<? extends K> keys)
keys
in this cache. The returned map will
only contain entries which are already present in the cache.void put(K key, V value)
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.
void invalidate(java.lang.Object key)
key
.void invalidateAll(java.lang.Iterable<?> keys)
keys
.void invalidateAll()
long size()
CacheStats stats()
java.util.concurrent.ConcurrentMap<K,V> asMap()
void cleanUp()
@Deprecated V get(K key) throws java.util.concurrent.ExecutionException
LoadingCache
interface, and will be
removed from Cache
in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader)
now returns a LoadingCache
, so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader)
.key
in this cache, first loading that value if
necessary. No observable state associated with this cache is modified until loading completes.java.util.concurrent.ExecutionException
- if a checked exception was thrown while loading the valueUncheckedExecutionException
- if an unchecked exception was thrown while loading the
valueExecutionError
- if an error was thrown while loading the value@Deprecated V getUnchecked(K key)
LoadingCache
interface, and will be
removed from Cache
in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader)
now returns a LoadingCache
, so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader)
.key
in this cache, first loading that value if
necessary. No observable state associated with this cache is modified until computation
completes. Unlike get(K, java.util.concurrent.Callable extends V>)
, 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.
Warning: this method silently converts checked exceptions to unchecked exceptions, and should not be used with cache loaders which throw checked exceptions.
UncheckedExecutionException
- if an exception was thrown while loading the value,
regardless of whether the exception was checked or uncheckedExecutionError
- if an error was thrown while loading the value@Deprecated V apply(K key)
LoadingCache
interface, and will be
removed from Cache
in Guava release 12.0. Note that
CacheBuilder.build(CacheLoader)
now returns a LoadingCache
, so this deprecation
(migration) can be dealt with by simply changing the type of references to the results of
CacheBuilder.build(CacheLoader)
.Function
interface; use get(K, java.util.concurrent.Callable extends V>)
or
getUnchecked(K)
instead.apply
in interface Function<K,V>
UncheckedExecutionException
- if an exception was thrown while loading the value,
regardless of whether the exception was checked or uncheckedExecutionError
- if an error was thrown while loading the value