Skip to main content

oxistore_cache/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(missing_docs)]
3
4//! `oxistore-cache` — Pure-Rust LRU, ARC, LFU, and W-TinyLFU eviction primitives.
5//!
6//! This crate provides four cache implementations behind a unified [`Cache`]
7//! trait:
8//!
9//! - [`LruCache`] — classic Least-Recently-Used cache backed by a
10//!   `hashlink::LinkedHashMap`.  O(1) amortised operations.
11//!
12//! - [`ArcCache`] — Adaptive Replacement Cache (Megiddo & Modha, FAST'03),
13//!   which balances recency and frequency by maintaining four lists (T1, T2,
14//!   B1, B2) and an adaptive target `p`.  ARC is scan-resistant.
15//!
16//! - [`LfuCache`] — Least-Frequently-Used cache with O(1) operations using
17//!   the Shah, Mitra & Matani (2010) algorithm.
18//!
19//! - [`WTinyLfuCache`] — Window TinyLFU with Count-Min Sketch frequency
20//!   estimation and a doorkeeper bloom filter, providing near-optimal hit rates
21//!   on skewed (Zipfian) workloads.
22//!
23//! All cache implementations support per-entry TTL (time-to-live) via the
24//! [`Cache::put_with_ttl`] method.  Expiry is checked lazily on access.
25//!
26//! # Example
27//!
28//! ```rust
29//! use oxistore_cache::{LruCache, ArcCache, LfuCache, WTinyLfuCache, Cache};
30//!
31//! let mut lru = LruCache::new(3);
32//! lru.put(1u32, "a");
33//! lru.put(2u32, "b");
34//! lru.put(3u32, "c");
35//! lru.put(4u32, "d"); // evicts 1 (LRU)
36//! assert!(lru.get(&1u32).is_none());
37//! assert_eq!(lru.get(&2u32), Some(&"b"));
38//!
39//! let mut arc = ArcCache::new(3);
40//! arc.put(1u32, "a");
41//! arc.put(2u32, "b");
42//! arc.put(3u32, "c");
43//!
44//! let mut lfu = LfuCache::new(3);
45//! lfu.put(1u32, "a");
46//! lfu.put(2u32, "b");
47//! lfu.put(3u32, "c");
48//!
49//! let mut wtlfu = WTinyLfuCache::new(3);
50//! wtlfu.put(1u32, "a");
51//! wtlfu.put(2u32, "b");
52//! wtlfu.put(3u32, "c");
53//! ```
54
55/// Adaptive Replacement Cache (ARC) — balances recency and frequency.
56pub mod arc;
57/// Bounded-memory cache wrapper — enforces a hard byte-budget cap.
58pub mod bounded;
59/// Cache builder — ergonomic constructor for all cache policies.
60pub mod builder;
61/// Least-Frequently-Used (LFU) cache — O(1) frequency-based eviction.
62pub mod lfu;
63/// Least-Recently-Used (LRU) cache — evicts the least recently accessed entry.
64pub mod lru;
65/// Sharded concurrent cache — N LRU shards behind a Mutex for low contention.
66pub mod sharded;
67/// Count-Min Sketch and Doorkeeper bloom filter for W-TinyLFU.
68pub mod sketch;
69/// Cache hit/miss statistics tracking.
70pub mod stats;
71/// Thread-safe cache wrapper — wraps any `Cache` impl behind a `Mutex`.
72pub mod sync;
73/// Window TinyLFU — state-of-the-art admission policy with CMS frequency estimator.
74pub mod tinylfu;
75/// Write-through and write-back cache adapters.
76pub mod write_adapter;
77
78/// Caching wrapper for `BlobStore` backends (see `oxistore_blob::BlobStore`).
79#[cfg(feature = "blob")]
80pub mod blob_cache;
81
82/// Caching adapter for hot Parquet row groups from `oxistore-columnar`.
83#[cfg(feature = "columnar")]
84pub mod columnar_cache;
85
86/// SQL query-result and prepared-plan caches backed by `oxisql-core` types.
87#[cfg(feature = "sql")]
88pub mod sql_cache;
89
90pub use arc::ArcCache;
91pub use bounded::BoundedCache;
92pub use builder::{CacheBuilder, CachePolicy};
93pub use lfu::LfuCache;
94pub use lru::LruCache;
95pub use sharded::ShardedCache;
96pub use stats::{CacheStats, StatsCache};
97pub use sync::SyncCache;
98pub use tinylfu::WTinyLfuCache;
99pub use write_adapter::{CacheableKvStore, WriteBackCache, WriteThroughCache};
100
101#[cfg(feature = "blob")]
102pub use blob_cache::BlobCache;
103
104#[cfg(feature = "columnar")]
105pub use columnar_cache::ColumnarRowGroupCache;
106
107#[cfg(feature = "sql")]
108pub use sql_cache::{CachedQueryRunner, SqlPlanCache, SqlQueryCache};
109
110/// A cache entry that optionally expires at a given instant.
111///
112/// Used internally by all cache implementations to support per-entry TTL.
113/// Callers interact with the value type `V` via the [`Cache`] trait methods;
114/// `CacheEntry` is exposed for advanced use cases (e.g. custom wrappers).
115pub struct CacheEntry<V> {
116    /// The stored value.
117    pub value: V,
118    /// Optional expiry time.  `None` means the entry never expires.
119    pub expires_at: Option<std::time::Instant>,
120}
121
122impl<V> CacheEntry<V> {
123    /// Create a non-expiring entry.
124    #[must_use]
125    pub fn new(value: V) -> Self {
126        CacheEntry {
127            value,
128            expires_at: None,
129        }
130    }
131
132    /// Create an entry that expires after `ttl` from now.
133    #[must_use]
134    pub fn with_ttl(value: V, ttl: std::time::Duration) -> Self {
135        CacheEntry {
136            value,
137            expires_at: Some(std::time::Instant::now() + ttl),
138        }
139    }
140
141    /// Return `true` if this entry has expired (i.e. its deadline has passed).
142    #[must_use]
143    pub fn is_expired(&self) -> bool {
144        match self.expires_at {
145            Some(deadline) => std::time::Instant::now() >= deadline,
146            None => false,
147        }
148    }
149}
150
151/// Unified interface for key-value caches with bounded capacity.
152///
153/// All four implementations ([`LruCache`], [`ArcCache`], [`LfuCache`],
154/// [`WTinyLfuCache`]) implement this trait, allowing callers to write generic
155/// code against the cache interface.
156pub trait Cache<K, V> {
157    /// Look up `key`, returning a reference to the value if present.
158    ///
159    /// Implementations update internal bookkeeping (e.g. promoting the entry
160    /// to MRU or incrementing frequency counts) as a side effect.
161    ///
162    /// If the entry exists but has expired (TTL), it is removed and `None` is
163    /// returned without updating recency or frequency.
164    fn get(&mut self, key: &K) -> Option<&V>;
165
166    /// Insert or update `key` -> `value` without a TTL.
167    ///
168    /// If inserting a new key would exceed the cache's capacity, the
169    /// implementation evicts one entry (per its policy) and returns the
170    /// evicted value.  On a key update, the old value is not returned.
171    fn put(&mut self, key: K, value: V) -> Option<V>;
172
173    /// Insert or update `key` -> `value` with a time-to-live.
174    ///
175    /// After `ttl` has elapsed, any access to `key` will treat it as a miss
176    /// and remove the entry lazily.
177    ///
178    /// Concrete implementations in this crate override this to actually store
179    /// the expiry.  External impls that don't need TTL may rely on the default
180    /// which falls back to a plain `put` (no expiry).
181    fn put_with_ttl(&mut self, key: K, value: V, ttl: std::time::Duration) -> Option<V> {
182        let _ = ttl;
183        self.put(key, value)
184    }
185
186    /// Return the number of live entries currently in the cache.
187    fn len(&self) -> usize;
188
189    /// Return the maximum number of entries the cache can hold.
190    fn cap(&self) -> usize;
191
192    /// Return `true` if the cache holds no entries.
193    fn is_empty(&self) -> bool {
194        self.len() == 0
195    }
196
197    /// Remove the entry associated with `key`, returning its value if present.
198    ///
199    /// Unlike eviction, this is an explicit removal requested by the caller.
200    fn remove(&mut self, key: &K) -> Option<V>;
201
202    /// Remove all entries from the cache.
203    fn clear(&mut self);
204
205    /// Look up `key` without updating access metadata (no promotion).
206    ///
207    /// If the entry has expired (TTL), it is removed and `None` is returned.
208    /// Returns `None` if the key is not present or has expired.
209    fn peek(&self, key: &K) -> Option<&V>;
210
211    /// Return `true` if `key` is present in the cache (without promotion).
212    ///
213    /// Expired entries are treated as absent.
214    fn contains_key(&self, key: &K) -> bool;
215
216    /// Dynamically resize the cache capacity.
217    ///
218    /// If `new_cap` is smaller than the current length, excess entries are
219    /// evicted according to the cache's eviction policy.
220    fn resize(&mut self, new_cap: usize);
221
222    /// Return `&V` for `key`, inserting `default()` if the key is absent.
223    ///
224    /// The closure `default` is called at most once.  If the key is already
225    /// present the closure is never invoked.
226    ///
227    /// Implementations may override this for efficiency (e.g. to avoid a
228    /// second hash lookup).  The default implementation uses [`Cache::peek`]
229    /// after ensuring the key exists.
230    fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &V
231    where
232        K: Clone,
233    {
234        if !self.contains_key(&key) {
235            let v = default();
236            self.put(key.clone(), v);
237        }
238        // peek is &self and won't panic since we just ensured the key exists.
239        self.peek(&key).expect("key was just inserted")
240    }
241
242    /// Return all live (non-expired) values currently stored in the cache.
243    ///
244    /// The default implementation returns an empty `Vec`.  Concrete
245    /// implementations override this to return actual values.
246    fn values(&self) -> Vec<&V> {
247        Vec::new()
248    }
249
250    /// Pre-populate the cache with `(key, value)` pairs from an iterator.
251    ///
252    /// Each pair is inserted via [`Cache::put`], respecting the cache's
253    /// eviction policy.  Pairs that exceed capacity cause earlier entries to
254    /// be evicted according to the policy.
255    fn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>) {
256        for (k, v) in iter {
257            self.put(k, v);
258        }
259    }
260}
261
262// ── Async helper ─────────────────────────────────────────────────────────────
263
264/// Look up `key` in `cache`, returning the cached value if present.
265///
266/// If the key is absent, the async closure `loader` is awaited to produce
267/// a value, which is then inserted into the cache and returned.
268///
269/// The closure is invoked **at most once** per call.  If the key is already
270/// present it is never called.
271///
272/// # Example
273///
274/// ```rust
275/// use oxistore_cache::{LruCache, Cache, get_or_insert_async};
276/// use std::sync::Mutex;
277///
278/// # async fn example() {
279/// let cache = Mutex::new(LruCache::<u32, String>::new(4));
280/// let val = get_or_insert_async(
281///     &cache,
282///     42u32,
283///     || async { "computed".to_string() },
284/// ).await;
285/// assert_eq!(val, "computed");
286/// # }
287/// ```
288pub async fn get_or_insert_async<K, V, C, F, Fut>(
289    cache: &std::sync::Mutex<C>,
290    key: K,
291    loader: F,
292) -> V
293where
294    K: Eq + std::hash::Hash + Clone,
295    V: Clone,
296    C: Cache<K, V>,
297    F: FnOnce() -> Fut,
298    Fut: std::future::Future<Output = V>,
299{
300    // Fast path: key already in cache.
301    {
302        let mut guard = cache.lock().expect("cache mutex poisoned");
303        if let Some(v) = guard.get(&key) {
304            return v.clone();
305        }
306    }
307    // Slow path: key absent — await the loader outside the lock.
308    let value = loader().await;
309    {
310        let mut guard = cache.lock().expect("cache mutex poisoned");
311        // Double-check in case another task inserted the key while we waited.
312        if let Some(existing) = guard.peek(&key) {
313            return existing.clone();
314        }
315        guard.put(key, value.clone());
316    }
317    value
318}
319
320/// Look up `key` in a `tokio::sync::Mutex`-wrapped cache asynchronously.
321///
322/// Identical semantics to [`get_or_insert_async`] but uses an async
323/// `tokio::sync::Mutex` instead of `std::sync::Mutex`.
324///
325/// This variant is suitable when the cache is shared across `tokio` tasks
326/// that run on a multi-threaded executor and cannot use a synchronous lock.
327///
328/// Requires the `async-helpers` feature flag.
329#[cfg(feature = "async-helpers")]
330pub async fn get_or_insert_async_tokio<K, V, C, F, Fut>(
331    cache: &tokio::sync::Mutex<C>,
332    key: K,
333    loader: F,
334) -> V
335where
336    K: Eq + std::hash::Hash + Clone,
337    V: Clone,
338    C: Cache<K, V>,
339    F: FnOnce() -> Fut,
340    Fut: std::future::Future<Output = V>,
341{
342    // Fast path: key already in cache (async lock, no blocking).
343    {
344        let mut guard = cache.lock().await;
345        if let Some(v) = guard.get(&key) {
346            return v.clone();
347        }
348    }
349    // Slow path: await the loader without holding the lock.
350    let value = loader().await;
351    {
352        let mut guard = cache.lock().await;
353        if let Some(existing) = guard.peek(&key) {
354            return existing.clone();
355        }
356        guard.put(key, value.clone());
357    }
358    value
359}