T - value type@ThreadSafe
public final class TTLCache<T>
extends java.lang.Object
Note that this cache does not proactively evict expired entries, however will immediately evict entries discovered to be expired on load.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TTLCache.EntryLoader<T> |
| Constructor and Description |
|---|
TTLCache(int maxSize,
long ttlInMillis,
TTLCache.EntryLoader<T> loader) |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears all entries from the cache.
|
long |
getLastUpdated(java.lang.String key)
Get when the entry at this key was last updated.
|
int |
getMaxSize()
Returns the maximum size of the cache.
|
T |
load(java.lang.String key)
Uses the default loader to calculate the value at key and insert it into the cache, if it
doesn't already exist or is expired according to the TTL.
|
T |
load(java.lang.String key,
java.util.function.Function<java.lang.String,T> f)
Uses the inputted function to calculate the value at key and insert it into the cache, if it
doesn't already exist or is expired according to the TTL.
|
T |
put(java.lang.String key,
T value)
Put a new entry in the cache.
|
int |
size()
Returns the current size of the cache.
|
java.lang.String |
toString() |
public TTLCache(int maxSize,
long ttlInMillis,
TTLCache.EntryLoader<T> loader)
maxSize - the maximum number of entries of the cachettlInMillis - the time to live value for entries of the cache, in millisecondspublic T load(java.lang.String key)
This immediately evicts entries past the TTL such that a load failure results in the removal of the entry.
Entries that are not expired according to the TTL are returned without recalculating the value.
Within a grace period past the TTL, the cache may either return the cached value without recalculating or use the loader to recalculate the value. This is implemented such that, in a multi-threaded environment, only one thread per cache key uses the loader to recalculate the value at one time.
key - The cache key to load the value atpublic T load(java.lang.String key, java.util.function.Function<java.lang.String,T> f)
This immediately evicts entries past the TTL such that a load failure results in the removal of the entry.
Entries that are not expired according to the TTL are returned without recalculating the value.
Within a grace period past the TTL, the cache may either return the cached value without recalculating or use the loader to recalculate the value. This is implemented such that, in a multi-threaded environment, only one thread per cache key uses the loader to recalculate the value at one time.
Returns the value of the given key (already existing or re-calculated).
key - The cache key to load the value atf - The function to use to load the value, given key as inputpublic T put(java.lang.String key, T value)
public long getLastUpdated(java.lang.String key)
public int size()
public int getMaxSize()
public void clear()
public java.lang.String toString()
toString in class java.lang.Object