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: CVThe cached value. Unlike the creation time and TTL, it can be taken out or changed in place.
Implementations§
Source§impl<CV> CacheEntry<CV>
impl<CV> CacheEntry<CV>
Sourcepub fn new(
value: CV,
created_at: SystemTime,
ttl: Option<Duration>,
) -> CacheEntry<CV>
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.
Sourcepub fn with_expires_at(self, expires_at: Option<SystemTime>) -> CacheEntry<CV>
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.
Sourcepub fn created_at(&self) -> SystemTime
pub fn created_at(&self) -> SystemTime
When the value was written. Copying the entry to another store keeps it.
Sourcepub fn ttl(&self) -> Option<Duration>
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.
Sourcepub fn expires_at(&self) -> Option<SystemTime>
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.
Sourcepub fn remaining_ttl(&self, now: SystemTime) -> Option<Duration>
pub fn remaining_ttl(&self, now: SystemTime) -> Option<Duration>
The time left before the entry expires, or None if it never does.
Sourcepub fn is_expired(&self, now: SystemTime) -> bool
pub fn is_expired(&self, now: SystemTime) -> bool
Whether now is at or past expires_at. An entry without one never
expires.
Sourcepub fn map<U>(self, convert: impl FnOnce(CV) -> U) -> CacheEntry<U>
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,
impl<CV> Clone for CacheEntry<CV>where
CV: Clone,
Source§fn clone(&self) -> CacheEntry<CV>
fn clone(&self) -> CacheEntry<CV>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more