Giter VIP home page Giter VIP logo

node-cache-manager-fs's Introduction

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 cachemanager
var cacheManager = require('cache-manager');
// storage for the cachemanager
var fsStore = require('cache-manager-fs');
// initialize caching on disk
const diskCache = cacheManager.caching({
    store: fsStore, 
    options: {
        path: 'diskcache',          //path for cached files
        ttl: 60 * 60,               //time to life in seconds
        maxsize: 1000*1000*1000,    // max size in bytes on disk
        zip: true,                  //zip files to save diskspace (default: false)        
        preventfill:true            
    }
});

(async () => {

    await diskCache.set('key', 'value');
    console.log(await diskCache.get('key')); //"value"
    console.log(await diskCache.ttl('key')); //3600 seconds
    await diskCache.del('key');
    console.log(await diskCache.get('key')); //undefined

    console.log(await getUserCached(5)); //{id: 5, name: '...'}
    console.log(await getUserCached(5)); //{id: 5, name: '...'}

    await diskCache.reset();

    function getUserCached(userId) {
        return diskCache.wrap(userId /* cache key */, function () {
            return getUser(userId);
        });
    }

    async function getUser(userId) {
        return {id: userId, name: '...'};
    }

})();

Options

options for store initialization

options.ttl = 60; // time to life in seconds
options.path = 'cache/'; // path for cached files
options.preventfill = false; // prevent filling of the cache with the files from the cache-directory
options.fillcallback = null; // callback fired after the initial cache filling is completed
options.zip = false; // if true the cached files will be zipped to save diskspace
options.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.

node-cache-manager-fs's People

Contributors

dependabot[bot] avatar fctb12 avatar luckyllama avatar mar-pfa avatar mzagel avatar sframe avatar sheershoff avatar wcoppens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-cache-manager-fs's Issues

diskCache.set is not a function when using the example on readme..

Code from readme gives diskCache.set is not a funciton... is this library still valid ?

// node cachemanager
var cacheManager = require('cache-manager');
// storage for the cachemanager
var fsStore = require('cache-manager-fs');
// initialize caching on disk
const diskCache = cacheManager.caching({
    store: fsStore,
    options: {
        path: 'diskcache',          //path for cached files
        ttl: 60 * 60,               //time to life in seconds
        maxsize: 1000*1000*1000,    // max size in bytes on disk
        zip: true,                  //zip files to save diskspace (default: false)        
        preventfill:true
    }
});

(async () => {

    await diskCache.set('key', 'value');
    console.log(await diskCache.get('key')); //"value"
    console.log(await diskCache.ttl('key')); //3600 seconds
    await diskCache.del('key');
    console.log(await diskCache.get('key')); //undefined

    console.log(await getUserCached(5)); //{id: 5, name: '...'}
    console.log(await getUserCached(5)); //{id: 5, name: '...'}

    await diskCache.reset();

    function getUserCached(userId) {
        return diskCache.wrap(userId /* cache key */, function () {
            return getUser(userId);
        });
    }

    async function getUser(userId) {
        return {id: userId, name: '...'};
    }

})();

get method returning undefined

var cacheManager = require('cache-manager');
var fsStore = require('cache-manager-fs');

var diskCache = cacheManager.caching({ store: fsStore, options: {   ttl:86400, maxsize: 1000000} });
diskCache.set('test',"1");
console.log(diskCache.get('test')) ==>undefined;

cache key as filename

Hi, I was wondering if there was any particular reason to use the UUID method for generating the .dat files? I noticed an issue when I run in cluster mode, each cluster process seems to generate it's own file in the cache directory so there are duplicates.

I was going to create a pull request for an optional flag to use the cache key as the filename. Is there something obviously bad I'm missing about this idea? Very long cache keys would have a problem or if the keys contained special characters and such.

Thanks for creating this!

Module doesn't delete if ttl expired

Hi All,

node-cache-manager-fs doesn't delete the cache files if ttl has expired. Is there any way to remove/delete those files using this node-cache-manager-fs node module(method).

Support of larger file size

Is there a limit of how big the size of the cache can be?

We are trying to cache a file of ~100MB, it crashed. Around 90MB would be OK.

when caching a Buffer, the cache returns json

I noticed that when I cache an image contained in a buffer, when I hit the cache, I'm returned a json that looks like:

{type: "Buffer", data: .... }

Should this return the image buffer?

How to clear file cache when ttl has expired

Hi All,

I am using multi cache

cacheManager.multiCaching([memoryCache, diskCache], { isCacheableValue: isCacheableValue });

i have to delete the physical files if ttl has expired. Please advise anyone how to process next?

Library throws an exception when trying to recover cached values which cache files has been deleted

I had some values cached. I wanted to "invalidate" this cached values before they've reached the end of their TTL values, so I went and deleted the content of the "cache" directory.

The result is that, when trying to get the values, an exception is being thrown indicating that the ".dat" file can't be loaded.

errno ENOENT syscall open path diskcache/cache_1f24a906-5b4c-4357-90e4-2dba0aaa7f1f.dat

Shouldn't the library considered that there's no value cached and attempt to retrieve a new one?
I've used other filesystem cache libraries and that is the standard behavior.

Delete operation does not support options arg

The delete operation does not support options arg, which results in incompatibility with several caching managers such as node-cache-manager. To fix this, the first few lines of the DiskStore delete operation need to be rewritten to the following.

DiskStore.prototype.del = function (key, options, cb) {

  if (typeof options === 'function') {
        cb = options;
        options = null;
  }
  cb = typeof cb === 'function' ? cb : noop;

Data is not cached

I am using this with express session 'express-session-cache-manager' . My current code is:
`
var cacheManager = require('cache-manager')
var CacheManagerStore = require('express-session-cache-manager').default

var fsStore = require('cache-manager-fs');

var diskCache = cacheManager.caching({
store: fsStore,
options:
{ttl: 606024/* seconds /,
maxsize: 1000
10001000 / max size in bytes on disk */,
path:'diskcache',
preventfill:false,
}});

const sessionMiddleware = session({
store: new CacheManagerStore(diskCache),
secret: APP_SECRET,
resave: true,
saveUninitialized: true,
cookie: { maxAge: 2419200000}
})

app.use(sessionMiddleware)
`
This creates multiple cache files on each call.
What am i missing here?

ENOENT: no such file or directory on the odd occasion when cache retrieves up around 50 per second

The following error was observed when diskCache.get() was called at a frequency of around 50 per second. At lower rates like 5 to 10 per second we never observed the error.

[Error: ENOENT: no such file or directory, open '/tmp/diskcache/cache_99e4ffff-6392-45b1-ba69-21786819c784.dat'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/tmp/diskcache/cache_99e4ffff-6392-45b1-ba69-21786819c784.dat'
}

Following cache-manager code used in an AWS Lambda.

"use strict";

const cacheManager = require("cache-manager");
const fsStore = require("cache-manager-fs");

const diskCache = cacheManager.caching({
   store: fsStore,
   options: {
      ttl: 300,
      maxsize: 10485760,
      path: "/tmp/diskcache",
      preventfill: true
   }
});

The following code was being used when the error above was encountered.

   diskCache.get("SOME KEY FOR CACHE", function (err, result) {
      if (err) {
         console.error({ error: err, result: result });
      } else if (result) {
         // do something based on result from cache
      } else {
         var args = {
            "Bucket": "SOME AWS S3 BUCKET",
            "Key": "SOME AWS S3 KEY"
         };

         s3.getObject(args, function (err, data) {
            if (err) {
               console.error({ error: err, data: data });
            } else {
               var json = data.Body.toString("ascii");
               diskCache.set("SOME KEY FOR CACHE", json);
               // do something based on result from cache
            }
         });
      }
   });

Changing to the following will address and work-around the odd occurrence of the error, however we figured we'd raise this incase we're not using cache-manager correctly.

   diskCache.get("SOME KEY FOR CACHE", function (err, result) {
      if (result) {
         // do something based on result from cache
      } else {
         if (err) {
            console.error({ error: err, result: result });
         }
         var args = {
            "Bucket": "SOME AWS S3 BUCKET",
            "Key": "SOME AWS S3 KEY"
         };

         s3.getObject(args, function (err, data) {
            if (err) {
               console.error({ error: err, data: data });
            } else {
               var json = data.Body.toString("ascii");
               diskCache.set("SOME KEY FOR CACHE", json);
               // do something based on result from cache
            }
         });
      }
   });

We cannot use client side rendering

Hi,

When we use this module in client side rendering. we are getting the below issue,

"Module not found: Error: Can't resolve 'fs'"

Is there any idea to resolve this issue?

Does not refill cache on startup

No matter if I have preventfill true or false, every time I restart my script, the library does not fetch values from the existing cache on the filesystem. Here is some example code:

var cacheManager = require('cache-manager');
var fsStore = require('cache-manager-fs');
var cache = cacheManager.caching({
    store: fsStore, options: {
      ttl: 60*60,
      maxsize: 1000*1000*1000,
      fillcallback: function(){
        console.log('done filling from cache');
      }
    }
  });

var ttl = 30;

function getUser(id, cb) {
    console.log("Returning user from slow database.");
    setTimeout(function () {
        cb(null, {id: id, name: 'Bob'});
    }, 5000);
}

var userId = 123;
var key = 'user_' + userId;

cache.wrap(key, function (cb) {
    getUser(userId, cb);
}, {ttl: ttl}, function (err, user) {
    console.log(user);

    cache.wrap(key, function (cb) {
        getUser(userId, cb);
    }, function (err, user) {
        console.log(user);
    });
});

Every time I run my script.js, the getUser() method is called (which takes 5 seconds). It never seems to repopulate the cache on startup.

Am I missing something really obvious here?

Location of cache files?

// node cachemanager
var cacheManager = require('cache-manager');
// storage for the cachemanager
var fsStore = require('cache-manager-fs');
// initialize caching on disk
var diskCache = cacheManager.caching({store: fsStore, options: {path:'diskcache', preventfill:true}});

Everything works fine, but I don't know the location of cache files.

I search 'diskcache' directory but nothing.

find / -type d -name 'diskcache'

Can you help me?

Missing parameter in get method

Hi,

I am using your llibrary and got some problems to make it work in my app. I noticed that the get method in your code has only 2 parameters, while otherwise it has 3 (see cache-manager-redis).

Alse when invoked in cache-manager wrap function 3 parameters are used

 self.store.get(key, options, function(err, result) { ... }

When options is null then cb is set to noop:

DiskStore.prototype.get = function (key, cb) {
cb = typeof cb === 'function' ? cb : noop;
    ...
}

Should I fix it and create a pull request or can you fix it yourself?

Regards,
Karel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.