Crate tower_cache

Source
Expand description

§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.

Modules§

lrulru
LRU cache provider

Structs§

CacheLayer
Layer that adds cache to a tower::Service
CacheService
Service generated by CacheLayer.

Enums§

Error
Error returned by the CacheLayer
ProviderRequest
Requests sent to the cache provider
ProviderResponse
Responses sent by the cache provider