# KV and cache

URL: /code/platform/kv-cache · Id: `capability:kv-cache`

> KV and cache serve hot reads at the edge with explicit expiry, and every value rebuilds from the database.

KV and cache serve hot reads at the edge with explicit expiry. Every value is a copy rebuilt from [db.sb](/code/platform/db-sb), so a miss or a loss never loses data.

## Contract
- A cache entry is never a source of truth. db.sb holds the record, and the cache holds a copy with a required expiry.
- Every entry belongs to one namespace. A [Tenant](/business/nouns/tenant)'s entries stay in its partition.
- Expiry defaults to one hour, and a value holds at most 1 MB.
- Writing to the cache never writes the record. To change a record, call its [Verb](/business/nouns/verb).
- An entry tagged with a [Thing](/business/nouns/thing)'s `$id` expires the moment that Thing changes.
- Losing the whole cache costs latency, never records.
- No other key-value store holds a [Startup](/business/nouns/startup)'s records. Every durable value is a Thing in db.sb.

## Verbs
None.

## Interfaces
- [API](/code/api/reference)
- [SDK](/code/sdk/reference)
- [CLI](/code/cli/reference)
- [MCP](/code/mcp/reference)

## Example
```ts
import { $ } from 'startup-builder'

// A price-comparison site caches its laptop category page for ten minutes.
let page = await $.cache.get<string>('category:laptops')
if (!page) {
  const laptops = await $.Collection.query({ query: 'collection_laptops.products' })
  page = JSON.stringify(laptops.rows)
  await $.cache.set('category:laptops', page, { ttl: '10m', tags: ['category_laptops'] })
}
```