Skip to main content

Crate tower_resilience_cache

Crate tower_resilience_cache 

Source
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 Service that caches responses.
CacheConfig
Configuration for the cache pattern.
CacheConfigBuilder
Builder for configuring and constructing a cache.
CacheLayer
A Tower Layer that applies response caching to a service.
SharedCacheConfigBuilder
Builder for configuring and constructing a shared cache layer.
SharedCacheLayer
A Tower Layer that applies response caching with a shared store.

Enums§

CacheError
Errors that can occur in the cache.
CacheEvent
Events emitted by the cache.
EvictionPolicy
Eviction policy for the cache.

Type Aliases§

KeyExtractor
Function that extracts a cache key from a request.