varcacheManager=require("cache-manager");varmemoryCache=cacheManager.caching({store:"memory",max:100,ttl:10/*seconds*/});varttl=5;// Note: callback is optional in set() and del().// Note: memory cache clones values before setting them unless `shouldCloneBeforeSet` is set to falsememoryCache.set("foo","bar",{ttl:ttl},function(err){if(err){throwerr;}memoryCache.get("foo",function(err,result){console.log(result);// >> 'bar'memoryCache.del("foo",function(err){});});});functiongetUser(id,cb){setTimeout(function(){console.log("Returning user from slow database.");cb(null,{id:id,name:"Bob"});},100);}varuserId=123;varkey="user_"+userId;// Note: ttl is optional in wrap()memoryCache.wrap(key,function(cb){getUser(userId,cb);},{ttl:ttl},function(err,user){console.log(user);// Second time fetches user from memoryCachememoryCache.wrap(key,function(cb){getUser(userId,cb);},function(err,user){console.log(user);});});// Outputs:// Returning user from slow database.// { id: 123, name: 'Bob' }// { id: 123, name: 'Bob' }
memoryCache.mset("foo","bar","foo2","bar2",{ttl:ttl},function(err){if(err){throwerr;}memoryCache.mget("foo","foo2",function(err,result){console.log(result);// >> ['bar', 'bar2']// Delete keys with del() passing arguments...memoryCache.del("foo","foo2",function(err){});// ...passing an Array of keysmemoryCache.del(["foo","foo2"],function(err){});});});
varmultiCache=cacheManager.multiCaching([memoryCache,someOtherCache]);userId2=456;key2="user_"+userId;ttl=5;// Sets in all caches.// The "ttl" option can also be a function (see example below)multiCache.set("foo2","bar2",{ttl:ttl},function(err){if(err){throwerr;}// Fetches from highest priority cache that has the key.multiCache.get("foo2",function(err,result){console.log(result);// >> 'bar2'// Delete from all cachesmultiCache.del("foo2");});});// Set the ttl value by context depending on the store.functiongetTTL(data,store){if(store==="redis"){return6000;}return3000;}// Sets multiple keys in all caches.// You can pass as many key,value pair as you wantmultiCache.mset("key","value","key2","value2",{ttl:getTTL},function(err){if(err){throwerr;}// mget() fetches from highest priority cache.// If the first cache does not return all the keys,// the next cache is fetched with the keys that were not found.// This is done recursively until either:// - all have been found// - all caches has been fetchedmultiCache.mget("key","key2",function(err,result){console.log(result[0]);console.log(result[1]);// >> 'bar2'// >> 'bar3'// Delete from all cachesmultiCache.del("key","key2");// ...or with an ArraymultiCache.del(["key","key2"]);});});// Note: options with ttl are optional in wrap()multiCache.wrap(key2,function(cb){getUser(userId2,cb);},{ttl:ttl},function(err,user){console.log(user);// Second time fetches user from memoryCache, since it's highest priority.// If the data expires in the memory cache, the next fetch would pull it from// the 'someOtherCache', and set the data in memory again.multiCache.wrap(key2,function(cb){getUser(userId2,cb);},function(err,user){console.log(user);});});// Multiple keysmultiCache.wrap("key1","key2",function(cb){getManyUser(["key1","key2"],cb);},{ttl:ttl},function(err,users){console.log(users[0]);console.log(users[1]);});