Skip to main content

DocumentCache

Struct DocumentCache 

Source
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

Source

pub const DEFAULT_CAPACITY: usize = 1024

The number of entries new allows.

Source

pub fn new(ttl: Duration) -> Self

A cache whose entries live for ttl, holding up to DEFAULT_CAPACITY entries.

Source

pub fn with_capacity(ttl: Duration, capacity: usize) -> Self

A cache whose entries live for ttl, holding up to capacity entries (at least one).

Source

pub fn ttl(&self) -> Duration

How long an entry lives.

Source

pub fn capacity(&self) -> usize

How many entries the cache holds at most.

Source

pub fn get(&self, did: &Did) -> Option<ResolutionResult>

The cached result for did, if one is present and has not expired.

Source

pub fn insert(&self, did: Did, result: ResolutionResult)

Caches result for did, replacing any earlier entry.

Source

pub fn remove(&self, did: &Did) -> Option<ResolutionResult>

Removes the entry for did, returning it if it had not expired.

Source

pub fn clear(&self)

Removes every entry.

Source

pub fn purge_expired(&self) -> usize

Removes the expired entries, returning how many there were.

Source

pub fn len(&self) -> usize

The number of entries that have not expired.

Source

pub fn is_empty(&self) -> bool

Whether no entry is live.

Trait Implementations§

Source§

impl Debug for DocumentCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more