CacheEntry

Struct CacheEntry 

Source
pub struct CacheEntry<R> {
    pub value: R,
    pub inserted_at: Instant,
    pub frequency: u64,
}
Expand description

Internal wrapper that tracks when a value was inserted into the cache. Used for TTL expiration support.

This structure is used internally to support TTL (Time To Live) expiration. Each cached value is wrapped in a CacheEntry which records the insertion timestamp using Instant::now().

§Type Parameters

  • R - The type of the cached value

§Fields

  • value - The actual cached value
  • inserted_at - The Instant when this entry was created
  • frequency - The number of times this entry has been accessed (for LFU policy)

§Examples

use cachelito_core::CacheEntry;

let entry = CacheEntry::new(42);
assert_eq!(entry.value, 42);
assert_eq!(entry.frequency, 0);

// Check if expired (TTL of 60 seconds)
assert!(!entry.is_expired(Some(60)));

Fields§

§value: R§inserted_at: Instant§frequency: u64

Implementations§

Source§

impl<R> CacheEntry<R>

Source

pub fn new(value: R) -> Self

Creates a new cache entry with the current timestamp.

§Arguments
  • value - The value to cache
§Returns

A new CacheEntry with inserted_at set to Instant::now() and frequency set to 0

Source

pub fn is_expired(&self, ttl: Option<u64>) -> bool

Returns true if the entry has expired based on the provided TTL.

§Arguments
  • ttl - Optional time-to-live in seconds. None means no expiration.
§Returns
  • true if the entry age exceeds the TTL
  • false if TTL is None or the entry is still valid
§Examples
use cachelito_core::CacheEntry;
use std::thread;
use std::time::Duration;

let entry = CacheEntry::new("data");

// Fresh entry is not expired
assert!(!entry.is_expired(Some(1)));

// Wait 2 seconds
thread::sleep(Duration::from_secs(2));

// Now it's expired (TTL was 1 second)
assert!(entry.is_expired(Some(1)));

// No TTL means never expires
assert!(!entry.is_expired(None));
Source

pub fn increment_frequency(&mut self)

Increments the access frequency counter.

This method is used by the LFU (Least Frequently Used) eviction policy to track how many times an entry has been accessed.

§Examples
use cachelito_core::CacheEntry;

let mut entry = CacheEntry::new(42);
assert_eq!(entry.frequency, 0);

entry.increment_frequency();
assert_eq!(entry.frequency, 1);

entry.increment_frequency();
assert_eq!(entry.frequency, 2);

Trait Implementations§

Source§

impl<R: Clone> Clone for CacheEntry<R>

Source§

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

Returns a duplicate 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<R: MemoryEstimator> MemoryEstimator for CacheEntry<R>

Source§

fn estimate_memory(&self) -> usize

Estimates the total memory size of this value in bytes. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<R> UnwindSafe for CacheEntry<R>
where R: 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, 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.