Crate http_cache_tags_actix

Crate http_cache_tags_actix 

Source
Expand description

Experimental: Alpha Release

This crate is in early development. APIs are unstable and may change without notice. Not recommended for production use yet.

Actix integration for HTTP cache tagging and validation.

This crate builds on the core http_cache_tags_core library to provide middleware, extractors, and API handlers tailored for the Actix-web framework.

It enables tag-based HTTP cache invalidation, cache metadata resolution, and response validation within Actix applications.

§Features

The features available here mirror those from the core crate and are propagated through this integration:

  • Config File (config_file feature): Load cache config from TOML files.
  • ETag Support (etag feature): Generate and handle weak ETags.
  • Last-Modified Support (last_modified feature): Track resource modification timestamps.
  • Redis Integration (redis feature): Use Redis as a persistent backend for cache metadata.

§Components

This crate provides:

  • Actix middleware to inject cache headers (ETag, Last-Modified, Cache-Control).
  • Extractors for retrieving validated JSON payloads and cache metadata.
  • API error types and controllers compatible with Actix-web.

§Example

use actix_web::{App, HttpServer, web, HttpResponse};
use http_cache_tags_actix::prelude::*;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let config = CacheConfig::builder()
        .invalidation_api_route("/_invalidate")
        .add_route_mapping("/blog/*", vec!["blog"])
        .build();

    let runtime = CacheRuntime::builder().config(config).build();

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(runtime.extractor()))
            .wrap(runtime.cache_meta_middleware())
            .wrap(runtime.cache_validation_middleware())
            .service(runtime.invalidation_scope().unwrap())
            .route("/blog/{slug}", web::get().to(blog_handler))
    })
    .bind(("0.0.0.0", 3000))?
    .run()
    .await
}

async fn blog_handler(meta: CacheMeta) -> HttpResponse {
    HttpResponse::Ok().body(format!(
        "Tags: {:?}, Last Modified: {:?}",
        meta.tags, meta.last_modified
    ))
}

§Integration

Use this crate alongside http_cache_tags_core to enable full cache tagging support in Actix-web applications. It is recommended to use the umbrella crate http_cache_tags which bundles all framework integrations, including Actix and Axum.

Modules§

api
Actix Web integration for HTTP cache tag invalidation.
core
Core components re-exported from http_cache_tags_core.
extractor
Extractors for Actix Web integration in the cache tagging system.
middleware
Cache Validation Middleware
prelude
Prelude module for convenient imports.
runtime
Actix integration for HTTP cache tagging.