java.lang.Object
ca.on.oicr.gsi.shesmu.plugin.cache.KeyValueCache<K,V>
- Type Parameters:
K
- the keys to use to lookup data in the cacheV
- the values retrievable from the cache based on state and key
public abstract class KeyValueCache<K,V>
extends Object
implements Owner, Iterable<Map.Entry<K,Record<V>>>
Store data that must be generated/fetched remotely and cache the results for a set period of
time.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionKeyValueCache
(String name, int ttl, RecordFactory<V> recordFactory) Create a new cache -
Method Summary
Modifier and TypeMethodDescriptionstatic Stream<? extends KeyValueCache<?,
?>> caches()
protected abstract V
Fetch an item from the remote service (or generate it)final V
Get an item from cachefinal V
Get an item from cache without updating itfinal void
invalidate
(K key) void
iterator()
final String
name()
The name of the cache for use in monitoringtoString()
final long
ttl()
The time-to-live for a record in cachefinal void
ttl
(int ttl) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
KeyValueCache
Create a new cache- Parameters:
name
- the name, as presented to Prometheusttl
- the number of minutes an item will remain in cache
-
-
Method Details
-
caches
-
fetch
Fetch an item from the remote service (or generate it)- Parameters:
key
- the item to be requestedlastUpdated
- the last time the item was successfully fetched- Returns:
- the cached value
- Throws:
Exception
- if an error occurs, the previous value will be retained
-
get
Get an item from cache- Parameters:
key
- the key to use- Returns:
- the value, if it was possible to fetch; the value may be stale if the remote end-point is in an error state
-
getStale
Get an item from cache without updating it- Parameters:
key
- the key to use- Returns:
- the last value that was fetched
-
invalidate
-
invalidateAll
public void invalidateAll() -
iterator
-
name
Description copied from interface:Owner
The name of the cache for use in monitoring -
ttl
public final long ttl()Description copied from interface:Owner
The time-to-live for a record in cache -
ttl
public final void ttl(int ttl) -
toString
-