Experimental: Alpha Release
This crate is in early development. APIs are unstable and may change without notice. Not recommended for production use yet.
HTTP Cache Tags: Framework Integrations for Actix and Axum
This crate provides seamless integration of HTTP cache tagging and validation features into popular Rust web frameworks, including Actix-web and Axum.
It builds on the core [http_cache_tags_core] library to deliver middleware, extractors, API handlers, and runtime components that enable tag-based cache invalidation and metadata resolution.
Features
- Axum Integration (
axum): Middleware, extractors, and runtime for the Axum framework. - Actix Integration (
actix): Middleware, extractors, and runtime for Actix-web framework. - Middleware for injecting cache metadata headers like
ETagandLast-Modified. - Extractors for validated JSON payloads and cache metadata.
- API controllers for cache invalidation and validation routes.
- Support for custom cache stores and seeders.
- Optional features mirroring the core crate (
etag,last_modified,redis, etc.).
By default, the axum integration and last_modified support are enabled.
At least one server integration (axum or actix) and one validation header feature (etag or last_modified) must be activated for proper operation.
| Feature | Description |
|---|---|
axum |
Enable Axum framework middleware and extractors |
actix |
Enable Actix-web middleware and extractors |
config_file |
Load configuration from TOML file |
last_modified |
Support Last-Modified timestamps |
etag |
Generate and handle ETags |
redis |
Redis backend for cache metadata persistence |
Getting Started
1. Define Your Cache Config
The cache config maps URL routes to cache tags, and configures invalidation endpoints.
use *;
let config = builder
.invalidation_api_route
.invalidation_api_secret
.add_route_mapping
.add_route_mapping
.add_ignore_mapping
.redis_uri
.build;
2. Build a Runtime
The CacheRuntime composes the cache config with stores, API controllers, and middleware components.
use *;
let config = builder.build;
let runtime = builder
.config
// Optionally add a custom seeder to pre-populate cache tags
// .seeder(Box::new(MyCustomSeeder {}))
.build;
Example Setup
Axum Example
use *;
use ;
use ;
async
async
Actix Example
use ;
use *;
async
async
Custom Cache Store Seeder Example
Implement CacheStoreSeeder to prepopulate the cache store with tags or metadata on startup, useful for warming caches or integrating external systems.
use *;
use ;
;
API Usage Examples
This crate supports HTTP cache tagging and invalidation with detailed control over cache metadata such as ETag and Last-Modified headers.
Below are typical HTTP request examples demonstrating how to interact with the cache system:
Conditional GET with If-Modified-Since
GET /blog/test HTTP/1.1
If-Modified-Since: Sun, 20 Jul 2025 23:12:31 +0000
Conditional GET with If-None-Match (ETag)
GET /blog/test HTTP/1.1
If-None-Match: W/"82f493c74aaf8d7557ee619db325d6948f9cfb19"
Invalidate with update of last_modified and etag (with auth header)
POST /_invalidate HTTP/1.1
Content-Type: application/json
Authorization: Bearer my-secret
{
"tags": [
{ "tag": "blog", "last_modified": true, "etag": true }
]
}
Remove all cache metadata for a tag
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "blog", "last_modified": null, "etag": null }
]
}
Set explicit last_modified timestamp
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "blog", "last_modified": "2024-01-01T12:00:00Z" }
]
}
Remove last_modified timestamp only
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "blog", "last_modified": null }
]
}
Trigger ETag regeneration only
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "blog", "etag": true }
]
}
Remove ETag completely
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "blog", "etag": null }
]
}
Invalidate multiple tags with different operations
POST /_invalidate HTTP/1.1
Content-Type: application/json
{
"tags": [
{ "tag": "user:42", "etag": true },
{ "tag": "product:99", "last_modified": true },
{ "tag": "stale:tag", "last_modified": null, "etag": null }
]
}
For more examples, see the examples/ folder containing HTTP request files demonstrating common usage patterns and advanced configurations.