Skip to main content

CACHE_CONTROL

Constant CACHE_CONTROL 

Source
pub const CACHE_CONTROL: &str = "private, no-cache";
Expand description

Cache-Control of files sent by serve: browsers keep them but revalidate each time.

While the ETag matches, the browser gets a body-less 304 Not Modified (still one R2 class B operation). private keeps shared caches from storing files that may belong to one user. Override the header on the returned response for files that may be cached longer.

ยงExamples

use axum::{extract::State, http::{HeaderMap, HeaderValue, header}, response::Response};
use ocre::{Ctx, Result, storage::{self, Attachment, Disposition}};

async fn logo(State(ctx): State<Ctx>, headers: HeaderMap) -> Result<Response> {
    let logo = Attachment {
        key: "public/logo".into(),
        filename: "logo.png".into(),
        content_type: "image/png".into(),
        size: 4096,
    };
    let mut response = storage::serve(&ctx, &logo, &headers, Disposition::Inline).await?;
    // Keys never change, so a public file can be cached for a year.
    let forever = HeaderValue::from_static("public, max-age=31536000, immutable");
    response.headers_mut().insert(header::CACHE_CONTROL, forever);
    Ok(response)
}