# Automatic Caching

> Turn caching on for a table, a database, a custom API or a system API and API Maker answers the reads from Redis, drops the cached answers on every write through it, and lets you reset them from code or per request.

Source: https://docs.apimaker.dev/v1/docs/features/automatic-caching.html

Turn caching on and the reads of a table are answered from Redis, for every server of the cluster. API Maker knows when the table changes, because the writes go through it too, so it drops the cached answers itself : no stale reads, no code.

> Diagram : Automatic caching : reads served from Redis, writes drop the cached answers
>
> Automatic caching : a read of a table with caching on is answered from Redis when the same request was answered before ; otherwise the database answers and the answer is stored. Any write to the table through API Maker drops its cached answers. A custom API with caching does the same, and resetCacheOnModificationOf ties it to the tables it reads.

| | |
|---|---|
| Turn on | `enableCaching: true` in the [collection settings](https://docs.apimaker.dev/v1/docs/settings/collectionSettings.html) or the [database settings](https://docs.apimaker.dev/v1/docs/settings/databaseSettings.html) ; in the settings of a [custom API](https://docs.apimaker.dev/v1/docs/settings/customApiSettings.html) or a [system API](https://docs.apimaker.dev/v1/docs/settings/systemApiSettings.html). |
| Key | The URL, the query, the body and the tokens of the request : two callers with different rights never share an answer, tenants never share one. |
| Lifetime | Until a write of the table, or `redisValueExpireInSeconds` (7200 by default) of the [configuration](https://docs.apimaker.dev/v1/docs/am-resources/api-maker-configurations.html). Answers longer than `maxCharsResToCache` are not cached. |
| Reset | A write through any API of the table ; the [reset cache system APIs](https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-reset-redis-cache-db-api.html) ; the header `x-am-cache-control: reset_cache` on one request. |
| See it | The [Redis dashboard](https://docs.apimaker.dev/v1/docs/dashboard/redis-dashboard.html) lists the keys ; the API testing page says whether an answer came from the cache. |

## Tables

- Set it on the table, or on the database for all its tables. The setting is not per API : every read of the table is cached, every write of the table resets.
- Data changed outside API Maker is not seen : reset the table with [reset database cache](https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-reset-redis-cache-db-api.html) from the job which changed it, or send `x-am-cache-control: reset_cache` with the next read.
- Cached answers run no hook : a pre hook which scopes rows to the caller still holds, because the caller is part of the key.

## Custom APIs

**Basic Info**

```typescript
enableCaching: true,
resetCacheOnModificationOf: [ 'DB:mongodb:shop:products', 'DB:mongodb:shop:categories' ],
```

- The answer of the API is cached per body, query and caller. `resetCacheOnModificationOf` names the tables (`DB:instance:database:table`), custom APIs (`CA:name`) and bundle versions (`TP:bundle:version`) whose writes drop it.
- Without the list, reset it yourself with [reset custom API cache](https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-reset-redis-cache-custom-api.html).

## System APIs

- `enableCaching: true` in the settings of the system API ; reset with [reset system API cache](https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-reset-redis-cache-system-api.html).

## Per request

| Header | Effect |
|---|---|
| `x-am-cache-control: no_action` | Answer without reading or writing the cache. |
| `x-am-cache-control: reset_cache` | Drop the cached answers of the API, then answer from the database and cache that. |

## Related

- [Request headers](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html#x-am-cache-control) · [Redis dashboard](https://docs.apimaker.dev/v1/docs/dashboard/redis-dashboard.html) · [Multi-tenant caching](https://docs.apimaker.dev/v1/docs/features/multi-tenant.html#caching)
