---
question: Which cache tier do reads and writes use?
kind: overview
---

# Cache

The cache keeps the result of a call under a key, so the next call with the same key skips the work. It is the kernel service `services.cache`: modules use it for model calls, lookups and rendered pages. It is a stack of tiers: a fast `hot` tier and a durable `cold` tier.

Caching a function is Python only. Super admins can browse, add, edit and delete entries in Nexus, under System › Data › Cache.

<!-- matrix -->

## Backends

`services.cache.adapters` is a list. Each entry is one tier: an `adapter`, a `tier` name (`cold` when you leave it out) and its `config`.

| Adapter | Storage |
|---|---|
| `fs` | Files on local disk |
| `redis` | A Redis server |
| `object_storage` | The engine's object storage service |
| `keyvalue` | The engine's key-value store |
| `nats_rpc` | Another engine's cache, over NATS |

When `config.yaml` has no `cache:` block, as in a new project, the engine uses Redis at `redis://localhost:6379/0` as `hot` and object storage as `cold`. A tier it cannot reach counts as a miss and logs a warning. The keys of each adapter, and how to pick the tiers, are on [Choose a backend](/user-guide/build/platform/choose-a-cache-backend).

## Key concepts

- **Reads go hot, then cold.** `get` returns the first tier that has the key. A cold hit is not copied into hot.
- **Writes go cold.** `set_text`, `set_json`, `set_binary` and `set_pickle` write the cold tier only. `cache.hot.set_json(...)` writes hot. `delete` removes the key from every tier.
- **Typed entries.** Each entry is `TEXT`, `JSON`, `BINARY` or `PICKLE` (`DataType`, from `naas_abi_core.services.cache.CachePort`). `get` returns the value as its type; the decorator treats an entry of another type as a miss.
- **Age, not expiry.** An entry stores when it was written. A `ttl` is checked when you read: an older entry is a miss and gets computed again. Nothing deletes old entries.
- **Shared keys.** Keys are not prefixed with the module. Prefix yours (`my_module:...`).
- **Errors.** `get` raises `CacheNotFoundError` when no tier has the key.
- **Events.** Writes and deletes publish `CacheSet` and `CacheDeleted`, and a failed write `CacheError`, when the event service is up.
- **Dependency.** A module lists `CacheService` in `ModuleDependencies.services`. The attribute is `self.engine.services.cache`.
