aa_cache/cached_value.rs
1//! [`CachedValue`] — a cache entry tagged with its insertion instant.
2
3use std::time::{Duration, Instant};
4
5/// A value stored in the L1 cache together with the [`Instant`] it was inserted.
6///
7/// The insertion time drives TTL expiry: an entry older than the cache's
8/// configured TTL is treated as a miss, forcing a reload from the wrapped store.
9#[derive(Debug, Clone)]
10pub struct CachedValue<V> {
11 /// The cached value.
12 pub value: V,
13 /// When this entry was inserted.
14 pub inserted_at: Instant,
15}
16
17impl<V> CachedValue<V> {
18 /// Wrap `value`, stamping it with the current instant.
19 #[must_use]
20 pub fn new(value: V) -> Self {
21 Self {
22 value,
23 inserted_at: Instant::now(),
24 }
25 }
26
27 /// Return `true` once the entry's age has reached or exceeded `ttl`.
28 #[must_use]
29 pub fn is_expired(&self, ttl: Duration) -> bool {
30 self.inserted_at.elapsed() >= ttl
31 }
32}