var NC = require( "node-cache" );
// deactivated useClones to use the toString function ;-)
var cache = new NC( { useClones: false } );
loadDataFromDB = function( cb ){
// wait for the DB to return the data.
// this simulates the time to call your db through the network
setTimeout( function(){
// generate fake data
var toCache = {};
toCache.id = 42;
toCache.name = "foo";
toCache.toString = function(){
return "id: " + this.id + " name: " + this.name
}
// save to the cache
cache.set( "id"+toCache.id, toCache )
// check if it exists
console.log( "AFTER SET CACHE: " + cache.get( "id42" ) )
cb( null, toCache )
}, 1000 );
}
// load data from fake db
loadDataFromDB( function( err, data ){
// check cache after data was loaded
console.log( "AFTER DATA LOADED: " + cache.get( "id42" ) )
})
// immediate cache check
console.log( "ON CODE BOTTOM: " + cache.get( "id42" ) )