Crate http_cache_quickcache

Crate http_cache_quickcache 

Source
Expand description

HTTP caching manager implementation using QuickCache.

This crate provides a CacheManager implementation using the QuickCache in-memory cache. QuickCache is an in-memory cache that can be used for applications that need cache access with predictable memory usage.

§Basic Usage

use http_cache_quickcache::QuickManager;
use quick_cache::sync::Cache;

// Create a cache with a maximum of 1000 entries
let cache = Cache::new(1000);
let manager = QuickManager::new(cache);

// Use with any HTTP cache implementation that accepts a CacheManager

§Integration with HTTP Cache Middleware

§With Tower Services

use tower::{Service, ServiceExt};
use http::{Request, Response, StatusCode};
use http_body_util::Full;
use bytes::Bytes;
use http_cache_quickcache::QuickManager;
use std::convert::Infallible;

// Example Tower service that uses QuickManager for caching
#[derive(Clone)]
struct CachingService {
    cache_manager: QuickManager,
}

impl Service<Request<Full<Bytes>>> for CachingService {
    type Response = Response<Full<Bytes>>;
    type Error = Box<dyn std::error::Error + Send + Sync>;
    type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
        std::task::Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request<Full<Bytes>>) -> Self::Future {
        let manager = self.cache_manager.clone();
        Box::pin(async move {
            // Cache logic using the manager would go here
            let response = Response::builder()
                .status(StatusCode::OK)
                .body(Full::new(Bytes::from("Hello from cached service!")))?;
            Ok(response)
        })
    }
}

§With Hyper

use hyper::{Request, Response, StatusCode, body::Incoming};
use http_body_util::Full;
use bytes::Bytes;
use http_cache_quickcache::QuickManager;
use std::convert::Infallible;

async fn handle_request(
    _req: Request<Incoming>,
    cache_manager: QuickManager,
) -> Result<Response<Full<Bytes>>, Infallible> {
    // Use cache_manager here for caching responses
    Ok(Response::builder()
        .status(StatusCode::OK)
        .header("cache-control", "max-age=3600")
        .body(Full::new(Bytes::from("Hello from Hyper with caching!")))
        .unwrap())
}

§Usage Characteristics

QuickCache is designed for scenarios where:

  • You need predictable memory usage
  • In-memory storage is acceptable
  • You want to avoid complex configuration
  • Memory-based caching fits your use case

For applications that need persistent caching across restarts, consider using CACacheManager instead, which provides disk-based storage.

Structs§

QuickManager
HTTP cache manager implementation using QuickCache.