Expand description
§High-Performance Caching
This module provides comprehensive caching solutions for Torch applications, including both in-memory and Redis-based caching. It supports response caching, data caching, and cache middleware for automatic request/response caching.
§Features
- In-Memory Cache: Fast, local caching with TTL support
- Redis Cache: Distributed caching with Redis backend
- Response Caching: Automatic HTTP response caching middleware
- TTL Support: Time-to-live expiration for cache entries
- Cache Invalidation: Manual and automatic cache invalidation
- Serialization: JSON serialization for complex data types
§Cache Types
§In-Memory Cache
Fast, local caching that stores data in application memory. Best for:
- Single-instance applications
- Frequently accessed data
- Low-latency requirements
§Redis Cache
Distributed caching using Redis as the backend. Best for:
- Multi-instance applications
- Shared cache across services
- Persistent caching
- Large cache sizes
§Examples
§Basic In-Memory Caching
use torch_web::{App, cache::MemoryCache};
use std::time::Duration;
let cache = MemoryCache::new(Some(Duration::from_secs(300))); // 5 minute TTL
let app = App::new()
.with_state(cache)
.get("/data/:id", |Path(id): Path<u32>, State(cache): State<MemoryCache>| async move {
let cache_key = format!("data:{}", id);
// Try to get from cache first
if let Some(cached_data) = cache.get(&cache_key).await {
return Response::ok()
.header("X-Cache", "HIT")
.body(cached_data);
}
// Fetch data (expensive operation)
let data = fetch_data_from_database(id).await;
// Cache the result
cache.set(&cache_key, &data, None).await;
Response::ok()
.header("X-Cache", "MISS")
.body(data)
});
§Redis Caching
use torch_web::{App, cache::RedisCache};
let cache = RedisCache::new("redis://localhost:6379").await?;
let app = App::new()
.with_state(cache)
.get("/users/:id", |Path(id): Path<u32>, State(cache): State<RedisCache>| async move {
let cache_key = format!("user:{}", id);
if let Some(user_json) = cache.get(&cache_key).await? {
return Response::ok()
.header("Content-Type", "application/json")
.header("X-Cache", "HIT")
.body(user_json);
}
let user = get_user_from_db(id).await?;
let user_json = serde_json::to_string(&user)?;
// Cache for 1 hour
cache.set(&cache_key, &user_json, Some(Duration::from_secs(3600))).await?;
Response::ok()
.header("Content-Type", "application/json")
.header("X-Cache", "MISS")
.body(user_json)
});
Structs§
- Cache
Middleware - Response caching middleware
- Cache
Stats - Cache statistics
- Cache
Warmer - Cache warming utility
- Memory
Cache - High-performance in-memory cache implementation.
- Redis
Cache - Redis cache implementation
Traits§
- Cache
- Cache trait for unified interface