Skip to content

Commit 854fc85

Browse files
committed
Add description of more of the features to the readme
1 parent 8c79824 commit 854fc85

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,63 @@ services.AddHttpClient("cachedClient")
6262

6363
`FileCache` implements `IHttpCache` and this is an extension point if one wants to use a different, non-file based cache implementation.
6464

65+
## Private Cache Handling
66+
67+
`HttpClient.Cache` supports the concept of private caches, ensuring that cached responses are only reused for the same user or context that originally requested them.
68+
This is particularly important for scenarios where responses may contain user-specific or sensitive data.
69+
70+
### How Private Caches Work
71+
72+
- **Detection:**
73+
The library inspects HTTP response headers such as `Cache-Control: private` to determine if a response should be stored in a private cache.
74+
75+
- **Isolation:**
76+
Private cache entries are isolated based on a user identifier computed from the request. This prevents one user's cached data from being served to another user.
77+
78+
- **Retrieval:**
79+
When a request is made, the cache handler checks for a matching private cache entry using the current user's context. Only if a valid private entry exists for that user will it be returned.
80+
81+
### Example
82+
83+
If a request have an `Authorization` header with a JWT and a response contains the header `Cache-Control: private`, the cache handler will:
84+
85+
1. Compute a cache key that includes the user identifier - since the `Authorization` header is a JWT, it will use the `sub` or `client_id` claim from the token. This behavior can be changed to any other header or set of claims by using a different [`ICacheKeyComputer`](src/HttpClient.Cache/ICacheKeyComputer.cs) other than the default [`CacheKeyComputer`](src/HttpClient.Cache/CacheKeyComputer.cs).
86+
2. Store the response in a location computed from the computed cache key.
87+
3. On subsequent requests, retrieve the cached response only if the same cache key is computed.
88+
89+
This approach ensures compliance with HTTP caching semantics and protects user privacy by preventing cross-user cache leaks.
90+
The mechanism by deriving the `sub`/`client_id` allows for using the cache even if the JWT token for the same user is renewed.
91+
92+
## Response Variation Handling
93+
94+
`HttpClient.Cache` fully supports the HTTP `Vary` header, which instructs caches to store and serve different versions of a response based on specified request headers. This ensures that clients receive the correct cached response variant according to their request characteristics.
95+
96+
### How Vary Headers Are Handled
97+
98+
- **Detection:**
99+
When a response includes a `Vary` header (e.g., `Vary: Accept-Encoding, User-Agent`), the cache handler records which request headers affect the cache entry.
100+
101+
- **Cache Key Computation:**
102+
The cache key is computed by combining the request URI with the values of all headers listed in the `Vary` header. This means each unique combination of those header values results in a separate cache entry.
103+
104+
- **Storage:**
105+
Each variant is stored independently, ensuring that responses tailored for different header values (such as language, encoding, or user agent) are not mixed.
106+
107+
- **Retrieval:**
108+
On subsequent requests, the cache handler checks the current request’s headers against the `Vary` criteria and retrieves the correct cached variant if available.
109+
110+
### Example
111+
112+
If a server responds with:
113+
114+
```
115+
Vary: Accept-Language
116+
```
117+
118+
The cache will store separate entries for each value of the `Accept-Language` header (e.g., `en-US`, `da-DK`). When a client requests the same resource with a different `Accept-Language`, the appropriate cached variant is served.
119+
120+
This mechanism ensures compliance with HTTP caching standards and delivers accurate, context-sensitive responses from the cache.
121+
65122
## Key Components
66123

67124
- [`CacheHandler`](src/HttpClient.Cache/CacheHandler.cs): HTTP message handler that manages caching logic. This handler can be registered on a `HttpClient` as a message handler layer.

0 commit comments

Comments
 (0)