pub struct Cache { /* private fields */ }Expand description
The app’s cache (state.cache), in memory or the cache table (CACHE_STORE).
Implementations§
Source§impl Cache
impl Cache
Sourcepub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
The cached value, if present and not expired. A value of another
type than T is an error.
Sourcepub async fn has(&self, key: &str) -> Result<bool>
pub async fn has(&self, key: &str) -> Result<bool>
Whether key holds a value that hasn’t expired.
Sourcepub async fn put(
&self,
key: &str,
value: &impl Serialize,
ttl: Option<Duration>,
) -> Result
pub async fn put( &self, key: &str, value: &impl Serialize, ttl: Option<Duration>, ) -> Result
Stores value for ttl, or until forgotten when ttl is None. A
zero ttl stores nothing (and forgets the key).
Sourcepub async fn add(
&self,
key: &str,
value: &impl Serialize,
ttl: Option<Duration>,
) -> Result<bool>
pub async fn add( &self, key: &str, value: &impl Serialize, ttl: Option<Duration>, ) -> Result<bool>
Stores value only if key has no live value; returns whether it
did. Atomic, so of several callers exactly one gets true.
Sourcepub async fn pull<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub async fn pull<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
The cached value, removed from the cache in the same step (a one-time code, a flash of data between requests).
Sourcepub async fn increment(&self, key: &str, by: i64) -> Result<i64>
pub async fn increment(&self, key: &str, by: i64) -> Result<i64>
Adds by (negative to subtract) to the number at key and returns
the new value; a missing or expired key counts as 0 and never
expires. Atomic, so concurrent calls don’t lose counts. A live value
that isn’t a whole number is an error.
Sourcepub fn lock(&self, name: &str, ttl: Duration) -> Lock
pub fn lock(&self, name: &str, ttl: Duration) -> Lock
A lock named name, held for ttl (up to a second more, never
less) once acquired unless released earlier, so a crashed holder
doesn’t block others forever. With the database store
it works across processes and servers; with the memory store, within
this process.
Sourcepub async fn prune(&self) -> Result<u64>
pub async fn prune(&self) -> Result<u64>
Deletes expired entries; returns how many (the database store’s count; the memory store returns 0 after sweeping).
Sourcepub async fn remember<T, F, Fut>(
&self,
key: &str,
ttl: Duration,
compute: F,
) -> Result<T>
pub async fn remember<T, F, Fut>( &self, key: &str, ttl: Duration, compute: F, ) -> Result<T>
Returns the cached value, or runs compute, caches its result for
ttl and returns it. Errors are not cached. Concurrent calls for the
same key in this process compute once; the others wait for it. A
cached value of another type (e.g. after a deploy) is computed again.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Cache
impl !UnwindSafe for Cache
impl Freeze for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl UnsafeUnpin for Cache
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more