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
ConstructorsConstructorDescriptionKeyValueCache(String name, int ttl, RecordFactory<V> recordFactory) Create a new cache -
Method Summary
Modifier and TypeMethodDescriptionstatic Stream<? extends KeyValueCache<?,?>> caches()protected abstract VFetch an item from the remote service (or generate it)final VGet an item from cachefinal VGet an item from cache without updating itfinal voidinvalidate(K key) voiditerator()final Stringname()The name of the cache for use in monitoringtoString()final longttl()The time-to-live for a record in cachefinal voidttl(int ttl) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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:OwnerThe name of the cache for use in monitoring -
ttl
public final long ttl()Description copied from interface:OwnerThe time-to-live for a record in cache -
ttl
public final void ttl(int ttl) -
toString
-