Skip to main content

Cache

Struct Cache 

Source
pub struct Cache<I, P> { /* private fields */ }
Available on crate features alloc or no-atomic or std only.
Expand description

Passive record cache.

Implementations§

Source§

impl<I, P> Cache<I, P>
where I: Instant, P: Pool<CacheEntry<I>>,

Source

pub fn new() -> Self

Empty cache with the default maximum entry cap (1024).

Source

pub fn with_max_entries(max: usize) -> Self

Empty cache with a custom maximum entry cap.

When try_insert is called and the number of stored entries has reached max, the soonest-expiring entry is evicted proactively before the new entry is inserted. This bounds memory usage even when the backing Pool grows without error (e.g. slab::Slab).

Source

pub const fn max_entries(&self) -> usize

The configured maximum number of entries.

Source

pub fn len(&self) -> usize

The current number of cached entries.

Source

pub fn is_empty(&self) -> bool

Whether the cache is empty.

Source

pub fn try_insert( &mut self, name: Name, rtype: ResourceType, rclass: ResourceClass, rdata: impl Into<RdataBuf>, ttl: Duration, now: I, cache_flush: bool, ) -> Result<Option<usize>, P::Error>

Insert (or update / remove) a record observation.

Semantics:

  • If ttl == 0, treat as “record going away” (RFC 6762 §10.1): clamp the matching (name, rtype, rclass, rdata) entry’s expires_at to one second out (rescue window, never extending a sooner expiry) and return Ok(None) — NOT an immediate delete.
  • If cache_flush == true (RFC 6762 §10.2), DEFER eviction by 1 second: clamp the expires_at of every existing sibling matching (name, rtype, rclass) (and not the new rdata) to min(current, now + 1s). This gives a refresh burst time to re-announce missing siblings before they disappear from the cache.
  • If a matching (name, rtype, rclass, rdata) entry already exists, refresh its expiration in place and return Ok(Some(key)) (deduplication).
  • Otherwise insert a new entry. If the pool is full, evict the soonest-expiring entry first (best-effort) then retry. If the retry still fails the error is propagated.

cache identity is (name, rtype, rclass, rdata). A non-IN class record cannot dedupe with, evict, or count as an IN record.

Source

pub fn sweep_expired(&mut self, now: I) -> usize

Sweep expired entries, returning how many were removed.

Source

pub fn next_expiration(&self) -> Option<I>

Next deadline (soonest expiration), if any.

Source

pub fn contains( &self, name: &Name, rtype: ResourceType, rclass: ResourceClass, ) -> bool

Look up whether the cache contains a record matching (name, rtype, rclass). class is part of the cache key.

Source

pub fn count_matching( &self, name: &Name, rtype: ResourceType, rclass: ResourceClass, ) -> usize

Count the number of cached entries matching (name, rtype, rclass).

Multiple distinct records can share (name, rtype, rclass) (e.g. a multi-homed host with several A records), so a single contains check cannot tell you whether the full RRSet landed. Use this for RRSet-coherency checks.

Trait Implementations§

Source§

impl<I, P> Default for Cache<I, P>
where I: Instant, P: Pool<CacheEntry<I>>,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<I, P> Freeze for Cache<I, P>
where P: Freeze,

§

impl<I, P> RefUnwindSafe for Cache<I, P>

§

impl<I, P> Send for Cache<I, P>
where P: Send, I: Send,

§

impl<I, P> Sync for Cache<I, P>
where P: Sync, I: Sync,

§

impl<I, P> Unpin for Cache<I, P>
where P: Unpin, I: Unpin,

§

impl<I, P> UnsafeUnpin for Cache<I, P>
where P: UnsafeUnpin,

§

impl<I, P> UnwindSafe for Cache<I, P>
where P: UnwindSafe, I: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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