@warlock.js/cache 5.28.0 → 5.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/llms-full.txt +105 -103
- package/llms.txt +18 -18
- package/package.json +3 -3
- package/skills/apply-cache-patterns/SKILL.md +10 -10
- package/skills/cache-basics/SKILL.md +22 -20
- package/skills/configure-pg-cache/SKILL.md +5 -5
- package/skills/configure-set-options/SKILL.md +3 -3
- package/skills/handle-cache-errors/SKILL.md +3 -3
- package/skills/observe-cache/SKILL.md +1 -1
- package/skills/overview/SKILL.md +21 -21
- package/skills/pick-cache-driver/SKILL.md +4 -4
- package/skills/test-cache-code/SKILL.md +2 -2
- package/skills/use-cache-atomic/SKILL.md +5 -5
- package/skills/use-cache-bulk/SKILL.md +5 -5
- package/skills/use-cache-list/SKILL.md +2 -2
- package/skills/use-cache-lock/SKILL.md +2 -2
- package/skills/use-cache-namespace/SKILL.md +4 -4
- package/skills/use-cache-similarity/SKILL.md +2 -2
- package/skills/use-cache-tags/SKILL.md +4 -4
- package/skills/use-cache-update-merge/SKILL.md +1 -1
- package/skills/use-cache-utils/SKILL.md +4 -4
- package/skills/use-cached-hof/SKILL.md +2 -2
- package/skills/use-swr/SKILL.md +3 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cache-namespace
|
|
3
|
-
description: 'Scope cache keys via cache.namespace(prefix, options?) — every key auto-prefixed, scope-level ttl / tags defaults, nested scopes, .clear() sugar. Triggers: `cache.namespace`, `cache.removeNamespace`, `clear`, `globalPrefix`; "scope cache keys under a prefix", "share TTL across a whole prefix", "drop every key under user.1", "nested cache scopes"; typical import `import { cache } from "@warlock.js/cache"`. Skip: tag-based bulk drop —
|
|
3
|
+
description: 'Scope cache keys via cache.namespace(prefix, options?) — every key auto-prefixed, scope-level ttl / tags defaults, nested scopes, .clear() sugar. Triggers: `cache.namespace`, `cache.removeNamespace`, `clear`, `globalPrefix`; "scope cache keys under a prefix", "share TTL across a whole prefix", "drop every key under user.1", "nested cache scopes"; typical import `import { cache } from "@warlock.js/cache"`. Skip: tag-based bulk drop — the `use-cache-tags` topic; multi-tenant driver-level prefix — the `pick-cache-driver` topic; SWR — the `use-swr` topic; competing libs `keyv` namespaces.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Scoped caches — `cache.namespace(prefix, options?)`
|
|
@@ -51,11 +51,11 @@ await cache.set("user:2:profile", otherProfile);
|
|
|
51
51
|
await cache.removeNamespace("user.1"); // drops both user:1 entries, keeps user:2
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
Cheaper than tags (no reverse index to maintain). Every real driver supports it — memory family and `lru` by prefix-scan, `file` by directory (including dotted sibling keys like `ns.a`), `redis` by non-blocking `SCAN` (not `KEYS`), `pg` by key/`LIKE` prefix; `null` no-ops. Namespace strings are treated as data, not glob/path input — see
|
|
54
|
+
Cheaper than tags (no reverse index to maintain). Every real driver supports it — memory family and `lru` by prefix-scan, `file` by directory (including dotted sibling keys like `ns.a`), `redis` by non-blocking `SCAN` (not `KEYS`), `pg` by key/`LIKE` prefix; `null` no-ops. Namespace strings are treated as data, not glob/path input — see the `pick-cache-driver` topic for the file-path-containment and redis-glob-escaping details.
|
|
55
55
|
|
|
56
56
|
## Multi-tenant scoping at the driver level
|
|
57
57
|
|
|
58
|
-
Instead of every call passing a tenant prefix, attach `globalPrefix` to the driver config — see
|
|
58
|
+
Instead of every call passing a tenant prefix, attach `globalPrefix` to the driver config — see the `pick-cache-driver` topic. Function form runs per call.
|
|
59
59
|
|
|
60
60
|
```ts
|
|
61
61
|
options: {
|
|
@@ -79,7 +79,7 @@ await feed.swr(
|
|
|
79
79
|
// stored at feed.<userId>.home, tagged [user.<userId>, computed]
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
Note: scope `ttl` defaults are NOT applied to SWR — `freshTtl` / `staleTtl` always come from the call site. See
|
|
82
|
+
Note: scope `ttl` defaults are NOT applied to SWR — `freshTtl` / `staleTtl` always come from the call site. See the `use-swr` topic.
|
|
83
83
|
|
|
84
84
|
## Things NOT to do
|
|
85
85
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cache-similarity
|
|
3
|
-
description: 'Vector retrieval via cache.similar(vector, {topK, threshold?, tags?}) — index with set(k, v, {vector}), query nearest by cosine similarity. Triggers: `cache.similar`, `cache.set` with `vector`, `topK`, `threshold`, `tags`, `cosineSimilarity`; "build a semantic cache for an LLM", "RAG retrieval from cache", "nearest-neighbor over cached entries", "skip the LLM when a similar answer exists"; typical import `import { cache } from "@warlock.js/cache"`. Skip: pgvector setup specifics —
|
|
3
|
+
description: 'Vector retrieval via cache.similar(vector, {topK, threshold?, tags?}) — index with set(k, v, {vector}), query nearest by cosine similarity. Triggers: `cache.similar`, `cache.set` with `vector`, `topK`, `threshold`, `tags`, `cosineSimilarity`; "build a semantic cache for an LLM", "RAG retrieval from cache", "nearest-neighbor over cached entries", "skip the LLM when a similar answer exists"; typical import `import { cache } from "@warlock.js/cache"`. Skip: pgvector setup specifics — the `configure-pg-cache` topic; competing libs `pinecone`, `weaviate`, `chromadb`, `lancedb`, `faiss-node`.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# `cache.similar()` — vector-based retrieval
|
|
@@ -83,7 +83,7 @@ options: { memory: { ttl: "1h" } }
|
|
|
83
83
|
options: { pg: { client: pool, vector: { dimensions: 1536 } } }
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
See
|
|
86
|
+
See the `configure-pg-cache` topic.
|
|
87
87
|
|
|
88
88
|
## Things NOT to do
|
|
89
89
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cache-tags
|
|
3
|
-
description: 'Tag-based invalidation — attach tags on write, then cache.tags([...]).invalidate() drops every key bound to any of those tags. Triggers: `cache.tags`, `invalidate`, `cache.set` with `tags`; "invalidate every key tagged users", "drop everything for tenant 42", "bulk cache invalidation without knowing keys", "tag a cached value"; typical import `import { cache } from "@warlock.js/cache"`. Skip: prefix-based drop —
|
|
3
|
+
description: 'Tag-based invalidation — attach tags on write, then cache.tags([...]).invalidate() drops every key bound to any of those tags. Triggers: `cache.tags`, `invalidate`, `cache.set` with `tags`; "invalidate every key tagged users", "drop everything for tenant 42", "bulk cache invalidation without knowing keys", "tag a cached value"; typical import `import { cache } from "@warlock.js/cache"`. Skip: prefix-based drop — the `use-cache-namespace` topic; HOF memoization with tags — the `use-cached-hof` topic; SWR — the `use-swr` topic; competing libs `cache-manager` tags, Next.js `revalidateTag`.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Tag-based invalidation
|
|
@@ -36,7 +36,7 @@ Multi-tag is **union** semantics: an entry is invalidated if it carries **at lea
|
|
|
36
36
|
|
|
37
37
|
| Use case | Reach for |
|
|
38
38
|
| --- | --- |
|
|
39
|
-
| The keys share a known prefix | `cache.removeNamespace("prefix")` (
|
|
39
|
+
| The keys share a known prefix | `cache.removeNamespace("prefix")` (the `use-cache-namespace` topic) |
|
|
40
40
|
| The keys are spread across prefixes, tied by entity | Tags |
|
|
41
41
|
| Both apply | Tags — more flexible; cheap on most drivers |
|
|
42
42
|
|
|
@@ -68,7 +68,7 @@ await cache.swr(
|
|
|
68
68
|
);
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
Tags re-apply on every successful refresh — see
|
|
71
|
+
Tags re-apply on every successful refresh — see the `use-swr` topic.
|
|
72
72
|
|
|
73
73
|
## `cached()` HOF with tags
|
|
74
74
|
|
|
@@ -79,7 +79,7 @@ const getPosts = cached(fn, { key: (u) => `posts.by.${u}`, ttl: "30m", tags: ["u
|
|
|
79
79
|
await cache.tags(["users"]).invalidate(); // drops both wrappers' caches
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
See
|
|
82
|
+
See the `use-cached-hof` topic.
|
|
83
83
|
|
|
84
84
|
## Things NOT to do
|
|
85
85
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cache-update-merge
|
|
3
|
-
description: 'Atomic read-modify-write via cache.update(key, fn) (callback receives current) or cache.merge(key, partial) (shallow merge). Per-key chain lock serializes concurrent in-process callers. Triggers: `cache.update`, `cache.merge`, `cache.increment`, `cache.pull`; "atomically update a cached counter", "change one field on a cached object", "avoid get-spread-set race", "serialize concurrent cache writers"; typical import `import { cache } from "@warlock.js/cache"`. Skip: cross-process locking —
|
|
3
|
+
description: 'Atomic read-modify-write via cache.update(key, fn) (callback receives current) or cache.merge(key, partial) (shallow merge). Per-key chain lock serializes concurrent in-process callers. Triggers: `cache.update`, `cache.merge`, `cache.increment`, `cache.pull`; "atomically update a cached counter", "change one field on a cached object", "avoid get-spread-set race", "serialize concurrent cache writers"; typical import `import { cache } from "@warlock.js/cache"`. Skip: cross-process locking — the `use-cache-lock` topic; conditional create/update — the `configure-set-options` topic; competing libs `lodash.merge`, raw redis `WATCH`/`MULTI`.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# `update` and `merge` — atomic read-modify-write
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cache-utils
|
|
3
|
-
description: 'Low-level cache utilities re-exported from @warlock.js/cache — parseTtl, expiresAtToTtl, resolveTtl, normalizeToOptions, normalizeToRememberOptions, parseCacheKey, mergeTagSets, injectTags, cosineSimilarity, and the CACHE_FOR TTL enum. Triggers: `parseTtl`, `parseCacheKey`, `resolveTtl`, `expiresAtToTtl`, `cosineSimilarity`, `mergeTagSets`, `injectTags`, `CACHE_FOR`; "parse a duration to seconds", "build a cache key from an object", "score two vectors", "common TTL constant"; typical import `import { parseTtl, CACHE_FOR } from "@warlock.js/cache"`. Skip: building a whole custom driver —
|
|
3
|
+
description: 'Low-level cache utilities re-exported from @warlock.js/cache — parseTtl, expiresAtToTtl, resolveTtl, normalizeToOptions, normalizeToRememberOptions, parseCacheKey, mergeTagSets, injectTags, cosineSimilarity, and the CACHE_FOR TTL enum. Triggers: `parseTtl`, `parseCacheKey`, `resolveTtl`, `expiresAtToTtl`, `cosineSimilarity`, `mergeTagSets`, `injectTags`, `CACHE_FOR`; "parse a duration to seconds", "build a cache key from an object", "score two vectors", "common TTL constant"; typical import `import { parseTtl, CACHE_FOR } from "@warlock.js/cache"`. Skip: building a whole custom driver — the `pick-cache-driver` topic; the high-level set API — the `configure-set-options` topic.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Cache utilities
|
|
@@ -84,6 +84,6 @@ For most call sites the duration string (`"1h"`, `"7d"`) reads better.
|
|
|
84
84
|
|
|
85
85
|
## See also
|
|
86
86
|
|
|
87
|
-
-
|
|
88
|
-
-
|
|
89
|
-
-
|
|
87
|
+
- The `configure-set-options` topic — the high-level `set` options these normalize
|
|
88
|
+
- The `use-cache-similarity` topic — `cache.similar()`, which uses `cosineSimilarity`
|
|
89
|
+
- The `pick-cache-driver` topic — building a custom driver where these help
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-cached-hof
|
|
3
|
-
description: 'Wrap an async function with cached(fn, options) — declare the caching strategy once, call from many sites, get a bound .invalidate(...args) helper. Triggers: `cached`, `invalidate`, `key`, `ttl`, `tags`, `driver`; "wrap a DB lookup with caching", "memoize a function and invalidate by args", "one declaration many call sites", "auto-derive cache key from args"; typical import `import { cached } from "@warlock.js/cache"`. Skip: one-shot memoization —
|
|
3
|
+
description: 'Wrap an async function with cached(fn, options) — declare the caching strategy once, call from many sites, get a bound .invalidate(...args) helper. Triggers: `cached`, `invalidate`, `key`, `ttl`, `tags`, `driver`; "wrap a DB lookup with caching", "memoize a function and invalidate by args", "one declaration many call sites", "auto-derive cache key from args"; typical import `import { cached } from "@warlock.js/cache"`. Skip: one-shot memoization — the `apply-cache-patterns` topic (`cache.remember`); tag bulk drop — the `use-cache-tags` topic; competing libs `p-memoize`, `mem`, `lodash.memoize`.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# `cached()` — function-memoization wrapper
|
|
@@ -79,7 +79,7 @@ const getPosts = cached(fn, { key: (u) => `posts.by.${u}`, ttl: "30m", tags: ["u
|
|
|
79
79
|
await cache.tags(["users"]).invalidate(); // drops both wrappers' caches
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
See
|
|
82
|
+
See the `use-cache-tags` topic.
|
|
83
83
|
|
|
84
84
|
### Project a subset of args into the key
|
|
85
85
|
|
package/skills/use-swr/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: use-swr
|
|
3
|
-
description: 'Stale-while-revalidate via cache.swr(key, {freshTtl, staleTtl}, fn) — returns cached instantly when fresh, returns cached + background refresh when stale, blocks only when fully expired. Triggers: `cache.swr`, `freshTtl`, `staleTtl`, `tags`, `driver`, `stale_at`; "serve stale while refreshing", "degrade when upstream is down", "never block on cache miss"; typical import `import { cache } from "@warlock.js/cache"`. Skip: block-until-fresh memoization —
|
|
3
|
+
description: 'Stale-while-revalidate via cache.swr(key, {freshTtl, staleTtl}, fn) — returns cached instantly when fresh, returns cached + background refresh when stale, blocks only when fully expired. Triggers: `cache.swr`, `freshTtl`, `staleTtl`, `tags`, `driver`, `stale_at`; "serve stale while refreshing", "degrade when upstream is down", "never block on cache miss"; typical import `import { cache } from "@warlock.js/cache"`. Skip: block-until-fresh memoization — the `apply-cache-patterns` topic; observing refresh failures — the `observe-cache` topic; competing libs `swr` (React, client-side).'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Stale-while-revalidate — `cache.swr(key, options, fn)`
|
|
@@ -11,7 +11,7 @@ Returns the cached value immediately when it can; refreshes in the background wh
|
|
|
11
11
|
|
|
12
12
|
Use `cache.swr()` when **slightly-stale data is acceptable** and **the upstream is slow / occasionally fails**. That's most product-detail pages, dashboards, third-party API responses, expensive aggregations.
|
|
13
13
|
|
|
14
|
-
Use `cache.remember()` when freshness is non-negotiable — auth, balances, billing, anything where the user must see the latest. Remember blocks every miss; SWR doesn't. See
|
|
14
|
+
Use `cache.remember()` when freshness is non-negotiable — auth, balances, billing, anything where the user must see the latest. Remember blocks every miss; SWR doesn't. See the `apply-cache-patterns` topic.
|
|
15
15
|
|
|
16
16
|
## Three windows
|
|
17
17
|
|
|
@@ -51,7 +51,7 @@ await cache.swr(
|
|
|
51
51
|
|
|
52
52
|
1. **Concurrent stale-window callers share one refresh.** Per-key dedupe via the driver's existing locks map — no thundering herd on background refresh.
|
|
53
53
|
2. **Failed background refreshes preserve the stale entry.** No retry storm; the next stale-window read tries again. Failures emit `error` events for observability.
|
|
54
|
-
3. **The caller never sees a refresh failure.** If you returned the stale value, you got your data — failures only show up via `cache.on("error", ...)`. See
|
|
54
|
+
3. **The caller never sees a refresh failure.** If you returned the stale value, you got your data — failures only show up via `cache.on("error", ...)`. See the `observe-cache` topic.
|
|
55
55
|
4. **Tags compose.** Per-call tags + scope tags (when via `cache.namespace().swr(...)`) merge additively.
|
|
56
56
|
5. **Scope `ttl` defaults are NOT applied to SWR.** `freshTtl` / `staleTtl` always come from the call site.
|
|
57
57
|
|