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 valueinserted_at- TheInstantwhen this entry was createdfrequency- 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: u64Implementations§
Source§impl<R> CacheEntry<R>
impl<R> CacheEntry<R>
Sourcepub fn is_expired(&self, ttl: Option<u64>) -> bool
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.Nonemeans no expiration.
§Returns
trueif the entry age exceeds the TTLfalseif TTL isNoneor 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));Sourcepub fn increment_frequency(&mut self)
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>
impl<R: Clone> Clone for CacheEntry<R>
Source§fn clone(&self) -> CacheEntry<R>
fn clone(&self) -> CacheEntry<R>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<R: MemoryEstimator> MemoryEstimator for CacheEntry<R>
impl<R: MemoryEstimator> MemoryEstimator for CacheEntry<R>
Source§fn estimate_memory(&self) -> usize
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> 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
Mutably borrows from an owned value. Read more