pub struct MemoryCache { /* private fields */ }Expand description
An in-process Cache for L1 caching.
Default instance is unbounded — it grows until the process runs out of
memory. For memory-constrained workloads, use MemoryCache::with_max_entries
to install a simple LRU-style cap: when the cap is exceeded, the oldest
(least-recently-inserted) entry is evicted.
Implementations§
Source§impl MemoryCache
impl MemoryCache
Sourcepub fn with_capacity(self, capacity: usize) -> Self
pub fn with_capacity(self, capacity: usize) -> Self
Pre-allocates space for capacity entries to reduce reallocation.
Sourcepub fn with_max_entries(self, max: usize) -> Self
pub fn with_max_entries(self, max: usize) -> Self
Installs a bounded LRU-style cap. When the cache exceeds max, the
oldest (least-recently-inserted) entry is evicted on each set.
This is the recommended constructor for production L1 caches: a
MemoryCache::new() (unbounded) left unmanaged can grow without bound
and exhaust process memory.
Sourcepub fn max_entries(&self) -> Option<usize>
pub fn max_entries(&self) -> Option<usize>
The configured max entries, if any.
Trait Implementations§
Source§impl Cache for MemoryCache
impl Cache for MemoryCache
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Fetches a value by key.
None indicates a miss.Source§fn set<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn set<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Stores a value under
key, optionally expiring after ttl.Source§impl Clone for MemoryCache
impl Clone for MemoryCache
Source§fn clone(&self) -> MemoryCache
fn clone(&self) -> MemoryCache
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MemoryCache
impl Debug for MemoryCache
Auto Trait Implementations§
impl Freeze for MemoryCache
impl RefUnwindSafe for MemoryCache
impl Send for MemoryCache
impl Sync for MemoryCache
impl Unpin for MemoryCache
impl UnsafeUnpin for MemoryCache
impl UnwindSafe for MemoryCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more