1 unstable release

0.0.1 Feb 28, 2022

#10 in #tower-layer

MIT license

13KB
255 lines

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.

Dependencies

~2.1–3MB
~55K SLoC