K - the type of the keys in the cache.V - the type of the values in the cache.public class ExpirationCache<K,V>
extends java.lang.Object
ItemDisposalFunc is defined.| Modifier and Type | Field and Description |
|---|---|
protected java.util.Map<K,CacheItem<V>> |
cache |
protected static long |
DEFAULT_TIME_TO_LIVE_NANOS |
protected boolean |
isRenewableExpiration |
protected ItemDisposalFunc<V> |
itemDisposalFunc |
protected ShouldDisposeFunc<V> |
shouldDisposeFunc |
protected long |
timeToLiveNanos |
| Constructor and Description |
|---|
ExpirationCache() |
ExpirationCache(boolean isRenewableExpiration,
long timeToLiveNanos,
@Nullable ShouldDisposeFunc<V> shouldDisposeFunc,
@Nullable ItemDisposalFunc<V> itemDisposalFunc)
Constructs an ExpirationCache instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes and disposes of all entries in the cache.
|
V |
computeIfAbsent(K key,
java.util.function.Function<? super K,? extends V> mappingFunction)
If a value does not exist for the given key or the existing value is expired and non-renewable, stores the value
returned by the given mapping function, unless the function returns null, in which case the key will be removed.
|
boolean |
exists(K key)
Indicates whether a non-expired value is stored at the given key.
|
V |
get(K key)
Retrieves 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 at the given key.
|
V |
remove(K key)
Removes and disposes of the value stored at the given key.
|
protected V |
removeAndDispose(K key) |
void |
removeExpiredEntries()
Removes and disposes of all expired entries in the cache.
|
void |
removeIfExpired(K key)
Removes and disposes of the item stored at the given key if it is expired and the
ShouldDisposeFunc
(if defined) returns true for the item. |
int |
size()
Gets the current size of the cache, including expired entries.
|
protected static final long DEFAULT_TIME_TO_LIVE_NANOS
protected final boolean isRenewableExpiration
protected final long timeToLiveNanos
protected final ShouldDisposeFunc<V> shouldDisposeFunc
protected final ItemDisposalFunc<V> itemDisposalFunc
public ExpirationCache()
public ExpirationCache(boolean isRenewableExpiration,
long timeToLiveNanos,
@Nullable ShouldDisposeFunc<V> shouldDisposeFunc,
@Nullable ItemDisposalFunc<V> itemDisposalFunc)
isRenewableExpiration - controls whether an item's expiration should be renewed when retrieved. If the item is
expired when it is retrieved and isRenewableExpiration is true, the item's expiration
will be renewed and the item will be returned.timeToLiveNanos - the duration that the item should sit in the cache before being considered expired, in
nanoseconds.shouldDisposeFunc - a function defining the conditions under which an expired entry should be cleaned up.
If null is passed, the entry will always be cleaned up if it is expired.itemDisposalFunc - a function defining how to dispose of an item when it is removed. If null is passed,
the item will be removed without performing any additional operations.public V put(K key, V value)
key - the key at which the value should be stored.value - the value to store.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 V get(K key)
key - the key for the value.public boolean exists(K key)
key - the key for the value.public V remove(K key)
key - the key associated with the value to be removed and disposed.public void removeExpiredEntries()
public void removeIfExpired(K key)
ShouldDisposeFunc
(if defined) returns true for the item. Otherwise, does nothing.key - the key for the value to check for removal.public void clear()
public java.util.Map<K,V> getEntries()
public int size()