Skip to main content

CacheEntry

Struct CacheEntry 

Source
pub struct CacheEntry<CV> {
    pub value: CV,
    /* private fields */
}
Expand description

A value with its creation time and TTL. Every store, memory, Redis and hybrid, reads and writes this one type, so get_entry from one store and set_entry into another copies an entry as it is. Its creation time and TTL can’t be changed; build a new entry instead.

use std::time::{Duration, SystemTime};
use sisa_caching_core::CacheEntry;

let created_at = SystemTime::UNIX_EPOCH;
let entry = CacheEntry::new("Ada", created_at, Some(Duration::from_secs(60)));

assert_eq!(entry.expires_at(), Some(created_at + Duration::from_secs(60)));
assert!(!entry.is_expired(created_at + Duration::from_secs(59)));
assert!(entry.is_expired(created_at + Duration::from_secs(60)));

The TTL can’t be changed in place:

ⓘ
let mut entry = CacheEntry::new(7, SystemTime::UNIX_EPOCH, None);
entry.ttl = Some(Duration::from_secs(10));

Fields§

§value: CV

The cached value. Unlike the creation time and TTL, it can be taken out or changed in place.

Implementations§

Source§

impl<CV> CacheEntry<CV>

Source

pub fn new( value: CV, created_at: SystemTime, ttl: Option<Duration>, ) -> CacheEntry<CV>

An entry that expires ttl after created_at, or never with None or a TTL so long the deadline can’t be represented.

Source

pub fn with_expires_at(self, expires_at: Option<SystemTime>) -> CacheEntry<CV>

Replaces the expiry derived from created_at + ttl, for a store whose server tracks the deadline itself. None means the entry no longer expires.

Source

pub fn created_at(&self) -> SystemTime

When the value was written. Copying the entry to another store keeps it.

Source

pub fn ttl(&self) -> Option<Duration>

The TTL the entry was written with, or None if it had none. Refresh-ahead counts from it; when the entry expires is expires_at, which a store’s server can set without a TTL.

Source

pub fn expires_at(&self) -> Option<SystemTime>

When the entry expires: created_at + ttl, or the deadline a store’s server keeps, as with_expires_at sets. None if it never expires.

Source

pub fn remaining_ttl(&self, now: SystemTime) -> Option<Duration>

The time left before the entry expires, or None if it never does.

Source

pub fn is_expired(&self, now: SystemTime) -> bool

Whether now is at or past expires_at. An entry without one never expires.

Source

pub fn map<U>(self, convert: impl FnOnce(CV) -> U) -> CacheEntry<U>

The same entry around another value, keeping its creation time, TTL and expiry.

Trait Implementations§

Source§

impl<CV> Clone for CacheEntry<CV>
where CV: Clone,

Source§

fn clone(&self) -> CacheEntry<CV>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<CV> Debug for CacheEntry<CV>
where CV: Debug,

Source§

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

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

impl<CV> PartialEq for CacheEntry<CV>
where CV: PartialEq,

Source§

fn eq(&self, other: &CacheEntry<CV>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<CV> StructuralPartialEq for CacheEntry<CV>
where CV: PartialEq,

Auto Trait Implementations§

§

impl<CV> Freeze for CacheEntry<CV>
where CV: Freeze,

§

impl<CV> RefUnwindSafe for CacheEntry<CV>
where CV: RefUnwindSafe,

§

impl<CV> Send for CacheEntry<CV>
where CV: Send,

§

impl<CV> Sync for CacheEntry<CV>
where CV: Sync,

§

impl<CV> Unpin for CacheEntry<CV>
where CV: Unpin,

§

impl<CV> UnsafeUnpin for CacheEntry<CV>
where CV: UnsafeUnpin,

§

impl<CV> UnwindSafe for CacheEntry<CV>
where CV: 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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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 = !

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