CachingDidResolver

class CachingDidResolver(delegate: DidResolver, ttl: Duration = 5.minutes, maxSize: Int = 1000, clock: Clock = Clock.System) : DidResolver

Caching decorator for any DidResolver.

Caches successful and deactivated resolutions keyed by DID string, with:

  • a configurable time-to-live (ttl, default 5 minutes);

  • a configurable maximum size (maxSize, default 1000) with least-recently-used eviction;

  • requests carrying versionId, versionTime, accept, expandRelativeUrls or additional options bypass the cache (neither read nor written);

  • documentMetadata.nextUpdate honored when it is earlier than the TTL expiry (the entry expires at nextUpdate); a nextUpdate in the past means the document is already due for an update, so the result is returned but not cached.

What is cached:

Concurrency: coroutine-friendly and thread-safe without blocking locks around the suspending resolve call. State lives in a ConcurrentHashMap; if two coroutines concurrently resolve the same uncached DID, both will hit the delegate and the last writer wins — duplicate resolution is accepted as a deliberate trade-off to avoid holding a lock across a suspension point. LRU eviction is approximate: access order is tracked with a monotonic counter and the least-recently-used entry is evicted on insert when the bound is exceeded; concurrent races may transiently overshoot the bound by a small number of entries.

Example Usage:

val registry = RegistryBasedResolver(didMethodRegistry)
val universal = DefaultUniversalResolver("https://dev.uniresolver.io").asDidResolver()

// Compose: cache on top of local-registry-with-universal-fallback
val resolver = CachingDidResolver(FallbackDidResolver(registry, universal))

val result = resolver.resolve(Did("did:web:example.com")) // miss → delegate
val cached = resolver.resolve(Did("did:web:example.com")) // hit → cached Success or Deactivated

resolver.invalidate(Did("did:web:example.com")) // drop one entry
resolver.clear() // drop everything

Parameters

delegate

The resolver whose successful results are cached

ttl

Maximum lifetime of a cache entry (must be positive; default 5 minutes)

maxSize

Maximum number of cached entries (must be positive; default 1000)

clock

Time source, injectable for testing (defaults to Clock.System)

Constructors

Link copied to clipboard
constructor(delegate: DidResolver, ttl: Duration = 5.minutes, maxSize: Int = 1000, clock: Clock = Clock.System)

Properties

Link copied to clipboard
val size: Int

Current number of cached entries (primarily for diagnostics and tests).

Functions

Link copied to clipboard
fun clear()

Removes all cached entries.

Link copied to clipboard
fun invalidate(did: Did)

Removes the cached entry for did, if present. The next resolve for this DID will hit the delegate.

Link copied to clipboard
open suspend override fun resolve(did: Did): DidResolutionResult

Resolves a DID to a DID resolution result.

open suspend override fun resolve(did: Did, options: ResolutionOptions): DidResolutionResult

Resolves with DID Resolution 1.0 §4.1 options.

Link copied to clipboard

Resolves a DID and returns the document, or null if it could not be resolved.

Link copied to clipboard

Resolves a DID and returns the document, or throws if not found.