tower-cache 0.0.1

Caching layer for Tower
Documentation
  • Coverage
  • 100%
    17 out of 17 items documented1 out of 9 items with examples
  • Size
  • Source code size: 25.63 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.6 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 21s Average build duration of successful builds.
  • all releases: 21s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nmoutschen/tower-cache
    6 2 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nmoutschen

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