public final class Cache<K,V> extends Object implements Retriever<K,V>
An efficient Map-like class which caches values for keys. Values are not "put" into a Cache;
instead the caller supplies the instance with an implementation of Retriever which can load the
value for a given key.
The cache does not support null keys.
Thanks to Amila Jayasooriya for helping evaluate performance of the rewrite of this class, as part of a Google Summer of Code 2007 project.
| Modifier and Type | Class and Description |
|---|---|
static interface |
Cache.MatchPredicate<T>
Used by {#link #removeKeysMatching(Object)} to decide things that are matching.
|
| Constructor and Description |
|---|
Cache(Retriever<? super K,? extends V> retriever)
Creates a new cache based on the given
Retriever. |
Cache(Retriever<? super K,? extends V> retriever,
int maxEntries)
Creates a new cache based on the given
Retriever and with given maximum size. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears the cache.
|
V |
get(K key)
Returns cached value for a key.
|
void |
remove(K key)
Uncaches any existing value for a given key.
|
void |
removeKeysMatching(Cache.MatchPredicate<K> predicate)
Clears all cache entries whose key matches the given predicate.
|
void |
removeValueMatching(Cache.MatchPredicate<V> predicate)
Clears all cache entries whose value matches the given predicate.
|
String |
toString() |
public Cache(Retriever<? super K,? extends V> retriever)
Creates a new cache based on the given Retriever.
retriever - object which can retrieve values for keyspublic Cache(Retriever<? super K,? extends V> retriever, int maxEntries)
Creates a new cache based on the given Retriever and with given maximum size.
retriever - object which can retrieve values for keysmaxEntries - maximum number of entries the cache will store before evicting somepublic V get(K key) throws TasteException
Returns cached value for a key. If it does not exist, it is loaded using a Retriever.
get in interface Retriever<K,V>key - cache keyTasteException - if an exception occurs while retrieving a new cached valuepublic void remove(K key)
Uncaches any existing value for a given key.
key - cache keypublic void removeKeysMatching(Cache.MatchPredicate<K> predicate)
public void removeValueMatching(Cache.MatchPredicate<V> predicate)
public void clear()
Clears the cache.
Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.