# Cache the response of an action

> Set cache on NEWACTION to keep the response for the same query, params, input, user or language, so the action does not run again while the entry lives.

Framework: total5
Product: Total.js 5 (total5@0.0.20)
Language: en
Content-SHA256: cb7cf42e82973a27aa5fd75e4cb04dc6c04f7361f7229ecd3ffea1e31d806ad6
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

With the `cache` option, `0.0.20` keeps the **response** of an action and answers the next identical call from memory: the middleware and the action do not run. Declare which parts of the request make a call different, and the entry is shared by every call that has the same values.

```js
cache: { query: 'page,sort', params: true, input: false, user: 'id', language: true, expire: '10 minutes' }
```

| Key | Meaning |
|---|---|
| `query`, `params`, `input` | `'a,b'` (these fields), `true` (every field declared in the schema of that part) or nothing (ignored) |
| `user` | `'id'` or `'id,role'` (these fields of the user) or `true` (the `id`) |
| `language` | `true` adds the language of the request |
| `expire` | How long the entry lives, `'5 minutes'` by default |
| `id` or `key` | A prefix, so that you can find the entries |

`cache: true` is valid too: the response is then shared by every call. A response is stored only when the action succeeds. The entries live in `CACHE`, so they are per process: with a cluster, each worker has its own.

```js definitions/report.js
MAIN.runs = 0;

NEWACTION('Report/read', {
	name: 'Read a report',
	query: 'page:Number',
	cache: { query: 'page', expire: '1 minute' },
	action: function($) {
		MAIN.runs++;
		$.callback({ page: $.query.page || 1, runs: MAIN.runs });
	}
});

NEWACTION('Report/runs', {
	name: 'How many times the report ran',
	action: function($) {
		$.callback({ runs: MAIN.runs });
	}
});
```

```js controllers/report.js
exports.install = function() {
	ROUTE('GET /report/ --> *Report/read');
	ROUTE('GET /runs/ --> *Report/runs');
};
```
