You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `cache` hook caches `get` and `find` results based on `params`. On mutating methods (`create`, `update`, `patch`, `remove`), affected cache entries are automatically invalidated.
@@ -15,11 +17,58 @@ The `cache` hook caches `get` and `find` results based on `params`. On mutating
15
17
16
18
## Options
17
19
18
-
| Option | Type | Description |
19
-
| --- | --- | --- |
20
-
|`map`|`Cache`| The cache implementation. Must implement `get`, `set`, `delete`, `clear`, and `keys`. |
21
-
|`id`|`string`| The id field to use. Defaults to `service.options.id`, then `'id'`. |
22
-
|`transformParams`|`(params) => params`| Transform params before they are used as cache key. Use this to exclude properties like `paginate` or `user` from the cache key. |
|`map`|`Cache`| The cache implementation. Must implement `get`, `set`, `delete`, `clear`, and `keys`. |
23
+
|`id`|`string`| The id field to use. Defaults to `service.options.id`, then `'id'`. |
24
+
|`transformParams`|`(params) => params`| Transform params before they are used as cache key. Compose it with [`passParams`](/utils/pass-params) to declaratively pick/drop keys and avoid false hits — see [Choosing Cache-Relevant Params](#choosing-cache-relevant-params-with-passparams). |
Deciding which `params` keys form the cache key is the trickiest part of caching, and the two failure modes are asymmetric:
29
+
30
+
-**False hits (dangerous):** if a key that affects the result is left out (e.g. `user`/tenant, `provider`), two semantically different requests collapse to the same key — one user can be served another user's cached data.
31
+
-**False misses (wasteful):** if a per-request/metrics key is included (e.g. `rateLimit`), every request produces a unique key and the cache never hits. A function-valued key (e.g. `stashed` from `stashable`) would even make serialization throw.
32
+
33
+
The [`passParams`](/utils/pass-params) utility makes this explicit and safe. It takes a declarative path schema (`true` include, `false` drop, or a predicate/projection function). `query` is always included by default, and keys you never classified are **kept by default** — the safe direction, since a forgotten key causes at worst a harmless cache miss, never a false hit.
34
+
35
+
> Transient keys that feathers-utils' own hooks attach to `params` — `rateLimit` (`rateLimit`), `skipHooks` (`skippable`/`addSkip`), the `stashed` function and `_stashable` flag (`stashable`) — are never cache-relevant. Drop them with `false`, or keep only what you list via `dropUnknownParams: true`.
36
+
37
+
### Exclude specific params (default)
38
+
39
+
Cache on everything except the keys you explicitly drop with `false`. This is the default direction — safe against false hits:
Set `dropUnknownParams: true` so only `query` (always) and the listed paths form the cache key. `user.id` is picked via dot-notation so different tenants never collide and per-request `user` fields don't bloat the key. Use `onUnknownParams` to log anything that was dropped:
54
+
55
+
```ts
56
+
import { passParams } from'feathers-utils/utils'
57
+
58
+
cache({
59
+
map: newMap(),
60
+
transformParams: (params) =>
61
+
passParams(
62
+
params,
63
+
{ 'user.id': true }, // `query` is included automatically
0 commit comments