Skip to main content

CacheEntry

Struct CacheEntry 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_ttl(&mut self, ttl: Duration)

Sets a per-entry TTL that overrides the tier-level TTL.

Source

pub fn into_value(self) -> V

Consumes the entry and returns the inner value.

Source

pub fn value(&self) -> &V

Returns a reference to the cached value.

Source

pub fn try_map_value<U, F>(self, f: F) -> Result<CacheEntry<U>, Error>
where F: FnOnce(V) -> Result<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,

Source§

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

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<V> Debug for CacheEntry<V>
where V: Debug,

Source§

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

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

impl<V> Deref for CacheEntry<V>

Source§

type Target = V

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<CacheEntry<V> as Deref>::Target

Dereferences the value.
Source§

impl<V> Eq for CacheEntry<V>
where V: Eq,

Source§

impl<V> From<V> for CacheEntry<V>

Source§

fn from(value: V) -> CacheEntry<V>

Converts to this type from the input type.
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
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