pub struct DocumentCache { /* private fields */ }Expand description
A bounded cache of resolution results, keyed by DID, where every entry expires after the same time to live.
A Resolver built with cache serves
repeated resolutions of a DID from it, for resolutions without options only: options such
as versionId or accept change what is returned, so those always reach the method.
The cache is Sync: readers share a lock, and an insert takes it exclusively. When it is
full, expired entries go first and then the entry that expires soonest.
§Examples
use std::time::Duration;
use identity_kit::did::{Did, DidDocument, ResolutionResult};
use identity_kit::resolver::DocumentCache;
let cache = DocumentCache::new(Duration::from_secs(300));
let did = Did::parse("did:example:123")?;
cache.insert(did.clone(), ResolutionResult::new(DidDocument::new(did.clone())));
assert!(cache.get(&did).is_some());
assert_eq!(cache.len(), 1);
cache.remove(&did);
assert!(cache.is_empty());Implementations§
Source§impl DocumentCache
impl DocumentCache
Sourcepub const DEFAULT_CAPACITY: usize = 1024
pub const DEFAULT_CAPACITY: usize = 1024
The number of entries new allows.
Sourcepub fn new(ttl: Duration) -> Self
pub fn new(ttl: Duration) -> Self
A cache whose entries live for ttl, holding up to
DEFAULT_CAPACITY entries.
Sourcepub fn with_capacity(ttl: Duration, capacity: usize) -> Self
pub fn with_capacity(ttl: Duration, capacity: usize) -> Self
A cache whose entries live for ttl, holding up to capacity entries (at least one).
Sourcepub fn get(&self, did: &Did) -> Option<ResolutionResult>
pub fn get(&self, did: &Did) -> Option<ResolutionResult>
The cached result for did, if one is present and has not expired.
Sourcepub fn insert(&self, did: Did, result: ResolutionResult)
pub fn insert(&self, did: Did, result: ResolutionResult)
Caches result for did, replacing any earlier entry.
Sourcepub fn remove(&self, did: &Did) -> Option<ResolutionResult>
pub fn remove(&self, did: &Did) -> Option<ResolutionResult>
Removes the entry for did, returning it if it had not expired.
Sourcepub fn purge_expired(&self) -> usize
pub fn purge_expired(&self) -> usize
Removes the expired entries, returning how many there were.