cache

Two-tier cache (L1 in-memory + L2 diskcache) for arXiv API results.

class arxiv_query_mcp.cache.TwoTierCache(cfg, dc)[source]

Bases: object

Two-tier cache with an in-memory L1 and a shared disk-backed L2.

L1 is a size-bounded OrderedDict used as an LRU cache with a TTL. L2 is a diskcache.Cache (SQLite-backed) that can be shared across multiple processes via the filesystem.

Concurrent in-process requests for the same key are coalesced via single-flight: only one underlying fetch runs and all waiters share the result via an asyncio.Future.

Parameters:
  • cfg (Config) – Server configuration providing cache settings.

  • dc (diskcache.Cache) – Open diskcache.Cache instance used as the L2 store. The caller is responsible for closing it.

async get_or_fetch(key, fetch_factory)[source]

Return a cached value or delegate to fetch_factory.

Look-up order: L1 hit → return; L2 hit → promote to L1 → return; miss → single-flight fetch → store L1 + L2 → return.

When cache_enabled is False this method is a transparent pass-through: it always calls fetch_factory and never reads or writes any cache.

Concurrent calls with identical key values are coalesced: the first caller runs fetch_factory, while subsequent callers await the same asyncio.Future.

Parameters:
  • key (tuple[Any, ...]) – Hashable tuple that identifies the request (e.g. ("api", tuple(sorted(params.items()))).

  • fetch_factory (Callable[[], Coroutine[Any, Any, Any]]) – Zero-argument callable that returns a coroutine performing the actual fetch. Called at most once per key during the single-flight window.

Returns:

The cached or freshly fetched value.

Raises:

Exception – Any exception raised by fetch_factory is propagated to all waiters and is not stored in the cache.

Return type:

Any

close()[source]

No-op: the underlying diskcache is managed by the caller.