Total.js ForgeTotal.js 5
English

Total.js 5

Cache the response of an action

Total.js 5 · total5@0.0.20 actionscacheperformance

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.

cache: { query: 'page,sort', params: true, input: false, user: 'id', language: true, expire: '10 minutes' }
KeyMeaning
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)
languagetrue adds the language of the request
expireHow long the entry lives, '5 minutes' by default
id or keyA 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.

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 });
	}
});
controllers/report.js
exports.install = function() {
	ROUTE('GET /report/ --> *Report/read');
	ROUTE('GET /runs/ --> *Report/runs');
};