@cache-utils/core
v0.0.2
Published
Adapter-based cache manager for TypeScript — wrap, get/set/del, distributed locks, and namespacing on top of a 4-method CacheAdapter interface.
Maintainers
Readme
@cache-utils/core
Adapter-based cache manager for TypeScript. CacheManager gives you
get/set/del/wrap, namespacing, and distributed locks on top of any
4-method CacheAdapter. Ships with a zero-dependency MemoryAdapter, so it
works standalone with no backing store required.
Install
npm install @cache-utils/coreUsage
import { CacheManager } from "@cache-utils/core";
const cache = new CacheManager(); // defaults to MemoryAdapter
await cache.set("user:1", { name: "Ada" }, 60); // ttl in seconds
const user = await cache.get<{ name: string }>("user:1");
// wrap: cache-aside in one call
const product = await cache.wrap(
"product:42",
() => fetchProductFromDb(42),
{ ttlSeconds: 300 },
);
// namespacing
const userCache = cache.namespace("user:");
await userCache.set("1", { name: "Ada" }); // stored as "user:1"
// distributed locks (backed by the adapter's setIfNotExists)
await cache.withLock("job:daily-report", 30, async () => {
await runDailyReport();
});Swap in a real backing store with @cache-utils/adapter-redis or
@cache-utils/adapter-memory, or implement CacheAdapter yourself.
License
MIT
