pub struct LocalCache { /* private fields */ }Expand description
Local in-memory cache with per-entry TTL support.
Uses Moka’s sync cache with a custom expiry policy to support
per-operation TTL values, ensuring behavioral consistency with
RedisCache.
§Performance
This implementation is optimized for hot-path usage:
- Uses
Bytesfor keys and values (reference-counted, cheap clones) - Supports borrowed lookups via the
equivalenttrait forcontains_sync - Minimizes allocations on repeated operations with the same key
§Example
use std::time::Duration;
use mx_cache::{LocalCache, Cache};
let cache = LocalCache::new(10_000, Duration::from_secs(300));
// Each operation can specify its own TTL
cache.set(b"short_lived", b"value1", Duration::from_secs(10)).await.unwrap();
cache.set(b"long_lived", b"value2", Duration::from_secs(3600)).await.unwrap();Implementations§
Source§impl LocalCache
impl LocalCache
Sourcepub fn new(capacity: u64, default_ttl: Duration) -> Self
pub fn new(capacity: u64, default_ttl: Duration) -> Self
Creates a new local cache with the specified capacity and default TTL.
§Arguments
capacity- Maximum number of entries in the cachedefault_ttl- Default TTL used as an upper bound for entries. Individual operations can specify shorter TTLs, but entries will never live longer than this default.
§Note
The default_ttl parameter sets a maximum lifetime for entries.
Per-operation TTLs (passed to set() and set_nx_px()) take
precedence and are fully respected, as long as they don’t exceed
this default.
Sourcepub fn contains_sync(&self, key: &[u8]) -> bool
pub fn contains_sync(&self, key: &[u8]) -> bool
Synchronous check for local cache presence - fast path for deduplication.
Returns true if the key exists in the cache and has not expired.
§Performance
This is a synchronous operation optimized for hot-path deduplication
checks. Uses borrowed key lookup via the equivalent trait to avoid
allocating a new Bytes on every call.
Trait Implementations§
Source§impl Cache for LocalCache
impl Cache for LocalCache
Source§fn set_nx_px(
&self,
key: &[u8],
value: &[u8],
ttl: Duration,
) -> impl Future<Output = Result<bool>> + Send
fn set_nx_px( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<bool>> + Send
Source§fn set(
&self,
key: &[u8],
value: &[u8],
ttl: Duration,
) -> impl Future<Output = Result<()>> + Send
fn set( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<()>> + Send
Sets a value with the specified TTL.
§Arguments
key- The cache keyvalue- The value to storettl- Time-to-live for this entry
Source§impl Clone for LocalCache
impl Clone for LocalCache
Source§fn clone(&self) -> LocalCache
fn clone(&self) -> LocalCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more