Expand description
§Cache layer for tower::Service
s
CacheLayer
is a tower Layer that provides caches for Service
s 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§
- lru
lru
- LRU cache provider
Structs§
- Cache
Layer - Layer that adds cache to a
tower::Service
- Cache
Service - Service generated by
CacheLayer
.
Enums§
- Error
- Error returned by the
CacheLayer
- Provider
Request - Requests sent to the cache provider
- Provider
Response - Responses sent by the cache provider