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//! **Stable.** As of 1.0.0 the public API surface is frozen under strict
11//! SemVer — see `docs/STABILITY.md` for the full enumeration. Five
12//! reference cache implementations sit behind a common [`Cache`] trait:
13//! [`LruCache`] (Least-Recently-Used), [`LfuCache`] (Least-Frequently-Used),
14//! [`TtlCache`] (Time-To-Live, lazy expiry), [`TinyLfuCache`] (Count-Min
15//! Sketch admission filter + LRU main), and [`SizedCache`] (byte-bound
16//! capacity). Internals are arena-backed and sharded for concurrent
17//! throughput; the public surface does not depend on either choice and
18//! will not change as those internals evolve in the 1.x line.
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(warnings)]
61#![deny(missing_docs)]
62#![deny(unsafe_op_in_unsafe_fn)]
63#![deny(unused_must_use)]
64#![deny(unused_results)]
65#![deny(clippy::unwrap_used)]
66#![deny(clippy::expect_used)]
67#![deny(clippy::todo)]
68#![deny(clippy::unimplemented)]
69#![deny(clippy::print_stdout)]
70#![deny(clippy::print_stderr)]
71#![deny(clippy::dbg_macro)]
72#![deny(clippy::undocumented_unsafe_blocks)]
73#![deny(clippy::missing_safety_doc)]
74
75mod cache;
76mod error;
77
78#[cfg(feature = "std")]
79mod lfu;
80#[cfg(feature = "std")]
81mod lru;
82#[cfg(feature = "std")]
83mod sharding;
84#[cfg(feature = "std")]
85mod sized;
86#[cfg(feature = "std")]
87mod tinylfu;
88#[cfg(feature = "std")]
89mod ttl;
90#[cfg(feature = "std")]
91mod util;
92
93pub use cache::Cache;
94pub use error::CacheError;
95
96#[cfg(feature = "std")]
97pub use lfu::LfuCache;
98#[cfg(feature = "std")]
99pub use lru::LruCache;
100#[cfg(feature = "std")]
101pub use sized::SizedCache;
102#[cfg(feature = "std")]
103pub use tinylfu::TinyLfuCache;
104#[cfg(feature = "std")]
105pub use ttl::TtlCache;
106
107/// Crate version string, populated by Cargo at build time.
108pub const VERSION: &str = env!("CARGO_PKG_VERSION");