Cache primitives with Task semantics for composable error handling.

The core abstraction is Cache, which wraps a CacheAdapter with typed errors and Task-based operations. Use createInMemory for testing or implement CacheAdapter for your cache backend.

Examples

Basic usage

import { Cache, createInMemory } from "@anabranch/cache";

const cache = await Cache.connect(createInMemory()).run();

await cache.set("user:1", { name: "Alice" }, { ttl: 60_000 }).run();
const user = await cache.get<{ name: string }>("user:1").run();

Cache-aside pattern

const user = await cache.getOrSet<User>(
  `user:${id}`,
  () => db.query("SELECT * FROM users WHERE id = ?", [id]),
  { ttl: 60_000 },
).run();