ForgeTotal.js 5
it

Total.js 5

Mettere in cache su disco risultati lenti con FILECACHE

Total.js 5 · total5@0.0.19 cacheperformancefiles Eseguita con Total.js 5

FILECACHE(id, expiration, callback, maker) cerca un file in cache per id. Se esiste ed è più recente di expiration (un testo come '1 minute'), il callback riceve il suo contenuto; altrimenti chiama maker(done) per calcolare il valore, lo memorizza e lo restituisce. Il risultato viene serializzato come JSON. A differenza di CACHE, sopravvive ai riavvii e funziona per risultati grandi.

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