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
//! 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>> {
//! // Settings are built from a default, so a later release can add one without breaking this.
//! let mut settings = InMemoryCacheSettings::default();
//! settings.memory.max_entries = Some(10_000);
//! let cache = InMemoryCacheStore::new("app")?.with_settings(settings)?;
//!
//! 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. The store purges every `memory.purge_interval_secs` of its settings, 60
//! seconds by default. To purge on a schedule of your own, turn that off and call `purge`:
//!
//! ```
//! use sisa_caching_memory::{InMemoryCacheSettings, InMemoryCacheStore};
//!
//! let mut settings = InMemoryCacheSettings::default();
//! settings.memory.purge_interval_secs = None;
//! let cache = InMemoryCacheStore::new("app")?.with_settings(settings)?;
//!
//! // From your own scheduler:
//! let removed = cache.purge();
//! # assert_eq!(removed, 0);
//! # Ok::<(), sisa_caching_core::CacheError>(())
//! ```
pub use ;
pub use InMemoryCacheStore;