public class SlidingExpirationCache<K,V>
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected java.util.Map<K,CacheItem<V>> |
cache |
protected long |
cleanupIntervalNanos |
protected java.util.concurrent.atomic.AtomicLong |
cleanupTimeNanos |
protected ItemDisposalFunc<V> |
itemDisposalFunc |
protected java.util.concurrent.atomic.AtomicReference<ShouldDisposeFunc<V>> |
shouldDisposeFunc |
| Constructor and Description |
|---|
SlidingExpirationCache()
A cache that periodically cleans up expired entries.
|
SlidingExpirationCache(ShouldDisposeFunc<V> shouldDisposeFunc,
ItemDisposalFunc<V> itemDisposalFunc)
A cache that periodically cleans up expired entries.
|
SlidingExpirationCache(ShouldDisposeFunc<V> shouldDisposeFunc,
ItemDisposalFunc<V> itemDisposalFunc,
long cleanupIntervalNanos) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
cleanUp() |
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. |
V |
get(K key,
long itemExpirationNano) |
java.util.Map<K,V> |
getEntries()
Get a map copy of all entries in the cache, including expired entries.
|
V |
put(K key,
V value,
long itemExpirationNano) |
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.
|
protected void |
removeAndDispose(K key) |
protected void |
removeIfExpired(K key) |
void |
setCleanupIntervalNanos(long cleanupIntervalNanos)
Set the cleanup interval for the cache.
|
void |
setShouldDisposeFunc(ShouldDisposeFunc<V> shouldDisposeFunc) |
int |
size()
Get the current size of the cache, including expired entries.
|
protected long cleanupIntervalNanos
protected final java.util.concurrent.atomic.AtomicLong cleanupTimeNanos
protected final java.util.concurrent.atomic.AtomicReference<ShouldDisposeFunc<V>> shouldDisposeFunc
protected final ItemDisposalFunc<V> itemDisposalFunc
public SlidingExpirationCache()
public SlidingExpirationCache(ShouldDisposeFunc<V> shouldDisposeFunc, 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 SlidingExpirationCache(ShouldDisposeFunc<V> shouldDisposeFunc, ItemDisposalFunc<V> itemDisposalFunc, long cleanupIntervalNanos)
public void setShouldDisposeFunc(ShouldDisposeFunc<V> 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 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/disposedprotected void removeAndDispose(K key)
protected void removeIfExpired(K key)
public void clear()
public java.util.Map<K,V> getEntries()
public int size()
protected void cleanUp()
public void setCleanupIntervalNanos(long cleanupIntervalNanos)
ShouldDisposeFunc (if defined) are disposed.cleanupIntervalNanos - the time interval defining when we should clean up expired
entries marked for cleanup, in nanoseconds