Class MemoryCache<K,V>

java.lang.Object
org.cy3sbml.cache.MemoryCache<K,V>

public final class MemoryCache<K,V> extends Object
Thread-safe, size-bounded least-recently-used cache for web service lookups.

A found value is cached until evicted. A "not found" result (e.g. an HTTP 404) is cached for a short TTL, so a missing term is not re-requested on every render. A transport or parse error is never cached, so a transient outage (e.g. while offline) recovers on the next lookup.

Each key is loaded at most once at a time: concurrent requests for a key that is being loaded wait for that load and share its result. Loads of different keys run in parallel; no lock is held while the loader runs.

  • Field Details

    • DEFAULT_NOT_FOUND_TTL

      public static final Duration DEFAULT_NOT_FOUND_TTL
      Default TTL for a cached "not found" result.
  • Constructor Details

    • MemoryCache

      public MemoryCache(int maxEntries)
    • MemoryCache

      public MemoryCache(int maxEntries, Duration notFoundTtl, Clock clock)
      For tests: an injectable TTL and clock for the "not found" entries.
  • Method Details

    • get

      public Optional<V> get(K key, Function<K,FetchResult<V>> loader)
      Returns the cached value or loads it with the given loader. A found value is cached until evicted; a "not found" result is cached for a short TTL; an error is not cached, so it is retried on the next call.
    • size

      public int size()
    • clear

      public void clear()