| name | use-cache-utils |
| 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 — `@warlock.js/cache/pick-cache-driver/SKILL.md`; the high-level set API — `@warlock.js/cache/configure-set-options/SKILL.md`. |
Cache utilities
The helpers the drivers are built from, all re-exported from
@warlock.js/cache. You rarely need them at the call site — cache.set("k", v, "1h") parses the duration for you. They earn their keep when you write a custom
driver or do cache-adjacent work outside the manager.
TTL helpers
import { parseTtl, expiresAtToTtl, resolveTtl } from "@warlock.js/cache";
parseTtl(3600);
parseTtl("1h");
parseTtl(Infinity);
parseTtl(-5);
expiresAtToTtl(new Date(Date.now() + 60_000));
expiresAtToTtl(Date.now() - 1000);
resolveTtl("1h", undefined, Infinity);
resolveTtl(undefined, undefined, 1800);
Option normalizers
Coerce the polymorphic 2nd/3rd argument of set/remember into a uniform shape
— what BaseCacheDriver uses internally:
import { normalizeToOptions, normalizeToRememberOptions } from "@warlock.js/cache";
normalizeToOptions(60);
normalizeToOptions("1h");
normalizeToOptions({ tags: ["x"] });
normalizeToRememberOptions("1h");
Key + tag helpers
import { parseCacheKey, mergeTagSets, injectTags } from "@warlock.js/cache";
parseCacheKey("users:1");
parseCacheKey({ page: 1, q: "John" });
parseCacheKey("user:1", { globalPrefix: "app" });
mergeTagSets(["a", "b"], ["b", "c"]);
mergeTagSets(undefined, undefined);
injectTags({ ttl: "1h" }, ["unread"]);
Vector scoring
import { cosineSimilarity } from "@warlock.js/cache";
cosineSimilarity([1, 0, 0], [1, 0, 0]);
cosineSimilarity([1, 0, 0], [0, 1, 0]);
cosineSimilarity([1, 2, 3], [1, 2]);
Powers the brute-force cache.similar() on the memory drivers — reach for it
directly only when scoring vectors outside the cache.
TTL constants — CACHE_FOR
import { cache, CACHE_FOR } from "@warlock.js/cache";
await cache.set("report", data, CACHE_FOR.ONE_WEEK);
Members: HALF_HOUR, ONE_HOUR, HALF_DAY, ONE_DAY, ONE_WEEK,
HALF_MONTH, ONE_MONTH, TWO_MONTHS, SIX_MONTHS, ONE_YEAR (all seconds).
For most call sites the duration string ("1h", "7d") reads better.
See also