Cache the response of an action
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' }| 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.
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 });
}
});exports.install = function() {
ROUTE('GET /report/ --> *Report/read');
ROUTE('GET /runs/ --> *Report/runs');
};