ForgeTotal.js 5
nl

Total.js 5

Trage resultaten op schijf cachen met FILECACHE

Total.js 5 · total5@0.0.19 cacheperformancefiles Uitgevoerd met Total.js 5

FILECACHE(id, expiration, callback, maker) zoekt een cachebestand voor id. Als het bestaat en jonger is dan expiration (een tekst als '1 minute'), krijgt de callback de inhoud; anders roept het maker(done) aan om de waarde te berekenen, slaat die op en geeft haar terug. Het resultaat wordt als JSON geserialiseerd. Anders dan CACHE overleeft het herstarts en werkt het voor grote resultaten.

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 });
	});
}