Expand description
Deduplicates concurrent async work for the same cache key.
Users implement CacheBackend for their cache store (or use the provided
MemoryCache or RedisCache), then call
CacheFlight::run() with a key and a work closure.
§Example
use cacheflight::{CacheFlight, MemoryCache, Result};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
let cf = CacheFlight::new(MemoryCache::new())
.ttl(Duration::from_secs(30));
let result = cf.run("user:42", || async {
let body = br#"{"id":42,"name":"Ada"}"#.to_vec();
Ok(body)
}).await?;
assert_eq!(result.value(), br#"{"id":42,"name":"Ada"}"#);
Ok(())
}Structs§
- Cache
Flight - Deduplicates concurrent recomputation for the same key, with optional stale-while-revalidate and probabilistic early expiration.
- Compute
Value - Wraps a computed value so the engine can distinguish successful results from errors during the cache-update path.
- Error
Source - Wraps an inner error for use as a source in
Error. - HasFlat
Expiry - Zero-sized marker: a flat TTL has been configured via
.ttl(). - HasSwr
Expiry - Zero-sized marker: stale-while-revalidate has been configured.
- HasXfetch
- Zero-sized marker: probabilistic early expiration is enabled.
- Lookup
Result - Memory
Cache - NoExpiry
- Zero-sized marker: no expiry strategy has been configured yet.
- NoXfetch
- Zero-sized marker: probabilistic early expiration is not enabled.
- Noop
Metrics - Redis
Cache
Enums§
Traits§
- Cache
Backend - The only cache abstraction the library depends on.
- Metrics
Hooks