ForgeTotal.js 5
en

Total.js 5

Cache slow results on disk with FILECACHE

Total.js 5 · total5@0.0.19 cacheperformancefiles Executed against Total.js 5

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.

controllers/report.js
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 });
	});
}