Skip to main content

MemoryCache

Struct MemoryCache 

Source
pub struct MemoryCache { /* private fields */ }
Expand description

A cache held in this process.

Right for development, tests, and anything genuinely per-process. Two instances behind a load balancer do not share it, which for a cache is a performance question rather than a correctness one — unlike a session, where it is both.

The exception is anything you cache for correctness: a rate-limit counter or a lock in a memory cache is per-instance, so the limit is really N × limit and the lock is not one.

Implementations§

Source§

impl MemoryCache

Source

pub fn new() -> Self

An empty cache.

Source

pub fn len(&self) -> usize

How many entries are held, expired ones included.

Source

pub fn is_empty(&self) -> bool

Whether nothing is held.

Source

pub fn purge(&self) -> u64

Drop every expired entry. Returns how many.

Reads clean up the key they touch, but a key never read again would otherwise sit there — so a long-lived process wants this on a timer.

Trait Implementations§

Source§

impl Cache for MemoryCache

Source§

fn is_shared(&self) -> bool

One process. Two instances of the application share nothing here.

Source§

fn name(&self) -> &str

A label for diagnostics — "memory", "redis", "memcached".
Source§

fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Vec<u8>>>>

The value at key, or None if it is absent or expired.
Source§

fn put<'a>( &'a self, key: &'a str, value: &'a [u8], ttl: Option<Duration>, ) -> BoxFuture<'a, Result<()>>

Store value, expiring after ttl. None means no expiry.
Source§

fn forget<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<bool>>

Remove a key. true if it was there.
Source§

fn flush(&self) -> BoxFuture<'_, Result<()>>

Remove everything. Read more
Source§

fn add<'a>( &'a self, key: &'a str, value: &'a [u8], ttl: Option<Duration>, ) -> BoxFuture<'a, Result<bool>>

Store value only if key is absent. true if it was stored. Read more
Source§

fn forget_if<'a>( &'a self, key: &'a str, expected: &'a [u8], ) -> BoxFuture<'a, Result<bool>>

Remove key only if it currently holds expected. true if it was removed. Read more
Source§

fn increment<'a>(&'a self, key: &'a str, by: i64) -> BoxFuture<'a, Result<i64>>

Add by to a counter, creating it at zero first. Returns the new value. Read more
Source§

fn supports_atomic_add(&self) -> bool

Whether add is genuinely atomic here. Read more
Source§

fn has<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<bool>>

Whether a key is present and unexpired.
Source§

impl Debug for MemoryCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> CacheExt for C
where C: Cache + ?Sized,

Source§

fn get_json<'life0, 'life1, 'async_trait, T>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
where T: 'async_trait + DeserializeOwned, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A value, deserialised from JSON.
Source§

fn put_json<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, key: &'life1 str, value: &'life2 T, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where T: 'async_trait + Serialize + Sync, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store a value as JSON.
Source§

fn get_string<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A string value.
Source§

fn put_string<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 str, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store a string.
Source§

fn add_string<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 str, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store a string only if the key is absent. Read more
Source§

fn decrement<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, by: i64, ) -> Pin<Box<dyn Future<Output = Result<i64>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Subtract from a counter.
Source§

fn remember<'life0, 'life1, 'async_trait, T, F, Fut>( &'life0 self, key: &'life1 str, ttl: Option<Duration>, compute: F, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where T: Serialize + DeserializeOwned + Sync + Send + 'async_trait, F: FnOnce() -> Fut + Send + 'async_trait, Fut: Future<Output = Result<T>> + Send + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

The cached value at key, or compute it and cache that. Read more
Source§

fn remember_forever<'life0, 'life1, 'async_trait, T, F, Fut>( &'life0 self, key: &'life1 str, compute: F, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where T: Serialize + DeserializeOwned + Sync + Send + 'async_trait, F: FnOnce() -> Fut + Send + 'async_trait, Fut: Future<Output = Result<T>> + Send + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

remember, with no expiry. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more