Class InMemoryMcpResponseCache

java.lang.Object
org.apache.juneau.rest.client.mcp.InMemoryMcpResponseCache
All Implemented Interfaces:
McpResponseCache

public class InMemoryMcpResponseCache extends Object implements McpResponseCache
Built-in, process-local, in-memory default implementation of McpResponseCache.

Entries are partitioned by scope, so the same key in two different scopes never collides. A non-positive ttlMs passed to put(String, String, Object, long) means the entry never expires.

Size bound / eviction policy:

The cache is bounded at DEFAULT_MAX_ENTRIES total entries (across all scopes combined) by default, overridable via InMemoryMcpResponseCache(int). Once the cap is reached, inserting one more entry evicts the single least-recently-used entry (an access via get(String, String), whether a hit or a still-live entry, counts as a use; an expired-and-evicted-on-read entry does not) — a bounded default that never grows unboundedly under sustained cache traffic, without needing every caller to reason about a capacity setting themselves.

This class is thread-safe (a synchronized LinkedHashMap in access-order/LRU mode).

Since:
10.0.0
  • Field Details

    • DEFAULT_MAX_ENTRIES

      public static final int DEFAULT_MAX_ENTRIES
      Default maximum number of entries (across all scopes combined) retained before the least-recently-used entry is evicted to make room for a new one. See the class javadoc's size-bound/eviction-policy note.
      See Also:
  • Constructor Details

  • Method Details

    • get

      public Optional<Object> get(String scope, String key)
      Description copied from interface: McpResponseCache
      Looks up a cached value.
      Specified by:
      get in interface McpResponseCache
      Parameters:
      scope - The cache-scope partition key. Must not be null.
      key - The cache key (typically method + canonical params). Must not be null.
      Returns:
      The cached value, or Optional.empty() if absent or expired.
    • put

      public void put(String scope, String key, Object value, long ttlMs)
      Description copied from interface: McpResponseCache
      Stores a value.
      Specified by:
      put in interface McpResponseCache
      Parameters:
      scope - The cache-scope partition key. Must not be null.
      key - The cache key (typically method + canonical params). Must not be null.
      value - The value to cache. Must not be null.
      ttlMs - The time-to-live in milliseconds. A value <= 0 means the entry never expires.
    • clear

      public void clear()
      Description copied from interface: McpResponseCache
      Removes all cached entries across all scopes.
      Specified by:
      clear in interface McpResponseCache