Skip to content

Commit a400966

Browse files
committed
updated docs and readmes
1 parent 6a77113 commit a400966

File tree

21 files changed

+289
-56
lines changed

21 files changed

+289
-56
lines changed

docs/book.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[book]
22
authors = ["Christian Haynes"]
33
language = "en"
4-
multilingual = false
54
src = "src"
65
title = "http-cache"

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
- [Backend Cache Manager Implementations](./managers/managers.md)
1717
- [cacache](./managers/cacache.md)
1818
- [moka](./managers/moka.md)
19+
- [foyer](./managers/foyer.md)
1920
- [quick_cache](./managers/quick-cache.md)
2021
- [streaming_cache](./managers/streaming_cache.md)

docs/src/cache-modes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let cache = HttpCache {
6868
```rust
6969
// Respect server headers but limit cache duration to 1 hour maximum
7070
let options = HttpCacheOptions {
71-
max_ttl: Some(Duration::from_hours(1)),
71+
max_ttl: Some(Duration::from_secs(3600)),
7272
..Default::default()
7373
};
7474
let cache = HttpCache {
@@ -360,7 +360,7 @@ let options = HttpCacheOptions {
360360
})),
361361

362362
// Global cache duration limit
363-
max_ttl: Some(Duration::from_hours(24)),
363+
max_ttl: Some(Duration::from_secs(86400)),
364364

365365
// Enable cache status headers for debugging
366366
cache_status_headers: true,

docs/src/clients/reqwest.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@ cargo add http-cache-reqwest
1212

1313
- `manager-cacache`: (default) Enables the [`CACacheManager`](https://docs.rs/http-cache/latest/http_cache/struct.CACacheManager.html) backend cache manager.
1414
- `manager-moka`: Enables the [`MokaManager`](https://docs.rs/http-cache/latest/http_cache/struct.MokaManager.html) backend cache manager.
15+
- `manager-foyer`: Enables the [`FoyerManager`](https://docs.rs/http-cache/latest/http_cache/struct.FoyerManager.html) backend cache manager.
1516
- `streaming`: Enables streaming cache support for memory-efficient handling of large response bodies.
17+
- `rate-limiting`: Enables cache-aware rate limiting functionality.
18+
- `url-ada`: Enables ada-url for URL parsing.
1619

1720
## Usage
1821

19-
In the following example we will construct our client using the builder provided by [`reqwest_middleware`](https://github.com/TrueLayer/reqwest-middleware) with our cache struct from [`http-cache-reqwest`](https://github.com/06chaynes/http-cache/tree/latest/http-cache-reqwest). This example will use the default mode, default cacache manager, and default http cache options.
22+
In the following example we will construct our client using the builder provided by [`reqwest_middleware`](https://github.com/TrueLayer/reqwest-middleware) with our cache struct from [`http-cache-reqwest`](https://github.com/06chaynes/http-cache/tree/main/http-cache-reqwest). This example will use the default mode, default cacache manager, and default http cache options.
2023

2124
After constructing our client, we will make a request to the [MDN Caching Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) which should result in an object stored in cache on disk.
2225

2326
```rust
2427
use reqwest::Client;
2528
use reqwest_middleware::{ClientBuilder, Result};
2629
use http_cache_reqwest::{Cache, CacheMode, CACacheManager, HttpCache, HttpCacheOptions};
30+
use std::path::PathBuf;
2731

2832
#[tokio::main]
2933
async fn main() -> Result<()> {
3034
let client = ClientBuilder::new(Client::new())
3135
.with(Cache(HttpCache {
3236
mode: CacheMode::Default,
33-
manager: CACacheManager::default(),
37+
manager: CACacheManager::new(PathBuf::from("./cache"), false),
3438
options: HttpCacheOptions::default(),
3539
}))
3640
.build();
@@ -57,7 +61,7 @@ http-cache-reqwest = { version = "1.0", features = ["streaming"] }
5761

5862
```rust
5963
use http_cache::StreamingManager;
60-
use http_cache_reqwest::StreamingCache;
64+
use http_cache_reqwest::{StreamingCache, CacheMode};
6165
use reqwest::Client;
6266
use reqwest_middleware::ClientBuilder;
6367
use std::path::PathBuf;
@@ -66,9 +70,9 @@ use futures_util::StreamExt;
6670
#[tokio::main]
6771
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
6872
// Create streaming cache manager
69-
let cache_manager = StreamingManager::new(PathBuf::from("./cache"), true);
70-
let streaming_cache = StreamingCache::new(cache_manager);
71-
73+
let cache_manager = StreamingManager::new(PathBuf::from("./cache"));
74+
let streaming_cache = StreamingCache::new(cache_manager, CacheMode::Default);
75+
7276
// Build client with streaming cache
7377
let client = ClientBuilder::new(Client::new())
7478
.with(streaming_cache)
@@ -117,13 +121,14 @@ This ensures that your application continues to work seamlessly even when using
117121
use reqwest::Client;
118122
use reqwest_middleware::ClientBuilder;
119123
use http_cache_reqwest::{Cache, CacheMode, CACacheManager, HttpCache, HttpCacheOptions};
124+
use std::path::PathBuf;
120125

121126
#[tokio::main]
122127
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
123128
let client = ClientBuilder::new(Client::new())
124129
.with(Cache(HttpCache {
125130
mode: CacheMode::Default,
126-
manager: CACacheManager::default(),
131+
manager: CACacheManager::new(PathBuf::from("./cache"), false),
127132
options: HttpCacheOptions::default(),
128133
}))
129134
.build();
@@ -152,13 +157,14 @@ use reqwest_middleware::ClientBuilder;
152157
use http_cache_reqwest::{Cache, CacheMode, CACacheManager, HttpCache, HttpCacheOptions};
153158
use futures_util::stream;
154159
use bytes::Bytes;
160+
use std::path::PathBuf;
155161

156162
#[tokio::main]
157163
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
158164
let client = ClientBuilder::new(Client::new())
159165
.with(Cache(HttpCache {
160166
mode: CacheMode::Default,
161-
manager: CACacheManager::default(),
167+
manager: CACacheManager::new(PathBuf::from("./cache"), false),
162168
options: HttpCacheOptions::default(),
163169
}))
164170
.build();

docs/src/clients/surf.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ cargo add http-cache-surf
1212

1313
- `manager-cacache`: (default) Enables the [`CACacheManager`](https://docs.rs/http-cache/latest/http_cache/struct.CACacheManager.html) backend cache manager.
1414
- `manager-moka`: Enables the [`MokaManager`](https://docs.rs/http-cache/latest/http_cache/struct.MokaManager.html) backend cache manager.
15+
- `manager-foyer`: Enables the [`FoyerManager`](https://docs.rs/http-cache/latest/http_cache/struct.FoyerManager.html) backend cache manager.
1516

1617
## Usage
1718

18-
In the following example we will construct our client with our cache struct from [`http-cache-surf`](https://github.com/06chaynes/http-cache/tree/latest/http-cache-surf). This example will use the default mode, default cacache manager, and default http cache options.
19+
In the following example we will construct our client with our cache struct from [`http-cache-surf`](https://github.com/06chaynes/http-cache/tree/main/http-cache-surf). This example will use the default mode, default cacache manager, and default http cache options.
1920

2021
After constructing our client, we will make a request to the [MDN Caching Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) which should result in an object stored in cache on disk.
2122

@@ -24,16 +25,17 @@ use http_cache_surf::{Cache, CacheMode, CACacheManager, HttpCache, HttpCacheOpti
2425
use surf::Client;
2526
use macro_rules_attribute::apply;
2627
use smol_macros::main;
28+
use std::path::PathBuf;
2729

2830
#[apply(main!)]
2931
async fn main() -> surf::Result<()> {
3032
let client = Client::new()
3133
.with(Cache(HttpCache {
3234
mode: CacheMode::Default,
33-
manager: CACacheManager::default(),
35+
manager: CACacheManager::new(PathBuf::from("./cache"), false),
3436
options: HttpCacheOptions::default(),
3537
}));
36-
38+
3739
client
3840
.get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching")
3941
.await?;

docs/src/clients/tower.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ cargo add http-cache-tower
1212

1313
- `manager-cacache`: (default) Enables the [`CACacheManager`](https://docs.rs/http-cache/latest/http_cache/struct.CACacheManager.html) backend cache manager.
1414
- `manager-moka`: Enables the [`MokaManager`](https://docs.rs/http-cache/latest/http_cache/struct.MokaManager.html) backend cache manager.
15+
- `manager-foyer`: Enables the [`FoyerManager`](https://docs.rs/http-cache/latest/http_cache/struct.FoyerManager.html) backend cache manager.
1516
- `streaming`: Enables streaming cache support for memory-efficient handling of large response bodies.
17+
- `rate-limiting`: Enables cache-aware rate limiting functionality.
18+
- `url-ada`: Enables ada-url for URL parsing.
1619

1720
## Basic Usage
1821

@@ -115,10 +118,11 @@ use http_cache::CACacheManager;
115118
use hyper_util::client::legacy::Client;
116119
use hyper_util::rt::TokioExecutor;
117120
use tower::{ServiceBuilder, ServiceExt};
121+
use std::path::PathBuf;
118122

119123
#[tokio::main]
120124
async fn main() -> Result<(), Box<dyn std::error::Error>> {
121-
let cache_manager = CACacheManager::default();
125+
let cache_manager = CACacheManager::new(PathBuf::from("./cache"), false);
122126
let cache_layer = HttpCacheLayer::new(cache_manager);
123127

124128
let client = Client::builder(TokioExecutor::new()).build_http();

docs/src/clients/ureq.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ Since ureq is a synchronous HTTP client, this implementation uses the [smol](htt
66

77
## Features
88

9-
- `json` - Enables JSON request/response support via `send_json()` and `into_json()` methods (requires `serde_json`)
10-
- `manager-cacache` - Enable [cacache](https://docs.rs/cacache/) cache manager (default)
11-
- `manager-moka` - Enable [moka](https://docs.rs/moka/) cache manager
9+
- `manager-cacache` (default): Enable [cacache](https://docs.rs/cacache/) cache manager.
10+
- `manager-moka`: Enable [moka](https://docs.rs/moka/) cache manager.
11+
- `manager-foyer`: Enable [foyer](https://github.com/foyer-rs/foyer) hybrid in-memory + disk cache manager.
12+
- `json`: Enables JSON request/response support via `send_json()` and `into_json()` methods (requires `serde_json`).
13+
- `rate-limiting`: Enable cache-aware rate limiting functionality.
14+
- `url-ada`: Enable ada-url for URL parsing.
1215

1316
## Basic Usage
1417

1518
Add the dependency to your `Cargo.toml`:
1619

1720
```toml
1821
[dependencies]
19-
http-cache-ureq = "1.0.0-alpha.1"
22+
http-cache-ureq = "1.0"
2023
```
2124

2225
Use the `CachedAgent` builder to create a cached HTTP client:
@@ -54,7 +57,7 @@ Enable the `json` feature to send and parse JSON data:
5457

5558
```toml
5659
[dependencies]
57-
http-cache-ureq = { version = "1.0.0-alpha.1", features = ["json"] }
60+
http-cache-ureq = { version = "1.0", features = ["json"] }
5861
```
5962

6063
```rust
@@ -141,7 +144,7 @@ Use the Moka in-memory cache:
141144

142145
```toml
143146
[dependencies]
144-
http-cache-ureq = { version = "1.0.0-alpha.1", features = ["manager-moka"] }
147+
http-cache-ureq = { version = "1.0", features = ["manager-moka"] }
145148
```
146149

147150
```rust

docs/src/development/supporting-a-backend-cache-manager.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ pub struct CacheMetadata {
102102
}
103103
```
104104

105-
This struct derives [serde](https://github.com/serde-rs/serde) Deserialize and Serialize to ease the serialization and deserialization with JSON for the streaming metadata, and [bincode](https://github.com/bincode-org/bincode) for the traditional Store struct.
105+
This struct derives [serde](https://github.com/serde-rs/serde) Deserialize and Serialize to ease the serialization and deserialization with JSON for the streaming metadata, and [postcard](https://github.com/jamesmunns/postcard) for the traditional Store struct.
106+
107+
**Important:** The `bincode` serialization format has been deprecated due to RUSTSEC-2025-0141 (bincode is unmaintained). New implementations should use `postcard` instead. The library still supports bincode through legacy feature flags (`manager-cacache-bincode`, `manager-moka-bincode`) for backward compatibility, but these will be removed in the next major version.
106108

107109
### Part Two: Implementing the traditional `CacheManager` trait
108110

@@ -126,7 +128,7 @@ impl CacheManager for CACacheManager {
126128
cache_key: &str,
127129
) -> Result<Option<(HttpResponse, CachePolicy)>> {
128130
let store: Store = match cacache::read(&self.path, cache_key).await {
129-
Ok(d) => bincode::deserialize(&d)?,
131+
Ok(d) => postcard::from_bytes(&d)?,
130132
Err(_e) => {
131133
return Ok(None);
132134
}
@@ -141,7 +143,7 @@ impl CacheManager for CACacheManager {
141143
policy: CachePolicy,
142144
) -> Result<HttpResponse> {
143145
let data = Store { response, policy };
144-
let bytes = bincode::serialize(&data)?;
146+
let bytes = postcard::to_allocvec(&data)?;
145147
cacache::write(&self.path, cache_key, bytes).await?;
146148
Ok(data.response)
147149
}
@@ -273,6 +275,7 @@ async fn put<B>(
273275
response: Response<B>,
274276
policy: CachePolicy,
275277
_request_url: Url,
278+
_metadata: Option<Vec<u8>>,
276279
) -> Result<Response<Self::Body>>
277280
where
278281
B: http_body::Body + Send + 'static,

docs/src/development/supporting-an-http-client.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The [`Middleware`](https://docs.rs/http-cache/latest/http_cache/trait.Middleware
1212
- `policy`: returns a [`CachePolicy`](https://docs.rs/http-cache-semantics/latest/http_cache_semantics/struct.CachePolicy.html) with default options for the given `HttpResponse`
1313
- `policy_with_options`: returns a [`CachePolicy`](https://docs.rs/http-cache-semantics/latest/http_cache_semantics/struct.CachePolicy.html) with the provided [`CacheOptions`](https://docs.rs/http-cache-semantics/latest/http_cache_semantics/struct.CacheOptions.html) for the given `HttpResponse`
1414
- `update_headers`: updates the request headers with the provided [`http::request::Parts`](https://docs.rs/http/latest/http/request/struct.Parts.html)
15-
- `force_no_cache`: overrides the `Cache-Control` header to 'no-cache' derective
15+
- `force_no_cache`: overrides the `Cache-Control` header to 'no-cache' directive
1616
- `parts`: returns the [`http::request::Parts`](https://docs.rs/http/latest/http/request/struct.Parts.html) from the request
1717
- `url`: returns the requested [`Url`](https://docs.rs/url/latest/url/struct.Url.html)
1818
- `method`: returns the method of the request as a `String`
@@ -36,7 +36,7 @@ The `update_headers` method is used to update the request headers with the provi
3636

3737
### The `force_no_cache` method
3838

39-
The `force_no_cache` method is used to override the `Cache-Control` header to 'no-cache' derective. This is used to allow caching but force revalidation before resuse.
39+
The `force_no_cache` method is used to override the `Cache-Control` header to 'no-cache' directive. This is used to allow caching but force revalidation before reuse.
4040

4141
### The `parts` method
4242

@@ -135,7 +135,7 @@ fn update_headers(&mut self, parts: &Parts) -> Result<()> {
135135
}
136136
```
137137

138-
The `force_no_cache` method is used to override the `Cache-Control` header in the request to 'no-cache' derective. This is used to allow caching but force revalidation before resuse.
138+
The `force_no_cache` method is used to override the `Cache-Control` header in the request to 'no-cache' directive. This is used to allow caching but force revalidation before reuse.
139139

140140
```rust
141141
fn force_no_cache(&mut self) -> Result<()> {

docs/src/managers/cacache.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ cargo add http-cache-tower
2626

2727
## Working with the manager directly
2828

29-
First construct your manager instance. This example will use the default cache directory.
29+
First construct your manager instance. You need to specify the cache directory and whether cache entries should be fully removed from disk.
3030

3131
```rust
32-
let manager = CACacheManager::default();
32+
use std::path::PathBuf;
33+
34+
let manager = CACacheManager::new(PathBuf::from("./my-cache"), false);
3335
```
3436

35-
You can also specify the cache directory and if you want the cache entries to be removed fully from disk.
37+
The second argument (`remove_fully`) controls whether cached entries are completely removed from disk when deleted:
3638

3739
```rust
38-
let manager = CACacheManager::new("./my-cache".into(), true);
40+
// Keep cache metadata on delete (faster, uses more disk space)
41+
let manager = CACacheManager::new(PathBuf::from("./my-cache"), false);
42+
43+
// Fully remove entries from disk on delete (slower, saves disk space)
44+
let manager = CACacheManager::new(PathBuf::from("./my-cache"), true);
3945
```
4046

4147
You can attempt to retrieve a record from the cache using the `get` method. This method accepts a `&str` as the cache key and returns an `Result<Option<(HttpResponse, CachePolicy)>, BoxError>`.

0 commit comments

Comments
 (0)