Expand description

A caching middleware that follows HTTP caching rules, thanks to http-cache-semantics. By default, it uses cacache as the backend cache manager.

Supported Clients

  • Surf *Should likely be registered after any middleware modifying the request
  • Reqwest *Uses reqwest-middleware for middleware support

Examples

Surf (requires feature: client-surf)

use http_cache::{Cache, CacheMode, CACacheManager};

#[async_std::main]
async fn main() -> surf::Result<()> {
    let req = surf::get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching");
    surf::client()
        .with(Cache {
            mode: CacheMode::Default,
            manager: CACacheManager::default(),
            options: None,
        })
        .send(req)
        .await?;
    Ok(())
}

Reqwest (requires feature: client-reqwest)

use reqwest::Client;
use reqwest_middleware::{ClientBuilder, Result};
use http_cache::{Cache, CacheMode, CACacheManager};

#[tokio::main]
async fn main() -> Result<()> {
    let client = ClientBuilder::new(Client::new())
        .with(Cache {
            mode: CacheMode::Default,
            manager: CACacheManager::default(),
            options: None,
        })
        .build();
    client
        .get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching")
        .send()
        .await?;
    Ok(())
}

Features

The following features are available. By default manager-cacache is enabled.

  • manager-cacache (default): use cacache, a high-performance disk cache, for the manager backend.
  • client-surf (disabled): enables surf client support.
  • client-reqwest (disabled): enables reqwest client support.

Structs

Implements CacheManager with cacache as the backend.

Caches requests according to http spec.

Options struct provided by http-cache-semantics. Configuration options which control behavior of the cache. Use with CachePolicy::new_options().

A basic generic type that represents an HTTP response

Enums

A generic “error” for HTTP caches

Similar to make-fetch-happen cache options. Passed in when the Cache struct is being built.

Represents an HTTP version

Traits

A trait providing methods for storing, reading, and removing cache records.

Type Definitions

A Result typedef to use with the CacheError type