claviron_cache/memory.rs
1//! Typed process-local caching for synchronous callers.
2
3use std::hash::Hash;
4use std::time::Duration;
5
6pub use moka::sync::Cache as MemoryCache;
7
8/// Longest fixed TTL [`new`] accepts; Moka's builder rejects a longer one.
9pub const MAX_TTL: Duration = Duration::from_secs(1_000 * 365 * 24 * 3600);
10
11/// Construct a bounded cache with a fixed time to live.
12///
13/// # Panics
14///
15/// Panics if `ttl` exceeds [`MAX_TTL`].
16pub fn new<K, V>(
17 capacity: u64,
18 ttl: Duration,
19) -> MemoryCache<K, V>
20where
21 K: Eq + Hash + Send + Sync + 'static,
22 V: Clone + Send + Sync + 'static,
23{
24 MemoryCache::builder().max_capacity(capacity).time_to_live(ttl).build()
25}