Skip to content

Repository files navigation

node-cache-manager-fs

Node Cache Manager store for Filesystem

The Filesystem store for the node-cache-manager module.

Installation

npm install cache-manager-fs --save

Features

  • 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)

Usage example

Here are examples that demonstrate how to implement the Filesystem cache store.

Single 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

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 objects

Installation

npm install cache-manager-fs

Tests

To run tests:

npm test

Code Coverage

To run Coverage:

npm run coverage

License

cache-manager-fs is licensed under the MIT license.

About

Node Cache Manager store for filesystem

Resources

Stars

31 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages