Actix Request-Reply Cache
A Redis-backed response caching middleware for Actix Web applications.
Features
- Redis-backed caching: Store HTTP responses in Redis for fast retrieval
- Configurable TTL: Set custom expiration times for cached responses
- Size limits: Control maximum response size to cache
- Flexible cache control: Use predicate functions to determine what should be cached
- Cache key customization: Set your own cache key prefix
- Standard compliant: Respects HTTP Cache-Control headers
- Minimal performance impact: Fast cache lookup and non-blocking I/O
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use ;
use RedisCacheMiddlewareBuilder;
async
Advanced Configuration
use ;
use RedisCacheMiddlewareBuilder;
async
Cache Decision Context
The cache_if predicate receives a CacheDecisionContext with the following information:
method: HTTP method string ("GET", "POST", etc.)path: Request pathquery_string: URL query parametersheaders: HTTP request headersbody: Request body as bytes
This allows for fine-grained control over what gets cached.
How It Works
- Incoming requests are intercepted by the middleware
- If Cache-Control headers specify no-cache/no-store, the request is processed normally
- Otherwise, a cache key is computed based on the request method, path, query string and body
- If a matching response is found in Redis, it's returned immediately with an X-Cache: HIT header
- If no match is found, the request is processed normally, and if the response is successful:
- The response is serialized and stored in Redis with the configured TTL
- The original response is returned to the client
License
This project is licensed under the MIT License.