K - the type of the keys in the cache.V - the type of the values in the cache.public class ExternallyManagedCache<K,V>
extends java.lang.Object
ExpirationCache, but allows users
to manually renew item expiration and provides more control over the conditions in which items are removed. Disposal
of removed items should be handled outside of this class.| Modifier and Type | Field and Description |
|---|---|
protected java.util.Map<K,CacheItem<V>> |
cache |
protected long |
timeToLiveNanos |
| Constructor and Description |
|---|
ExternallyManagedCache(long timeToLiveNanos)
Constructs an externally managed cache.
|
| Modifier and Type | Method and Description |
|---|---|
V |
computeIfAbsent(K key,
java.util.function.Function<? super K,? extends V> mappingFunction)
If a value does not exist for the given key, stores the value returned by the given mapping function, unless the
function returns null, in which case the key will be removed.
|
void |
extendExpiration(K key)
Extends the expiration of the item stored at the given key, if it exists.
|
V |
get(K key)
Get the value stored at the given key.
|
java.util.Map<K,V> |
getEntries()
Gets a map copy of all entries in the cache, including expired entries.
|
V |
put(K key,
V value)
Stores the given value in the cache at the given key.
|
V |
remove(K key)
Removes the value stored at the given key from the cache.
|
V |
removeExpiredIf(K key,
java.util.function.Predicate<V> predicate)
Removes the value stored at the given key if it is expired and the given predicate returns true for the value.
|
V |
removeIf(K key,
java.util.function.Predicate<V> predicate)
Removes the value stored at the given key if the given predicate returns true for the value.
|
public ExternallyManagedCache(long timeToLiveNanos)
timeToLiveNanos - the duration that the item should sit in the cache before being considered expired, in
nanoseconds.public V put(K key, V value)
key - the key for the value.value - the value to store.public V get(K key)
key - the key for the value.public V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
key - the key for the new or existing value.mappingFunction - the function to call to compute a new value.public void extendExpiration(K key)
key - the key for the value whose expiration should be extended.public V remove(K key)
key - the key for the value to be removed.public V removeIf(K key, java.util.function.Predicate<V> predicate)
key - the key for the value to assess for removal.predicate - a predicate lambda that defines the condition under which the value should be removed.public V removeExpiredIf(K key, java.util.function.Predicate<V> predicate)
key - the key for the value to assess for removal.predicate - a predicate lambda that defines the condition under which the value should be removed if it is
also expired.