Skip to main content

Crate cacheflight

Crate cacheflight 

Source
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§

CacheFlight
Deduplicates concurrent recomputation for the same key, with optional stale-while-revalidate and probabilistic early expiration.
ComputeValue
Wraps a computed value so the engine can distinguish successful results from errors during the cache-update path.
ErrorSource
Wraps an inner error for use as a source in Error.
HasFlatExpiry
Zero-sized marker: a flat TTL has been configured via .ttl().
HasSwrExpiry
Zero-sized marker: stale-while-revalidate has been configured.
HasXfetch
Zero-sized marker: probabilistic early expiration is enabled.
LookupResult
MemoryCache
NoExpiry
Zero-sized marker: no expiry strategy has been configured yet.
NoXfetch
Zero-sized marker: probabilistic early expiration is not enabled.
NoopMetrics
RedisCache

Enums§

CacheMissReason
Error
LookupState
RecomputeOutcome
RecomputeReason

Traits§

CacheBackend
The only cache abstraction the library depends on.
MetricsHooks

Type Aliases§

Result

Attribute Macros§

async_trait