Giter VIP home page Giter VIP logo

Comments (8)

erdii avatar erdii commented on May 21, 2024 19

@cjaoude you write one singleton module that exports a cache instance and import that one instance everywhere you want to use it.

Example:

./src/cache.js:

const NodeCache = require("nodecache");

module.exports = new NodeCache(/* your nodecache settings here */);

./src/a.js

const cacheSingleton = require("./cache.js");

// cacheSingleton is the same cache instance everywhere inside your app
// as long it runs inside the same node.js process

./src/b.js

const cacheSingleton = require("./cache.js");

// cacheSingleton is the same cache instance everywhere inside your app
// as long it runs inside the same node.js process

This is not specific to NodeCache but more how the node.js module system works. Read more at: https://nodejs.org/dist/latest-v10.x/docs/api/modules.html

from node-cache.

cjaoude avatar cjaoude commented on May 21, 2024 1

How to access the same cache in a different module? Seems pointless to have a cache just for one file. (same app) Tnx

from node-cache.

mpneuried avatar mpneuried commented on May 21, 2024

Thats not possible.
The usecase of node-cache is a cache within one running node process.
You can compare it to a advanced JavaScript object.

It's a planned feature to define a storage provider like process memory, a file or a external server solution.

But for now I recommend using a memcached or redis server that's saving your data.

from node-cache.

CalebEverett avatar CalebEverett commented on May 21, 2024

Would there be any security or performance issues with putting a cache instance on app.locals in an express app?

from node-cache.

mpneuried avatar mpneuried commented on May 21, 2024

@CalebEverett node-cache for itself has no security built in.
Putting the node-cache to the app.locals of express is a possible solution.
I don't know any security issues of app.locals, but i recommend to asking this question directly to express.

from node-cache.

ffflabs avatar ffflabs commented on May 21, 2024

What happens if you run nodecache in an app running in cluster mode, say, like in pm2? Does this mean that each thread will have its own cache?

from node-cache.

mpneuried avatar mpneuried commented on May 21, 2024

@amenadiel Cluster mode or event pm2 cluster ist currently not development target and is not tested yet. Fell free to check it and tell me the results or send me a PR ;-)

from node-cache.

cjaoude avatar cjaoude commented on May 21, 2024

I see, thanks, that makes sense. @erdii

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.