Skip to content

Commit 9a0203b

Browse files
committed
Update docs
1 parent 0d6cec9 commit 9a0203b

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ with_serde = ["dep:serde", "dep:http-serde"]
2929

3030
[package.metadata.docs.rs]
3131
targets = ["x86_64-unknown-linux-gnu"]
32+
rustdoc-args = ["--generate-link-to-definition"]
3233

3334
[badges]
3435
maintenance = { status = "passively-maintained" }

README.md

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Can I cache this?
22

3-
`CachePolicy` tells when responses can be reused from a cache, taking into account [HTTP RFC 7234](http://httpwg.org/specs/rfc7234.html) rules for user agents and shared caches. It's aware of many tricky details such as the `Vary` header, proxy revalidation, and authenticated responses.
3+
`CachePolicy` tells when responses can be reused from a cache, taking into account [HTTP RFC 7234/9111](http://httpwg.org/specs/rfc9111.html) rules for user agents and shared caches. It's aware of many tricky details such as the `Vary` header, age updates, proxy revalidation, and authenticated responses.
44

55
## Usage
66

@@ -12,57 +12,42 @@ The key method is `before_request(new_request)`, which checks whether the `new_r
1212

1313
### Options
1414

15-
If `options.shared` is `true` (default), then the response is evaluated from a perspective of a shared cache (i.e. `private` is not cacheable and `s-maxage` is respected). If `options.shared` is `false`, then the response is evaluated from a perspective of a single-user cache (i.e. `private` is cacheable and `s-maxage` is ignored). `shared: true` is recommended for HTTP clients.
15+
If `options.shared` is `true` (default), then the response is evaluated from a perspective of a shared cache (i.e. `private` is not cacheable and `s-maxage` is respected). If `options.shared` is `false`, then the response is evaluated from a perspective of a single-user cache (i.e. `private` is cacheable and `s-maxage` is ignored). `shared: true` is recommended for HTTP proxies, and `false` for single-user clients.
1616

17-
`options.cache_heuristic` is a fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), e.g. if a file hasn't been modified for 100 days, it'll be cached for 100\*0.1 = 10 days.
17+
`options.cache_heuristic` is a fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), e.g. if a file hasn't been modified for 100 days, it'll be cached for 100×0.1 = 10 days.
1818

1919
`options.immutable_min_time_to_live` is a duration to assume as the default time to cache responses with `Cache-Control: immutable`. Note that [per RFC](http://httpwg.org/http-extensions/immutable.html) these can become stale, so `max-age` still overrides the default.
2020

2121
If `options.ignore_cargo_cult` is true, common anti-cache directives will be completely ignored if the non-standard `pre-check` and `post-check` directives are present. These two useless directives are most commonly found in bad StackOverflow answers and PHP's "session limiter" defaults.
2222

23-
### `storable()`
23+
### `is_storable()`
2424

2525
Returns `true` if the response can be stored in a cache. If it's `false` then you MUST NOT store either the request or the response.
2626

2727
### `before_request(new_request)`
2828

2929
This is the most important method. Use this method to check whether the cached response is still fresh in the context of the new request.
3030

31-
If it returns `true`, then the given `request` matches the original response this cache policy has been created with, and the response can be reused without contacting the server. Note that the old response can't be returned without being updated, see `cached_response()`.
31+
If it returns `Fresh`, then the given `request` matches the original response this cache policy has been created with, and the response can be reused without contacting the server. This will contain an updated, filtered set of response headers to return to clients receiving the cached response. This processing is necessary, because proxies MUST always remove hop-by-hop headers (such as `TE` and `Connection`) and update response's `Age` to avoid doubling cache time.
3232

33-
If it returns `false`, then the response may not be matching at all (e.g. it's for a different URL or method), or may require to be refreshed first (see `revalidation_request()`).
34-
35-
### `cached_response()`
36-
37-
Returns updated, filtered set of response headers to return to clients receiving the cached response. This function is necessary, because proxies MUST always remove hop-by-hop headers (such as `TE` and `Connection`) and update response's `Age` to avoid doubling cache time.
33+
If it returns `Stale`, then the response may not be matching at all (e.g. it's for a different URL or method), or may require to be refreshed first. The variant will contain HTTP headers for making a revalidation request to the server.
3834

3935
### `time_to_live()`
4036

41-
Returns approximate time until the response becomes stale (i.e. not fresh).
37+
Returns approximate time until the response becomes stale (i.e. not fresh). This is equivalent of `max-age`, but with appropriate time correction applied.
4238

43-
After that time (when `time_to_live() <= 0`) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with `before_request()`.
39+
After that time (when `time_to_live() == Duration::ZERO`) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with `before_request()`.
4440

4541
### Refreshing stale cache (revalidation)
4642

4743
When a cached response has expired, it can be made fresh again by making a request to the origin server. The server may respond with status 304 (Not Modified) without sending the response body again, saving bandwidth.
4844

49-
The following methods help perform the update efficiently and correctly.
50-
51-
#### `revalidation_request(new_request)`
52-
53-
Returns updated, filtered set of request headers to send to the origin server to check if the cached response can be reused. These headers allow the origin server to return status 304 indicating the response is still fresh. All headers unrelated to caching are passed through as-is.
54-
55-
Use this method when updating cache from the origin server.
56-
5745
#### `after_response(revalidation_request, revalidation_response)`
5846

59-
Use this method to update the cache after receiving a new response from the origin server. It returns an object with:
60-
61-
- `policy` — A new `CachePolicy` with HTTP headers updated from `revalidation_response`. You can always replace the old cached `CachePolicy` with the new one.
62-
- `modified` — Boolean indicating whether the response body has changed.
63-
- If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
64-
- If `true`, you should use new response's body (if present), or make another request to the origin server without any conditional headers (i.e. don't use `revalidation_request()` this time) to get the new resource.
47+
Use this method to update the cache after receiving a new response from the origin server. It returns `Modified`/`NotModified` object with a new `CachePolicy` with HTTP headers updated from `revalidation_response`. You can always replace the old cached `CachePolicy` with the new one.
6548

49+
- If `NotModified`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
50+
- If `Modified`, you should replace the old cached body with the new response's body.
6651

6752
# Yo, FRESH
6853

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub struct CacheOptions {
120120
pub shared: bool,
121121
/// `cache_heuristic` is a fraction of response's age that is used as a
122122
/// fallback cache duration. The default is 0.1 (10%), e.g. if a file
123-
/// hasn't been modified for 100 days, it'll be cached for 100*0.1 = 10
123+
/// hasn't been modified for 100 days, it'll be cached for 100×0.1 = 10
124124
/// days.
125125
pub cache_heuristic: f32,
126126
/// `immutable_min_time_to_live` is a duration to assume as the
@@ -296,11 +296,11 @@ impl CachePolicy {
296296
/// Returns whether the cached response is still fresh in the context of
297297
/// the new request.
298298
///
299-
/// If it returns `true`, then the given request matches the original
299+
/// If it returns `Fresh`, then the given request matches the original
300300
/// response this cache policy has been created with, and the response can
301301
/// be reused without contacting the server.
302302
///
303-
/// If it returns `false`, then the response may not be matching at all
303+
/// If it returns `Stale`, then the response may not be matching at all
304304
/// (e.g. it's for a different URL or method), or may require to be
305305
/// refreshed first. Either way, the new request's headers will have been
306306
/// updated for sending it to the origin server.
@@ -586,13 +586,17 @@ impl CachePolicy {
586586
default_min_ttl
587587
}
588588

589-
/// Returns approximate time in _milliseconds_ until the response becomes
590-
/// stale (i.e. not fresh).
589+
/// Returns approximate time until the response becomes
590+
/// stale (i.e. not fresh). This is the correct way of getting the current `max-age` value.
591591
///
592-
/// After that time (when `time_to_live() <= 0`) the response might not be
592+
/// After that time (when `time_to_live() == Duration::ZERO`) the response might not be
593593
/// usable without revalidation. However, there are exceptions, e.g. a
594594
/// client can explicitly allow stale responses, so always check with
595595
/// `before_request()`.
596+
///
597+
/// If you're storing responses in a cache/database, keep them approximately for
598+
/// the `time_to_live` duration plus some extra time to allow for revalidation
599+
/// (an expired response is still useful).
596600
pub fn time_to_live(&self, now: SystemTime) -> Duration {
597601
self.max_age()
598602
.checked_sub(self.age(now))
@@ -612,6 +616,8 @@ impl CachePolicy {
612616
///
613617
/// It returns request "parts" without a body. You can upgrade it to a full
614618
/// response with `Request::from_parts(parts, BYOB)` (the body is usually `()`).
619+
///
620+
/// You don't need this if you use [`before_request()`]
615621
fn revalidation_request<Req: RequestLike>(&self, incoming_req: &Req) -> http::request::Parts {
616622
let mut headers = Self::copy_without_hop_by_hop_headers(incoming_req.headers());
617623

0 commit comments

Comments
 (0)