in_memory_cache/
lib.rs

1//! # In-Memory Cache
2//!
3//! A fast, thread-safe, in-memory cache library for Rust with TTL support
4//! and LRU eviction.
5//!
6//! ## Features
7//!
8//! - **Thread-safe**: Share across threads with `Clone` (uses `Arc` internally)
9//! - **TTL support**: Entries can expire after a configurable duration
10//! - **LRU eviction**: Automatic eviction of least-recently-used entries when at capacity
11//! - **Statistics**: Track cache hits, misses, evictions, and more
12//! - **Zero unsafe code**: Built entirely with safe Rust
13//!
14//! ## Quick Start
15//!
16//! ```rust
17//! use in_memory_cache::{Cache, CacheConfig};
18//! use std::time::Duration;
19//!
20//! // Create a cache with configuration
21//! let config = CacheConfig::new()
22//!     .max_capacity(10_000)
23//!     .default_ttl(Duration::from_secs(300))
24//!     .build();
25//!
26//! let cache = Cache::new(config);
27//!
28//! // Store and retrieve values
29//! cache.set("user:123", "Alice");
30//!
31//! if let Some(value) = cache.get("user:123") {
32//!     println!("Found: {:?}", value);
33//! }
34//!
35//! // Set with custom TTL
36//! cache.set_with_ttl("session:abc", "session_data", Duration::from_secs(60));
37//!
38//! // Check statistics
39//! let stats = cache.stats();
40//! println!("Hit rate: {:.1}%", stats.hit_rate);
41//! ```
42//!
43//! ## Thread Safety
44//!
45//! The cache is safe to share across threads. Cloning a `Cache` creates a new
46//! handle to the same underlying data:
47//!
48//! ```rust
49//! use in_memory_cache::Cache;
50//! use std::thread;
51//!
52//! let cache = Cache::default();
53//!
54//! let handles: Vec<_> = (0..4).map(|i| {
55//!     let cache = cache.clone();
56//!     thread::spawn(move || {
57//!         cache.set(format!("key_{}", i), format!("value_{}", i));
58//!     })
59//! }).collect();
60//!
61//! for handle in handles {
62//!     handle.join().unwrap();
63//! }
64//! ```
65
66// Public API - stable in v1.0.0
67pub mod cache;
68pub mod config;
69pub mod error;
70pub mod stats;
71
72pub use cache::Cache;
73pub use config::CacheConfig;
74pub use error::{CacheError, CacheResult};
75pub use stats::{CacheStats, StatsSnapshot};
76
77// Internal modules - not part of public API
78pub(crate) mod entry;
79pub(crate) mod storage;
80
81// Legacy modules - preserved for backward compatibility with server/client binaries
82pub mod utils;
83pub use utils::buffer_to_array;
84
85pub mod command;
86pub use command::Command;
87
88// Re-export Db for backward compatibility, but mark as deprecated
89#[doc(hidden)]
90pub mod database {
91    //! Legacy database module - use `Cache` instead.
92    pub use crate::storage::Db;
93}
94#[doc(hidden)]
95pub use storage::Db;
96
97pub mod cli;
98pub use cli::{Cli, ClientCommand};