1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! An in-process cache store with TTLs, an optional entry limit, and least-recently-used eviction.
//!
//! One [`InMemoryCacheStore`] holds values of every type. Name the type when you read; reading a
//! key as another type fails with `CacheError::TypeMismatch`. Clones share the same entries.
//!
//! Values keep their Rust form and are never serialized here, but they still need `Serialize`
//! and `Deserialize`, like on every store, so a store can be swapped for another without touching
//! its call sites. A type without serde, such as a compiled regex or a handle, can't be cached.
//!
//! ```
//! use std::time::Duration;
//!
//! use sisa_caching_core::{CacheOptions, CacheStore};
//! use sisa_caching_memory::{InMemoryCacheSettings, InMemoryCacheStore};
//!
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let cache = InMemoryCacheStore::new("app")?.with_settings(
//! InMemoryCacheSettings {
//! max_entries: Some(10_000),
//! ..Default::default()
//! },
//! )?;
//!
//! cache.set("user:v1:42", "Ada".to_string(), CacheOptions::with_ttl(Duration::from_secs(60))).await?;
//!
//! assert_eq!(cache.get::<String>("user:v1:42").await?, Some("Ada".to_string()));
//! # Ok(())
//! # }
//! ```
//!
//! An expired entry reads as a miss and stays until it is overwritten, a full store trims, or a
//! purge drops it. Run a [`PurgeWorker`] on a `WorkerHost` for that. It purges every
//! `purge_interval_secs` of the store's settings, 60 seconds by default:
//!
//! ```no_run
//! # use sisa_caching_core::WorkerHost;
//! # use sisa_caching_memory::{InMemoryCacheSettings, InMemoryCacheStore, PurgeWorker};
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let cache = InMemoryCacheStore::new("app")?.with_settings(InMemoryCacheSettings {
//! purge_interval_secs: Some(30),
//! ..Default::default()
//! })?;
//!
//! let workers = WorkerHost::new();
//! workers.register(PurgeWorker::new(cache.clone()));
//! # Ok(())
//! # }
//! ```
pub use InMemoryCacheSettings;
pub use InMemoryCacheStore;
pub use PurgeWorker;