Skip to main content

CacheEntry

Struct CacheEntry 

Source
pub struct CacheEntry<K, V, M = ()> {
    pub key: K,
    pub value: V,
    pub metadata: CacheMetadata<M>,
}
Expand description

Unified cache entry holding key, value, and metadata.

The M parameter allows each algorithm to store its own metadata without affecting the core entry structure. Use () for algorithms that don’t need extra per-entry metadata (e.g., LRU).

§Examples

use cache_rs::entry::CacheEntry;

// Create a simple entry for count-based caching
let entry: CacheEntry<&str, i32> = CacheEntry::new("key", 42, 1);
assert_eq!(entry.key, "key");
assert_eq!(entry.value, 42);
assert_eq!(entry.metadata.size, 1);

Fields§

§key: K

The cached key

§value: V

The cached value (or reference/handle to external storage)

§metadata: CacheMetadata<M>

Cache metadata including size, timestamps, and algorithm-specific data

Implementations§

Source§

impl<K, V, M: Default> CacheEntry<K, V, M>

Source

pub fn new(key: K, value: V, size: u64) -> Self

Creates a new cache entry without algorithm-specific metadata.

Use this constructor for algorithms like LRU that don’t need per-entry metadata beyond the basic key, value, and timestamps.

§Arguments
  • key - The cache key
  • value - The cached value
  • size - Size of the content this entry represents (use 1 for count-based caches)
§Examples
use cache_rs::entry::CacheEntry;

// Count-based cache entry
let entry: CacheEntry<&str, String> = CacheEntry::new("user:123", "Alice".to_string(), 1);

// Size-aware cache entry
let data = vec![0u8; 1024];
let entry: CacheEntry<&str, Vec<u8>> = CacheEntry::new("file.bin", data, 1024);
Source§

impl<K, V, M> CacheEntry<K, V, M>

Source

pub fn with_algorithm_metadata( key: K, value: V, size: u64, algorithm_meta: M, ) -> Self

Creates a new cache entry with algorithm-specific metadata.

Use this constructor for algorithms like LFU, LFUDA, SLRU, or GDSF that need to track additional per-entry state.

§Arguments
  • key - The cache key
  • value - The cached value
  • size - Size of the content this entry represents (use 1 for count-based caches)
  • algorithm_meta - Algorithm-specific metadata
§Examples
use cache_rs::entry::CacheEntry;
use cache_rs::lfu::LfuMeta;

let entry = CacheEntry::with_algorithm_metadata(
    "key".to_string(),
    vec![1, 2, 3],
    3,
    LfuMeta { frequency: 0 },
);
assert_eq!(entry.metadata.algorithm.frequency, 0);
Source

pub fn touch(&mut self)

Updates the last_accessed timestamp to the current time.

Source

pub fn age_nanos(&self) -> u64

Gets the age of this entry in nanoseconds.

Source

pub fn idle_nanos(&self) -> u64

Gets the time since last access in nanoseconds.

Source

pub fn size(&self) -> u64

Returns the size of the cached content.

Trait Implementations§

Source§

impl<K: Clone, V: Clone, M: Clone> Clone for CacheEntry<K, V, M>

Source§

fn clone(&self) -> Self

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V, M> Freeze for CacheEntry<K, V, M>
where K: Freeze, V: Freeze, M: Freeze,

§

impl<K, V, M> RefUnwindSafe for CacheEntry<K, V, M>

§

impl<K, V, M> Send for CacheEntry<K, V, M>
where K: Send, V: Send, M: Send,

§

impl<K, V, M> Sync for CacheEntry<K, V, M>
where K: Sync, V: Sync, M: Sync,

§

impl<K, V, M> Unpin for CacheEntry<K, V, M>
where K: Unpin, V: Unpin, M: Unpin,

§

impl<K, V, M> UnsafeUnpin for CacheEntry<K, V, M>

§

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