The Filesystem store for the node-cache-manager module.
npm install cache-manager-fs --save- Saves anything that is
JSON.stringify-able to disk - limit maximum size on disk
- refill cache on startup (in case of application restart)
- "Callback"-interface style (no promises, no async)
Here are examples that demonstrate how to implement the Filesystem cache store.
// node cachemanagervarcacheManager=require('cache-manager');// storage for the cachemanagervarfsStore=require('cache-manager-fs');// initialize caching on diskconstdiskCache=cacheManager.caching({store: fsStore,options: {path: 'diskcache',//path for cached filesttl: 60*60,//time to life in secondsmaxsize: 1000*1000*1000,// max size in bytes on diskzip: true,//zip files to save diskspace (default: false) preventfill:true}});(async()=>{awaitdiskCache.set('key','value');console.log(awaitdiskCache.get('key'));//"value"console.log(awaitdiskCache.ttl('key'));//3600 secondsawaitdiskCache.del('key');console.log(awaitdiskCache.get('key'));//undefinedconsole.log(awaitgetUserCached(5));//{id: 5, name: '...'}console.log(awaitgetUserCached(5));//{id: 5, name: '...'}awaitdiskCache.reset();functiongetUserCached(userId){returndiskCache.wrap(userId/* cache key */,function(){returngetUser(userId);});}asyncfunctiongetUser(userId){return{id: userId,name: '...'};}})();options for store initialization
options.ttl=60;// time to life in secondsoptions.path='cache/';// path for cached filesoptions.preventfill=false;// prevent filling of the cache with the files from the cache-directoryoptions.fillcallback=null;// callback fired after the initial cache filling is completedoptions.zip=false;// if true the cached files will be zipped to save diskspaceoptions.reviveBuffers=true;// if true buffers are returned from cache as buffers, not objectsnpm install cache-manager-fs
To run tests:
npm test
To run Coverage:
npm run coverage
cache-manager-fs is licensed under the MIT license.