Giter Site home page Giter Site logo

Saving the cache about node-cache HOT 9 CLOSED

node-cache avatar node-cache commented on May 20, 2024
Saving the cache

from node-cache.

Comments (9)

erdii avatar erdii commented on May 20, 2024 1

@AnandChowdhary Fraud looks like a nice project! Glad to see that you built something useful for you business on nodecache!

from node-cache.

smrchy avatar smrchy commented on May 20, 2024

Nice idea for a new project that implements nodecache and does just this.

from node-cache.

mpneuried avatar mpneuried commented on May 20, 2024

I'm developing a solution for this requirement during my time in the train.

Have a look: https://github.com/mpneuried/node-cache-persist
It's not published yet, because the documentation is missing and the api is not fixed yet.

from node-cache.

cinder92 avatar cinder92 commented on May 20, 2024

and where is saving cache this project now?

from node-cache.

supukarmin avatar supukarmin commented on May 20, 2024

Had the same problem, just wrote some short functions; no need for a library, the combination of .keys and .mget creates the magic:

const NodeCache = require('node-cache');
const fs = require('fs');
const path = require('path');

const cache = new NodeCache();

const saveCache = (cache, directoryPath = __dirname) => {
    cache.keys((err, keys) => {
      if (!err) {
        cache.mget(keys, (err, cacheData) => {
          if (!err) {
            const stringifiedData = JSON.stringify(cacheData);
            fs.writeFileSync(path.join(directoryPath, 'cache.json'), stringifiedData)
          } else {
            console.error(err);
          }
        });
      } else {
        console.error(err);
      }
    });
}

const loadCache = (cache, directoryPath = __dirname) => {
  const filePath = path.join(directoryPath, 'cache.json');
  if (fs.existsSync(filePath)) {
    const jsonData = fs.readFileSync(filePath, 'utf8');
    const data = JSON.parse(jsonData);
    for (const [key, val] of Object.entries(data)) {
      if (data.hasOwnProperty(key)) {
        cache.set(key, val);
      }
    }
  }
};

loadCache(cache);

//and then put it into an interval or save it after every change or whatever suits your needs

setInterval(() => {
  saveCache(cache);
}, 60*1000);

from node-cache.

erdii avatar erdii commented on May 20, 2024

@supukarmin: fs.writeFileSync(path.join(directoryPath, 'cache.json'), stringifiedData)

This will block your proccess while saving. I think you are better off with fs.writeFile

Actually all fs.*Sync functions you use, have counterparts without the Sync-suffix, that will not block your process and use the callblack-pattern for the flow of your node-app.

from node-cache.

erdii avatar erdii commented on May 20, 2024

@supukarmin It DOES ALWAYS block your process.
Read more about it here: https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/

Edit: small example to demonstrate:

function blockEverything() {
	console.log("now the process will be blocked forever");
	while(true) {}
}

setInterval(() => {
	console.log("not-blocked");
}, 500)

setTimeout(blockEverything, 2500);

Edit: it seems that @supukarmin deleted a comment here... 😞

from node-cache.

AnandChowdhary avatar AnandChowdhary commented on May 20, 2024

I made a library which uses a file system-based key-JSON storage, based on nodecache. It's kind of the opposite of "save the cache", since the primary source is the file system, but every key which is fetched is then saved in the cache. If you're still interested in having a file system store for your cache, check it out: https://github.com/AnandChowdhary/fraud/.

from node-cache.

erdii avatar erdii commented on May 20, 2024

Closed because the filesystem is out of scope (at least for now)

from node-cache.

Related Issues (20)

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.