Skip to content
Wallet and Entity Collection draft updates
Documentation menu

8.2. Cache evidence without extending its lifetime

Feature guides / 8. Discovery and caching · Example context

Research Portal can reuse a recently fetched Umeå University OP EC rather than contact Umeå University OP for every login. The cached statement still expires, and the application still needs a valid chain for the operation it is performing.

How to use it

Attach a cache through LeafConfig.options, or to the protocol role's options when that operation needs its own cache. Authority endpoint contexts use storage.cache. Core supplies the shared cache contract and memory adapter; host and role operations manage the statement keys and retention. EC and Subordinate Statement retention defaults to at most 3600 seconds and is also bounded by each statement's exp; this example selects a shorter cap. EC retrieval and caching; Refresh.

import { Leaf } from "@oidfed/leaf";
import { entityId, MemoryFederationStorage, type CryptoContext, type TrustAnchorSet } from "@oidfed/core";

export async function cachedLigoWiki(keys: CryptoContext, trustAnchors: TrustAnchorSet) {
  const ligoWiki = await Leaf.create({
    entityId: "https://wiki.ligo.example", authorityHints: ["https://incommon.example"],
    crypto: keys, trustAnchors, metadata: { federation_entity: {} },
    storage: new MemoryFederationStorage({ namespace: "https://wiki.ligo.example", capabilities: ["cache"], maxCacheEntries: 500 }),
    options: { cacheMaxTtlSeconds: 300 },
  });
  return {
    ligoWiki,
    discoverUmeaOp: () => ligoWiki.discoverEntity(entityId("https://op.umu.example")),
  };
}

What your application owns

Use a shared cache when replicas need consistent behavior. The memory cache is process-local and disappears on restart. Expiry and refresh are separate from revocation: a cached valid statement can remain usable until its accepted lifetime ends. Do not extend a signed object's validity with your own TTL. Scheduling refresh, handling outages, and invalidating application sessions remain host decisions.

Plan for cache misses and expiry

A cache improves repeated discovery but cannot extend the signed evidence's lifetime. Choose maximum retention with the federation's update expectations in mind and preserve the expiration checks in consumers. Clearing a cache causes retrieval work; it does not remove trust from an anchor configuration.

When deploying several processes, decide whether their caches should be shared and how to bound simultaneous misses. A zero network budget can exercise cached or supplied evidence without transport. Test the missing-evidence case as well, so an offline or cache-only request fails clearly instead of unexpectedly starting discovery.

Use a process-local cache

The memory adapter’s cache stores detached JSON values with TTLs in seconds and evicts least recently used entries at capacity. Its data is process-local and lost on restart.

Attach the adapter to the host as above and let its operations read and populate evidence entries. Application code should not insert placeholder JWTs into the statement cache. Custom cache implementers can use the cache contract; ordinary callers do not need to calculate internal statement-cache keys.

Next: 8.3. Choose a validated path for a partner

View source on GitHub