pub trait CacheExt: Cache {
// Provided methods
fn get_json<T>(
&self,
key: &str,
) -> impl Future<Output = Result<Option<T>, CacheError>> + Send
where T: DeserializeOwned { ... }
fn set_json<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> impl Future<Output = Result<(), CacheError>> + Send
where T: Serialize { ... }
fn get_or_set<T, F, Fut>(
&self,
key: &str,
ttl: Option<Duration>,
f: F,
) -> impl Future<Output = Result<T, CacheError>> + Send
where T: DeserializeOwned + Serialize + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Result<T, CacheError>> + Send { ... }
}Expand description
JSON helpers implemented for every Cache.
Provided Methods§
Sourcefn get_json<T>(
&self,
key: &str,
) -> impl Future<Output = Result<Option<T>, CacheError>> + Sendwhere
T: DeserializeOwned,
fn get_json<T>(
&self,
key: &str,
) -> impl Future<Output = Result<Option<T>, CacheError>> + Sendwhere
T: DeserializeOwned,
Fetch key and deserialize it as JSON of type T.
§Errors
CacheError::Deserialize when the stored bytes are not JSON of type
T. Backend failures from Cache::get.
Sourcefn set_json<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> impl Future<Output = Result<(), CacheError>> + Sendwhere
T: Serialize,
fn set_json<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> impl Future<Output = Result<(), CacheError>> + Sendwhere
T: Serialize,
Serialize value as compact JSON and store it at key.
§Errors
CacheError::Serialize when value cannot be encoded. Backend
failures from Cache::set.
Sourcefn get_or_set<T, F, Fut>(
&self,
key: &str,
ttl: Option<Duration>,
f: F,
) -> impl Future<Output = Result<T, CacheError>> + Sendwhere
T: DeserializeOwned + Serialize + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Result<T, CacheError>> + Send,
fn get_or_set<T, F, Fut>(
&self,
key: &str,
ttl: Option<Duration>,
f: F,
) -> impl Future<Output = Result<T, CacheError>> + Sendwhere
T: DeserializeOwned + Serialize + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Result<T, CacheError>> + Send,
Return the JSON value at key, computing and storing it on a miss.
A stored value that cannot be deserialized is treated as a miss so a
corrupt entry can be replaced. Concurrent misses may both run f.
§Errors
Errors from f, JSON encoding of the computed value, or the backend.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".