public class SlidingExpirationCache<K,V>
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
SlidingExpirationCache.ItemDisposalFunc<V>
An optional function defining extra cleanup steps to take when a cache item is cleaned up.
|
static interface |
SlidingExpirationCache.ShouldDisposeFunc<V>
An optional function defining the conditions under which an expired entry should be cleaned up
at cleanup time.
|
| Constructor and Description |
|---|
SlidingExpirationCache()
A cache that periodically cleans up expired entries.
|
SlidingExpirationCache(SlidingExpirationCache.ShouldDisposeFunc<V> shouldDisposeFunc,
SlidingExpirationCache.ItemDisposalFunc<V> itemDisposalFunc)
A cache that periodically cleans up expired entries.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Remove and dispose of all entries in the cache.
|
V |
computeIfAbsent(K key,
java.util.function.Function<? super K,? extends V> mappingFunction,
long itemExpirationNano)
In addition to performing the logic defined by
Map.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>), cleans up expired
entries if we have hit cleanup time. |
java.util.Map<K,V> |
getEntries()
Get a map copy of all entries in the cache, including expired entries.
|
void |
remove(K key)
Cleanup expired entries if we have hit the cleanup time, then remove and dispose the value
associated with the given key.
|
void |
setCleanupIntervalNanos(long cleanupIntervalNanos)
Set the cleanup interval for the cache.
|
int |
size()
Get the current size of the cache, including expired entries.
|
public SlidingExpirationCache()
public SlidingExpirationCache(SlidingExpirationCache.ShouldDisposeFunc<V> shouldDisposeFunc, SlidingExpirationCache.ItemDisposalFunc<V> itemDisposalFunc)
shouldDisposeFunc - a function defining the conditions under which an expired entry should
be cleaned up when we hit the cleanup timeitemDisposalFunc - a function that will be called on any item that meets the cleanup
criteria at cleanup time. The criteria for cleanup is that the item
is both expired and marked for cleanup via a call to
shouldDisposeFunc.public V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction, long itemExpirationNano)
Map.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>), cleans up expired
entries if we have hit cleanup time. If an expired entry is requested and we have not hit
cleanup time or SlidingExpirationCache.ShouldDisposeFunc indicated the entry should not be closed, the entry
will be marked as non-expired.key - the key with which the specified value is to be associatedmappingFunction - the function to compute a valueitemExpirationNano - the expiration time of the new or renewed entrypublic void remove(K key)
key - the key associated with the value to be removed/disposedpublic void clear()
public java.util.Map<K,V> getEntries()
public int size()
public void setCleanupIntervalNanos(long cleanupIntervalNanos)
SlidingExpirationCache.ShouldDisposeFunc (if defined) are disposed.cleanupIntervalNanos - the time interval defining when we should clean up expired
entries marked for cleanup, in nanoseconds