Persist small settings with MEMORIZE
MEMORIZE(name) returns a plain object that is saved to databases/memorize_<name>.json and loaded again when the app starts. Change it with data.set(key, value), which also schedules the save. Saves are delayed (10 seconds by default) so several changes become one write; pass a delay in milliseconds as the second argument to change it. It is meant for small amounts of data such as counters, flags and settings.
MAIN.counter = MEMORIZE('counter', 100);exports.install = function() {
ROUTE('POST /hit/', hit);
ROUTE('GET /hits/', hits);
};
function hit($) {
var count = (MAIN.counter.hits || 0) + 1;
MAIN.counter.set('hits', count);
$.json({ hits: count });
}
function hits($) {
$.json({ hits: MAIN.counter.hits || 0 });
}