pub struct CachedKeyStore<C: KeyCache, S: KeyStore> { /* private fields */ }Expand description
A caching wrapper for any KeyStore implementation.
This wrapper uses the cache-aside pattern: it first checks the cache for the key set, and only fetches from the underlying store on a cache miss. Retrieved key sets are then stored in the cache for future requests.
This is the recommended entry point for cached JWKS retrieval.
When looking up a key by ID, if the cached key set doesn’t contain the requested key, the store refetches from the underlying source. This handles key rotation gracefully: newly added keys are discovered automatically without waiting for cache expiration.
§Async and Send Bounds
On native targets, the KeyStore implementation for CachedKeyStore
requires Send + Sync for both cache and source store for thread-safe usage.
On wasm32, these bounds are relaxed to match the ?Send async model.
§Examples
use jwk_simple::jwks::{CachedKeyStore, HttpKeyStore, KeyStore, MokaKeyCache};
use std::time::Duration;
// Create a cached remote key store
let cache = MokaKeyCache::new(Duration::from_secs(300));
let remote = HttpKeyStore::new("https://example.com/.well-known/jwks.json")?;
let cached = CachedKeyStore::new(cache, remote);
// First call fetches from remote, caches the key set
let key = cached.get_key("kid").await?;
// Subsequent calls use the cache
let key = cached.get_key("kid").await?;