default

Cache primitives with Task semantics for composable error handling.

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();

Classes

Functions

f
createInMemory(): InMemoryConnector

Creates an in-memory cache connector for testing and development.

Interfaces

I
CacheAdapter

Cache adapter interface for cache-agnostic operations.

I
CacheConnector

Connector that produces connected CacheAdapter instances.

I
SetOptions

Options for set operations.

  • ttl: number

    Time-to-live in milliseconds. When omitted the entry does not expire.