dns_resolver::cache

Struct PartitionedCache

Source
pub struct PartitionedCache<K1: Eq + Hash, K2: Eq + Hash, V> {
    partitions: HashMap<K1, Partition<K2, V>>,
    access_priority: PriorityQueue<K1, Reverse<Instant>>,
    expiry_priority: PriorityQueue<K1, Reverse<Instant>>,
    current_size: usize,
    desired_size: usize,
}

Fields§

§partitions: HashMap<K1, Partition<K2, V>>

Cached entries, indexed by partition key.

§access_priority: PriorityQueue<K1, Reverse<Instant>>

Priority queue of partition keys ordered by access times.

When the cache is full and there are no expired records to prune, partitions will instead be pruned in LRU order.

INVARIANT: the keys in here are exactly the keys in partitions.

§expiry_priority: PriorityQueue<K1, Reverse<Instant>>

Priority queue of partition keys ordered by expiry time.

When the cache is pruned, expired records are removed first.

INVARIANT: the keys in here are exactly the keys in partitions.

§current_size: usize

The number of records in the cache, across all partitions.

INVARIANT: this is the sum of the size fields of the partitions.

§desired_size: usize

The desired maximum number of records in the cache.

Implementations§

Source§

impl<K1: Clone + Eq + Hash, K2: Copy + Eq + Hash, V: PartialEq> PartitionedCache<K1, K2, V>

Source

pub fn new() -> Self

Create a new cache with a default desired size.

Source

pub fn with_desired_size(desired_size: usize) -> Self

Create a new cache with the given desired size.

The prune method will remove expired records, and also enough records (in least-recently-used order) to get down to this size.

Source

pub fn get_partition_without_checking_expiration( &mut self, partition_key: &K1, ) -> Option<&HashMap<K2, Vec<(V, Instant)>>>

Get all records for the given partition key from the cache, along with their expiration times.

These records may have expired if prune has not been called recently.

Source

pub fn get_without_checking_expiration( &mut self, partition_key: &K1, record_key: &K2, ) -> Option<&[(V, Instant)]>

Get all records for the given partition and record key from the cache, along with their expiration times.

These records may have expired if prune has not been called recently.

Source

pub fn upsert( &mut self, partition_key: K1, record_key: K2, value: V, ttl: Duration, )

Insert a record into the cache, or reset the expiry time if already present.

Source

pub fn remove_expired(&mut self) -> usize

Delete all expired records.

Returns the number of records deleted.

Source

pub fn prune(&mut self) -> (bool, usize, usize, usize)

Delete all expired records, and then enough least-recently-used records to reduce the cache to the desired size.

Returns (has overflowed?, current size, num expired, num pruned).

Source

fn remove_expired_step(&mut self) -> usize

Helper for remove_expired: looks at the next-to-expire domain and cleans up expired records from it. This may delete more than one record, and may even delete the whole domain.

Returns the number of records removed.

Source

fn remove_least_recently_used(&mut self) -> usize

Helper for prune: deletes all records associated with the least recently used domain.

Returns the number of records removed.

Trait Implementations§

Source§

impl<K1: Clone + Eq + Hash, K2: Clone + Eq + Hash, V: Clone> Clone for PartitionedCache<K1, K2, V>

Source§

fn clone(&self) -> PartitionedCache<K1, K2, V>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K1: Debug + Eq + Hash, K2: Debug + Eq + Hash, V: Debug> Debug for PartitionedCache<K1, K2, V>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K1: Clone + Eq + Hash, K2: Copy + Eq + Hash, V: PartialEq> Default for PartitionedCache<K1, K2, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K1, K2, V> Freeze for PartitionedCache<K1, K2, V>

§

impl<K1, K2, V> RefUnwindSafe for PartitionedCache<K1, K2, V>

§

impl<K1, K2, V> Send for PartitionedCache<K1, K2, V>
where K1: Send, K2: Send, V: Send,

§

impl<K1, K2, V> Sync for PartitionedCache<K1, K2, V>
where K1: Sync, K2: Sync, V: Sync,

§

impl<K1, K2, V> Unpin for PartitionedCache<K1, K2, V>
where K1: Unpin, K2: Unpin, V: Unpin,

§

impl<K1, K2, V> UnwindSafe for PartitionedCache<K1, K2, V>
where K1: UnwindSafe, K2: UnwindSafe, V: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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