Expand description
Response caching middleware for Tower services.
This crate provides a Tower middleware for caching service responses, reducing load on downstream services by storing and reusing responses for identical requests.
§Features
- Multiple Eviction Policies: LRU, LFU, and FIFO eviction strategies
- TTL Support: Optional time-to-live for cache entries
- Event System: Observability through cache events (Hit, Miss, Eviction)
- Flexible Key Extraction: User-defined key extraction from requests
§Examples
use tower_resilience_cache::{CacheLayer, EvictionPolicy};
use tower::ServiceBuilder;
use std::time::Duration;
// Create a cache layer with LFU eviction
let cache_layer = CacheLayer::builder()
.max_size(100)
.ttl(Duration::from_secs(60))
.eviction_policy(EvictionPolicy::Lfu) // or Lru (default), Fifo
.key_extractor(|req: &String| req.clone())
.on_hit(|| println!("Cache hit!"))
.on_miss(|| println!("Cache miss!"))
.build();
// Apply to a service
let service = ServiceBuilder::new()
.layer(cache_layer)
.service(tower::service_fn(|req: String| async move {
Ok::<_, std::io::Error>(format!("Response: {}", req))
}));Structs§
- Cache
- A Tower
Servicethat caches responses. - Cache
Config - Configuration for the cache pattern.
- Cache
Config Builder - Builder for configuring and constructing a cache.
- Cache
Layer - A Tower
Layerthat applies response caching to a service. - Shared
Cache Config Builder - Builder for configuring and constructing a shared cache layer.
- Shared
Cache Layer - A Tower
Layerthat applies response caching with a shared store.
Enums§
- Cache
Error - Errors that can occur in the cache.
- Cache
Event - Events emitted by the cache.
- Eviction
Policy - Eviction policy for the cache.
Type Aliases§
- KeyExtractor
- Function that extracts a cache key from a request.