cache

Remember slow results on the server and clear them by tag when the data changes.

BackendServer

What it is

@kitr/cache is a server-side cache: get, set, remember, cached and invalidateTags. remember() computes a value once and stores it; invalidateTags(['orders']) clears everything about orders.

Runs on the server only. For caching in the browser, query already does it.

Install

kitr add cache

First time? Run npm i -g @kitr/kitr once. Inside a project, the global kitr runs your project's own version.

Adds @kitr/cache, its provider and config/cache.ts with the memory driver. Needs server. Every change.

Quick start

On the server, wrap a slow read in remember(). This controller builds a project report once every ten minutes:

// server/controllers/reports_controller.ts
import type { HttpContext } from '@kitr/api'
import { useCache } from '@kitr/cache'
import { buildProjectReport } from '#server/reports/project_report'

export default class ReportsController {
  async show({ app, params }: HttpContext<{ id: string }>) {
    return useCache(app).remember(
      `report:project:${params.id}`,
      () => buildProjectReport(params.id),
      { ttl: 600, tags: ['reports', `project:${params.id}`] },
    )
  }
}

The first request builds the report and stores it for ten minutes. Every request in that window gets the copy, and concurrent requests build it once. Next in the guide: Cache your first slow read.

Built on

Storage goes through a six-method driver:

Driver Built on Use it for
Memory A Map in the server process Development, tests and single-process servers.
unstorage unstorage Redis, Upstash, Cloudflare KV, Vercel KV and every other unstorage driver.
Your own The CacheDriver interface Anything with get, set, delete and keys.

kitr adds expiry, tags, the key prefix, remember() with request coalescing, serialization for Date, Map and Set, and a devtools panel.

Problems it solves

Further reading