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

Can I access the cache across different script? #312

Open
tungrix opened this issue Aug 7, 2023 · 3 comments
Open

Can I access the cache across different script? #312

tungrix opened this issue Aug 7, 2023 · 3 comments

Comments

@tungrix
Copy link

tungrix commented Aug 7, 2023

Let's say I am declaring an obj in test1.js.

const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } );

obj = { my: "Special", variable: 42 };
 
success = myCache.set( "myKey", obj, 10000 );

Is it possible to access "myKey" in test2.js during running?

If yes, how can I do it?

If not, how to access "myKey" in some child nested functions? Through multiple passing as the parameter?

@doc-han
Copy link

doc-han commented Aug 7, 2023

You can create a new file called cache.js somewhere that contains your node-cache initialization and then returns the cache instance. You can then use the returned instance in any other file as it serves as a singleton.

// Filename: cache.js
const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } );
module.exports = {
	myCache
}
// Filename: test1.js
const { myCache } = require("./cache.js");

const obj = { my: "Special", variable: 42 };
 
success = myCache.set( "myKey", obj, 10000 );
// Filename: test2.js
const { myCache } = require("./cache.js");
success = myCache.set("anotheKey", "something here", 10000 );

With this the same instance of cache is shared around.

@tungrix
Copy link
Author

tungrix commented Aug 8, 2023

Thank you so much.

If I want to apply something like refreshing the token every 9 hours, is it possible for me to do it like this inside cache.js, or is there any better way to do it?

const cache = new NodeCache();
cache.set("token", await getToken(), 9*60*60);

cache.on("del", async function (key, value) {
  cache.set("token", await getToken(), 9*60*60);
  });

Is stdTTL means if I didn't set the ttl inside cache.set(), the stdTTL will be the default one for every cache?

@AggelosAst
Copy link

AggelosAst commented Aug 28, 2023

Yes, that is possible. Inside the file that you require it in, you can still bind events. Although, to answer your question, it is generally better to either place them in the file where you mainly use nodecache in, or if you would like, for better code organization, make a function like

const cache = new NodeCache();
// . . . 
function registerEvents() {
	cache.cache.on("del", (key, value) => {
             cache.cache.set(key, "...");
    });
}

And to answer the second question, stdTTL in the constructor option, applies to every cache.set() BUTm if you have the stdTLL in the constructor aswell as in a cache.set(), its going to use that one but if you dont use the stdTTL constructor option and use it in the cache.set(), it will utilize that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants