pub struct InMemoryCacheStore<C = SystemClock> { /* private fields */ }memory only.Expand description
An in-process store. One store holds values of every type; get::<User> and set(&key, user, None) name the type per call, and a read with the wrong type fails with
CacheError::TypeMismatch.
The store drops its expired entries every memory.purge_interval_secs of its settings, on a
task it starts on its first write inside a tokio runtime. The task stops when the store’s last
clone drops. Inside a runtime, its purges, the trims of a full store and the freeing of what
remove_prefix and clear took run on the blocking pool, so they hold up no async thread.
C is the clock the store reads the time from, SystemClock unless
with_clock sets another.
Implementations§
Source§impl InMemoryCacheStore
impl InMemoryCacheStore
Sourcepub fn new(name: &str) -> Result<InMemoryCacheStore, CacheError>
pub fn new(name: &str) -> Result<InMemoryCacheStore, CacheError>
A store named name, such as app or tenant:eu, on the system clock. Fails with
CacheError::InvalidKey when the name is empty or has an empty level.
Source§impl<C> InMemoryCacheStore<C>where
C: Clock,
impl<C> InMemoryCacheStore<C>where
C: Clock,
Sourcepub fn with_clock<T>(self, clock: T) -> InMemoryCacheStore<T>where
T: Clock,
pub fn with_clock<T>(self, clock: T) -> InMemoryCacheStore<T>where
T: Clock,
Runs the store on clock instead of the system clock, so tests can move time. Set it while
building the store, before any write: the store keeps its entries but starts a purge task
of its own, on the new clock.
Sourcepub fn with_settings(
self,
settings: InMemoryCacheSettings,
) -> Result<InMemoryCacheStore<C>, CacheError>
pub fn with_settings( self, settings: InMemoryCacheSettings, ) -> Result<InMemoryCacheStore<C>, CacheError>
Replaces the store’s settings. Fails when one is out of range, such as a TTL over 365
days, so a bad value stops setup instead of a later write. The store starts empty, split
over shards maps.
Sourcepub fn settings(&self) -> &InMemoryCacheSettings
pub fn settings(&self) -> &InMemoryCacheSettings
The settings the store runs with.
Trait Implementations§
Source§impl<C> CacheStore for InMemoryCacheStore<C>where
C: Clock,
impl<C> CacheStore for InMemoryCacheStore<C>where
C: Clock,
Source§type Error = CacheError
type Error = CacheError
CacheError converts into it.Source§fn cache_settings(&self) -> &CacheSettings
fn cache_settings(&self) -> &CacheSettings
Source§async fn get_entry<CV>(
&self,
key: &str,
) -> Result<Option<CacheEntry<CV>>, CacheError>where
CV: CacheValue,
async fn get_entry<CV>(
&self,
key: &str,
) -> Result<Option<CacheEntry<CV>>, CacheError>where
CV: CacheValue,
key as a CV, or None when it is missing or expired. The memory store
fails with CacheError::TypeMismatch when it holds another Rust type there. The Redis
store decodes its JSON into CV, so another type whose fields fit reads it too, and JSON
that doesn’t fit is an error. The entry is as it was when looked up: it can expire while
a slow Clone of its value, or its decoding, runs.Source§async fn set_entry<CV>(
&self,
key: &str,
entry: CacheEntry<CV>,
) -> Result<(), CacheError>where
CV: CacheValue,
async fn set_entry<CV>(
&self,
key: &str,
entry: CacheEntry<CV>,
) -> Result<(), CacheError>where
CV: CacheValue,
entry as it is: its creation time and TTL are kept, and it expires when the entry
does, not after this store’s default TTL. A store may hold it for less, as a hybrid store’s
local layer does for local.ttl_secs. get_entry from one store and set_entry into
another copies an entry without changing it.Source§async fn remove(&self, key: &str) -> Result<bool, CacheError>
async fn remove(&self, key: &str) -> Result<bool, CacheError>
key, and returns whether it was there.Source§async fn remove_prefix(&self, prefix: &str) -> Result<usize, CacheError>
async fn remove_prefix(&self, prefix: &str) -> Result<usize, CacheError>
prefix, matched by whole levels (user removes user:42, not
users:42, nor the key user itself), and returns how many. Fails with
CacheError::InvalidKey when prefix is empty or has an empty level. Read moreSource§async fn clear(&self) -> Result<(), CacheError>
async fn clear(&self) -> Result<(), CacheError>
Source§fn get<CV>(
&self,
key: &str,
) -> impl Future<Output = Result<Option<CV>, Self::Error>> + Sendwhere
CV: CacheValue,
fn get<CV>(
&self,
key: &str,
) -> impl Future<Output = Result<Option<CV>, Self::Error>> + Sendwhere
CV: CacheValue,
key as a CV, or None when it is missing or expired.Source§fn set<CV>(
&self,
key: &str,
value: CV,
options: impl Into<Option<CacheOptions>> + Send,
) -> impl Future<Output = Result<(), Self::Error>> + Sendwhere
CV: CacheValue,
fn set<CV>(
&self,
key: &str,
value: CV,
options: impl Into<Option<CacheOptions>> + Send,
) -> impl Future<Output = Result<(), Self::Error>> + Sendwhere
CV: CacheValue,
value under key, replacing what is there. options sets this write’s TTL;
None uses the store’s default TTL. Fails when the TTL is over 365 days. Read moreSource§fn get_or_set<CV, F, Fut, E>(
&self,
key: &str,
load: F,
options: impl Into<Option<CacheOptions>> + Send,
) -> impl Future<Output = Result<CV, E>> + Send
fn get_or_set<CV, F, Fut, E>( &self, key: &str, load: F, options: impl Into<Option<CacheOptions>> + Send, ) -> impl Future<Output = Result<CV, E>> + Send
key, or runs load when there is none, then stores and returns
what it gives. Of the callers on this store and its clones, one per key runs load; the
others wait for it and get what it stored, or after lock_timeout_ms run load too. Other
stores, such as those in other processes, load on their own, and a write made while load
runs can be overwritten by its result. With refresh_threshold set, a value past that
share of its TTL is loaded again by one caller, while the others get the cached value; if
that load fails, its caller gets the cached value as long as the store still holds it. Read moreSource§impl<C> Clone for InMemoryCacheStore<C>where
C: Clone,
impl<C> Clone for InMemoryCacheStore<C>where
C: Clone,
Source§fn clone(&self) -> InMemoryCacheStore<C>
fn clone(&self) -> InMemoryCacheStore<C>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more