surf

The http-cache-surf crate provides a Middleware implementation for the surf HTTP client.

Getting Started

cargo add http-cache-surf

Features

  • manager-redb: (default) Enables the RedbManager backend cache manager.
  • manager-cacache: Enables the CACacheManager backend cache manager.
  • manager-moka: Enables the MokaManager backend cache manager.
  • manager-foyer: Enables the FoyerManager backend cache manager.
  • rate-limiting: Enables cache-aware rate limiting functionality.
  • url-ada: Enables ada-url for URL parsing (mutually exclusive with the default url-standard).

Usage

In the following example we will construct our client with our cache struct from http-cache-surf. This example will use the default mode, default redb manager, and default http cache options.

After constructing our client, we will make a request to the MDN Caching Docs which should result in an object stored in cache on disk.

use http_cache_surf::{Cache, CacheMode, RedbManager, HttpCache, HttpCacheOptions};
use surf::Client;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let client = Client::new()
        .with(Cache(HttpCache {
          mode: CacheMode::Default,
          manager: RedbManager::new("./http-cache.redb")?,
          options: HttpCacheOptions::default(),
        }));

    client
        .get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching")
        .await?;
    Ok(())
}