tower-resilience-cache 0.1.0

Response caching/memoization for Tower services
Documentation

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

  • LRU Eviction: Least Recently Used eviction policy
  • 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::CacheConfig;
use tower::ServiceBuilder;
use std::time::Duration;

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Create a cache configuration
let cache_config = CacheConfig::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .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_config.layer())
    .service(tower::service_fn(|req: String| async move {
        Ok::<_, std::io::Error>(format!("Response: {}", req))
    }));
# Ok(())
# }