Class LabelledKeyValueCache<K,L,V>

java.lang.Object
ca.on.oicr.gsi.shesmu.plugin.cache.LabelledKeyValueCache<K,L,V>
Type Parameters:
K - the keys to use to lookup data in the cache
L - the "label" associated with a key; this allows a key's lifecycle to be different from its data's
V - the values retrievable from the cache based on state and key
All Implemented Interfaces:
Owner, Iterable<Map.Entry<L,Record<V>>>

public abstract class LabelledKeyValueCache<K,L,V> extends Object implements Owner, Iterable<Map.Entry<L,Record<V>>>
Store data that must be generated/fetched remotely and cache the results for a set period of time.
  • Constructor Details

    • LabelledKeyValueCache

      public LabelledKeyValueCache(String name, int ttl, RecordFactory<V> recordCtor)
      Create a new cache
      Parameters:
      name - the name, as presented to Prometheus
      ttl - the number of minutes an item will remain in cache
  • Method Details

    • caches

      public static Stream<? extends LabelledKeyValueCache<?,?,?>> caches()
    • fetch

      protected abstract V fetch(K key, L label, Instant lastUpdated) throws Exception
      Fetch an item from the remote service (or generate it)
      Parameters:
      key - the item to be requested
      lastUpdated - 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

      public final V get(K key)
      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

      public final V getStale(K key)
      Get an item from cache without updating it
      Parameters:
      key - the key to use
      Returns:
      the last value that was fetched
    • invalidate

      public final void invalidate(K key)
    • invalidateAll

      public void invalidateAll()
    • iterator

      public final Iterator<Map.Entry<L,Record<V>>> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • label

      protected abstract L label(K key)
      The fixed label associated with the specified key
    • name

      public final String name()
      Description copied from interface: Owner
      The name of the cache for use in monitoring
      Specified by:
      name in interface Owner
    • ttl

      public final long ttl()
      Description copied from interface: Owner
      The time-to-live for a record in cache
      Specified by:
      ttl in interface Owner
    • ttl

      public final void ttl(int ttl)