Skip to content

InMemoryCache

InMemoryCache is a Cache implementation backed by an in-memory SQLite database (via Node.js built-in node:sqlite). Data does not survive process restarts.

import { InMemoryCache } from "@farbenmeer/tag-based-cache/in-memory-cache";
new InMemoryCache()

No arguments required. Creates an in-memory SQLite database and starts automatic garbage collection for expired entries.

const cache = new InMemoryCache();
await cache.set({
key: "book:1",
data: { title: "Dune" },
ttl: 300,
tags: ["books", "book:1"],
});
const entry = await cache.get("book:1");
// { data: { title: "Dune" }, attachment: null }
// Invalidate by tag
await cache.delete(["book:1"]);

Expired entries are cleaned up automatically on a periodic timer. The interval adapts based on how many expired entries are found:

  • Starts at 5 seconds.
  • Decreases (down to 5s minimum) when many expired entries are found.
  • Increases (up to 5 minutes maximum) when few expired entries are found.
  • Development and testing.
  • Single-process production deployments where persistence is not needed.
  • Scenarios where you want the fastest possible reads/writes with no I/O overhead.