tower-cache 0.0.1

Caching layer for Tower
Documentation

Cache layer for tower::Services

[CacheLayer] is a tower Layer that provides caches for Services by using another service to handle the cache. This allows the usage of asynchronous and external caches.

Usage

use std::convert::Infallible;
use tower::{ServiceBuilder, service_fn};
use tower_cache::{
    CacheLayer,
    lru::LruProvider,
};
async fn handler(req: String) -> Result<String, Infallible> {
    Ok(req.to_uppercase())
}

// Initialize the cache provider service
let lru_provider = LruProvider::<String, String>::new(20);

// Initialize the service
let my_service = service_fn(handler);

// Wrap the service with CacheLayer.
let my_service = ServiceBuilder::new()
    .layer(CacheLayer::<_, String>::new(lru_provider))
    .service(handler);

Creating cache providers

A cache provider is a [tower::Service] that takes a [ProviderRequest] as request and returns a [ProviderResponse].