pub trait CacheExt: Cache {
// Provided methods
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 { ... }
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 { ... }
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 { ... }
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 { ... }
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 { ... }
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 { ... }
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 { ... }
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 { ... }
}Expand description
The typed conveniences every Cache gets.
Kept out of the object-safe trait so Arc<dyn Cache> still exists.
Provided Methods§
Sourcefn 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,
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.
Sourcefn 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>>
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>>
Store a value as JSON.
Sourcefn 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,
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.
Sourcefn 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,
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.
Sourcefn 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,
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.
Cache::add with the encoding done for you. Atomic, like the method
it calls.
Sourcefn 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,
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.
Sourcefn 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>>
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>>
The cached value at key, or compute it and cache that.
Laravel’s Cache::remember. The pattern nearly every cache use is,
written once:
let settings: Settings = cache
.remember("settings", Some(Duration::from_secs(300)), || async {
load_settings(&database).await
})
.await?;§A failure is never cached
If compute returns an error, the error is returned and nothing is
stored. Caching a failure for five minutes turns one bad second into
five bad minutes, and the request that would have succeeded never gets
to try. Cache a value; let the next caller retry.
A cached value that will not deserialise is treated as a miss and
recomputed — see get_json.
§It is not a lock
Ten simultaneous misses run compute ten times and the last write
wins. That is the right trade for a cache: serialising them would mean
nine requests waiting on a lock held by a tenth that might fail. When
the computation is expensive enough that a stampede matters, take a
Lock around it explicitly.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".