Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

share the same cache between 2 nodejs script #61

Closed
kenpeter opened this issue May 21, 2016 · 8 comments
Closed

share the same cache between 2 nodejs script #61

kenpeter opened this issue May 21, 2016 · 8 comments

Comments

@kenpeter
Copy link

I set a cache key and store a value in one nodejs script. How do I use the same key and access the value in another nodejs script? Right now, it will say key_not_found in another script.

@mpneuried
Copy link
Contributor

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.

@CalebEverett
Copy link

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

@mpneuried
Copy link
Contributor

@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.

@ffflabs
Copy link

ffflabs commented Feb 3, 2017

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?

@mpneuried
Copy link
Contributor

mpneuried commented Feb 6, 2017

@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 ;-)

@cjaoude
Copy link

cjaoude commented Aug 23, 2018

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

@erdii
Copy link
Member

erdii commented Aug 25, 2018

@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

@cjaoude
Copy link

cjaoude commented Aug 25, 2018

I see, thanks, that makes sense. @erdii

@erdii erdii closed this as completed Aug 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants