Giter VIP home page Giter VIP logo

Comments (5)

rossvz avatar rossvz commented on July 17, 2024 2

This was also the issue I was running into. I created a simplified version of the above code:

const initCache = () =>
  new Promise((resolve, reject) => {
    memoryCache = cacheManager.caching({
      store: fsStore,
      path: 'cache',
      ttl: 12 * 60 * 60,
      preventfill: false,
      reviveBuffers: false,
      fillcallback: data => {
        resolve()
      }
    })
  })

And I call that in my app like so

class MyClass {
   constructor(){
       initCache().then( //do some other stuff  ) 
   }
}

from node-cache-manager-fs.

Jakobud avatar Jakobud commented on July 17, 2024

I'm messing with your code a bit. The DiskStore.prototype.intializefill seems to be finding the .dat file and finding the values and it sets it in this.collection, but it still doesn't seem to work with the above code.

from node-cache-manager-fs.

gofreddo avatar gofreddo commented on July 17, 2024

The problem is a race condition. You are calling wrap before the cache is filled. You have to wait for the fillcallback before you can allow a call to wrap. The solution is to wrap cacheManager.caching in a promise that is resolved when the diskcache is filled. For example:

'use strict';
const rp = require('request-promise');
const cacheManager = require('cache-manager');
const fsStore = require('cache-manager-fs');

const _getFileNames = (opts) => {

return new Promise((resolve, reject) => {

const options = {
  uri: opts.uri,
  auth: opts.auth,
  json: true,
  timeout: 2 * 60 * 1000,
};

return rp(options)
  .then((reponse) => {
    return resolve(reponse);
  })
  .catch((error) => {
    return reject(error);
  });

});
};

const diskCachePromise = () => {
return new Promise((resolve) => {
const diskCache = cacheManager.caching(
{
store: fsStore,
options: {
ttl: 60 * 60 * 24 /* seconds */,
path: 'diskcache',
preventfill: false,
fillcallback: () => resolve(diskCache),
},
});
});
}

const diskCache = diskCachePromise();

const getFileNames = (opts) => {
return new Promise((resolve, reject) => {

diskCache.then((diskCache) => {
  diskCache.wrap('getFileNames', () => {
    return _getFileNames(opts);
  })
    .then((result) => {
      return resolve(result);
    })
    .catch((error) => {
      return reject(error);
    });

});

});
};

module.exports = {
getFileNames,
};

from node-cache-manager-fs.

websocket98765 avatar websocket98765 commented on July 17, 2024

@rossvz 's post is helpful. I assume that only happens once data size reaches a certain size to lose the race condition. The second part can be written as await initCache(); too within an async function.

Another thing to know is that diskCache does not support mget(). It will fail without an error message.

This was hard to diagnose because I set up a multiCache using a memoryCache (which worked) and a diskCache (which didn't). Switching my cache fetch from mget(), to a loop using get() allowed disk cache to work and was needed to get data pre-filling from disk working.

from node-cache-manager-fs.

Mar-Pfa avatar Mar-Pfa commented on July 17, 2024

the DiskCache is intended to run as a "singleton" - only one instance at one time and the api is running based on callbacks - no async, no promises... added that into the readme

from node-cache-manager-fs.

Related Issues (18)

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.