surf
The http-cache-surf crate provides a Middleware implementation for the surf HTTP client.
Getting Started
cargo add http-cache-surf
Features
manager-cacache: (default) Enables theCACacheManagerbackend cache manager.manager-moka: Enables theMokaManagerbackend cache manager.manager-foyer: Enables theFoyerManagerbackend cache manager.rate-limiting: Enables cache-aware rate limiting functionality.url-ada: Enables ada-url for URL parsing (mutually exclusive with the defaulturl-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 cacache 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.
CACacheManager delegates to the cacache crate, which uses tokio::fs internally and therefore requires a tokio reactor. Surf itself is runtime-agnostic when using the curl-client backend, so the simplest pattern is to run the surf client inside a tokio runtime.
use http_cache_surf::{Cache, CacheMode, CACacheManager, HttpCache, HttpCacheOptions}; use surf::Client; use std::path::PathBuf; #[tokio::main(flavor = "current_thread")] async fn main() -> surf::Result<()> { let client = Client::new() .with(Cache(HttpCache { mode: CacheMode::Default, manager: CACacheManager::new(PathBuf::from("./cache"), false), options: HttpCacheOptions::default(), })); client .get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching") .await?; Ok(()) }