pub struct CacheEntry<V> { /* private fields */ }Expand description
A cached value with associated metadata.
CacheEntry wraps a value with optional TTL and timestamp information.
The cache system uses this metadata for expiration tracking and telemetry.
§Examples
use std::time::Duration;
use cachet_tier::CacheEntry;
// Simple entry with just a value (no expiration)
let entry = CacheEntry::new(42);
assert_eq!(*entry.value(), 42);
// CacheEntry<V> implements Deref<Target = V>, so you can call
// methods on the inner value directly:
let entry = CacheEntry::new("hello world".to_string());
assert_eq!(entry.len(), 11); // calls String::len() via Deref
assert!(entry.starts_with("hello")); // calls str::starts_with() via Deref
// Entry that expires after a duration from insert time
let entry = CacheEntry::expires_after("data".to_string(), Duration::from_secs(60));
assert_eq!(entry.ttl(), Some(Duration::from_secs(60)));Implementations§
Source§impl<V> CacheEntry<V>
impl<V> CacheEntry<V>
Sourcepub fn new(value: V) -> CacheEntry<V>
pub fn new(value: V) -> CacheEntry<V>
Creates a new cache entry with the given value.
The entry will not expire unless a tier-level TTL is configured.
Sourcepub fn expires_after(value: V, ttl: Duration) -> CacheEntry<V>
pub fn expires_after(value: V, ttl: Duration) -> CacheEntry<V>
Creates a cache entry that expires after the given duration from insert time.
The timestamp will be set by the cache when the entry is inserted. The per-entry TTL takes precedence over any tier-level TTL.
Sourcepub fn expires_at(
value: V,
ttl: Duration,
cached_at: SystemTime,
) -> CacheEntry<V>
pub fn expires_at( value: V, ttl: Duration, cached_at: SystemTime, ) -> CacheEntry<V>
Creates a cache entry that expires at cached_at + ttl.
This is primarily useful for testing expiration logic with controlled timestamps, or if you need to construct entries with pre-determined expiration times.
Sourcepub fn cached_at(&self) -> Option<SystemTime>
pub fn cached_at(&self) -> Option<SystemTime>
Returns the timestamp when this entry was cached.
Returns None if the entry hasn’t been inserted into a cache yet,
or was created without expires_at.
Sourcepub fn ensure_cached_at(&mut self, cached_at: SystemTime)
pub fn ensure_cached_at(&mut self, cached_at: SystemTime)
Sets the cache timestamp if not already set.
Called automatically by the cache during insertion. If the entry
was created with expires_at, the existing
timestamp is preserved.
Sourcepub fn ttl(&self) -> Option<Duration>
pub fn ttl(&self) -> Option<Duration>
Returns the per-entry TTL override.
When set, this takes precedence over any tier-level TTL configured on the cache.
Sourcepub fn set_ttl(&mut self, ttl: Duration)
pub fn set_ttl(&mut self, ttl: Duration)
Sets a per-entry TTL that overrides the tier-level TTL.
Sourcepub fn into_value(self) -> V
pub fn into_value(self) -> V
Consumes the entry and returns the inner value.
Sourcepub fn try_map_value<U, F>(self, f: F) -> Result<CacheEntry<U>, Error>
pub fn try_map_value<U, F>(self, f: F) -> Result<CacheEntry<U>, Error>
Transforms the value while preserving metadata (cached_at, ttl).
§Errors
Forwards the error returned by the input encoding function
Trait Implementations§
Source§impl<V> Clone for CacheEntry<V>where
V: Clone,
impl<V> Clone for CacheEntry<V>where
V: Clone,
Source§fn clone(&self) -> CacheEntry<V>
fn clone(&self) -> CacheEntry<V>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<V> Debug for CacheEntry<V>where
V: Debug,
impl<V> Debug for CacheEntry<V>where
V: Debug,
Source§impl<V> Deref for CacheEntry<V>
impl<V> Deref for CacheEntry<V>
impl<V> Eq for CacheEntry<V>where
V: Eq,
Source§impl<V> From<V> for CacheEntry<V>
impl<V> From<V> for CacheEntry<V>
Source§fn from(value: V) -> CacheEntry<V>
fn from(value: V) -> CacheEntry<V>
Source§impl<V> PartialEq for CacheEntry<V>where
V: PartialEq,
impl<V> PartialEq for CacheEntry<V>where
V: PartialEq,
Source§fn eq(&self, other: &CacheEntry<V>) -> bool
fn eq(&self, other: &CacheEntry<V>) -> bool
self and other values to be equal, and is used by ==.impl<V> StructuralPartialEq for CacheEntry<V>
Auto Trait Implementations§
impl<V> Freeze for CacheEntry<V>where
V: Freeze,
impl<V> RefUnwindSafe for CacheEntry<V>where
V: RefUnwindSafe,
impl<V> Send for CacheEntry<V>where
V: Send,
impl<V> Sync for CacheEntry<V>where
V: Sync,
impl<V> Unpin for CacheEntry<V>where
V: Unpin,
impl<V> UnsafeUnpin for CacheEntry<V>where
V: UnsafeUnpin,
impl<V> UnwindSafe for CacheEntry<V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.