Cache slow results on disk with FILECACHE
FILECACHE(id, expiration, callback, maker) looks for a cached file for id. If it exists and is younger than expiration (text such as '1 minute'), the callback receives its content; otherwise it calls maker(done) to compute the value, stores it and returns it. The result is serialized as JSON. Unlike CACHE, it survives restarts and works for large results.
exports.install = function() {
ROUTE('GET /report/', report);
};
var computed = 0;
function report($) {
FILECACHE('monthly-report', '1 minute', function(err, response) {
$.json({ report: response, computed: computed });
}, function(done) {
// The expensive work goes here
computed++;
done(null, { total: 42, generated: computed });
});
}