cache_mod/lib.rs
1//! # cache-mod
2//!
3//! HIGH-PERFORMANCE IN-PROCESS CACHING
4//!
5//! Multiple eviction policies (LRU, LFU, TinyLFU, TTL, size-bounded). Async-safe,
6//! lock-minimized internals. Typed key-value API. No external store dependency.
7//!
8//! # Status
9//!
10//! The public API surface is feature-complete: the [`Cache`] trait, the
11//! [`CacheError`] error type, and five reference cache implementations —
12//! [`LruCache`] (Least-Recently-Used), [`LfuCache`] (Least-Frequently-Used),
13//! [`TtlCache`] (Time-To-Live, lazy expiry), [`TinyLfuCache`] (Count-Min Sketch
14//! admission filter + LRU main), and [`SizedCache`] (byte-bound capacity).
15//! Internals are arena-backed (0.6.0) and sharded for concurrent throughput
16//! (0.7.0). The API is currently in the 0.9.x hardening cycle; it is **not**
17//! yet frozen, but no further breaking changes are planned before the 1.0
18//! release. Pin exact versions until 1.0 is tagged.
19//!
20//! # Guarantees
21//!
22//! - **No `unsafe`.** This crate contains zero `unsafe` blocks. Every cache
23//! operation goes through safe abstractions only.
24//! - **No `panic!`, no `unwrap`, no `expect`.** The library code never calls
25//! any of these macros / methods on shipping paths. Where a non-panicking
26//! `unreachable!()` appears, it documents an invariant the caller is
27//! responsible for upholding inside `pub(crate)` arena helpers; every
28//! public method is panic-free given valid arguments. `clippy::unwrap_used`
29//! and `clippy::expect_used` are denied at the crate level.
30//! - **No background threads.** Every cache type uses lazy bookkeeping —
31//! eviction, expiry, and sketch aging run on the call-site thread that
32//! triggered them. No `std::thread::spawn` happens inside the crate.
33//! - **No required runtime.** The crate has no async runtime dependency.
34//! `&self` everywhere lets you share cache instances across `.await`
35//! points if you do use one.
36//! - **Send + Sync.** Every cache type is `Send + Sync` when `K: Send + Sync`
37//! and `V: Send + Sync`.
38//!
39//! # Quick start
40//!
41//! ```
42//! use cache_mod::{Cache, LruCache};
43//!
44//! let cache: LruCache<&'static str, u32> = LruCache::new(64).expect("capacity > 0");
45//!
46//! cache.insert("requests", 1);
47//! cache.insert("errors", 0);
48//!
49//! assert_eq!(cache.get(&"requests"), Some(1));
50//! assert_eq!(cache.len(), 2);
51//! ```
52//!
53//! # License
54//!
55//! Dual-licensed under Apache-2.0 OR MIT.
56
57#![doc(html_root_url = "https://docs.rs/cache-mod")]
58#![cfg_attr(docsrs, feature(doc_cfg))]
59#![cfg_attr(not(feature = "std"), no_std)]
60#![deny(missing_docs)]
61#![deny(unsafe_op_in_unsafe_fn)]
62#![deny(unused_must_use)]
63#![deny(unused_results)]
64#![deny(clippy::unwrap_used)]
65#![deny(clippy::expect_used)]
66#![deny(clippy::todo)]
67#![deny(clippy::unimplemented)]
68#![deny(clippy::print_stdout)]
69#![deny(clippy::print_stderr)]
70#![deny(clippy::dbg_macro)]
71#![deny(clippy::undocumented_unsafe_blocks)]
72#![deny(clippy::missing_safety_doc)]
73
74mod cache;
75mod error;
76
77#[cfg(feature = "std")]
78mod lfu;
79#[cfg(feature = "std")]
80mod lru;
81#[cfg(feature = "std")]
82mod sharding;
83#[cfg(feature = "std")]
84mod sized;
85#[cfg(feature = "std")]
86mod tinylfu;
87#[cfg(feature = "std")]
88mod ttl;
89#[cfg(feature = "std")]
90mod util;
91
92pub use cache::Cache;
93pub use error::CacheError;
94
95#[cfg(feature = "std")]
96pub use lfu::LfuCache;
97#[cfg(feature = "std")]
98pub use lru::LruCache;
99#[cfg(feature = "std")]
100pub use sized::SizedCache;
101#[cfg(feature = "std")]
102pub use tinylfu::TinyLfuCache;
103#[cfg(feature = "std")]
104pub use ttl::TtlCache;
105
106/// Crate version string, populated by Cargo at build time.
107pub const VERSION: &str = env!("CARGO_PKG_VERSION");