Reputation: 495
I am trying to use memory-cache npm module for creating an in-memory cache in my project. Here is
var secureCache = require('memory-cache');
var get = function (key) {
if(!_.isEmpty(secureCache)) {
return secureCache.get(key);
} return null;
}
var set = function (key, value, callback) {
secureCache.put(key, value, (30*24*60*60*1000), function (key, value) {
callback(key, value);
})
}
module.exports = {
get : get,
set : set
}
Now whenever I use set to set a key value in cache by requiring this file, it gets set, but when I try to retrieve it in some other file using get, it returns empty. Looks like the variable secureCache gets re-initialised. However, when I do the same with
var secureCache = {};
and directly set and access the key values from object, I am able to do it, and the object does not get reinitialised.
My question is what difference does require make here. And how do I make it work ?
Upvotes: 0
Views: 1794
Reputation: 3080
Note that this memory-cache
only works under the same node process, as multiple node processes don't share the same memory (thanksfully).
Which means that if you are running one file which sets the memory cache like that:
node set-cache.js
and then run another one like that:
node get-cache.js
It will not work.
It will work within the same process application. For example in one file caching some data, and in another required file, retrieving that data, it will work. Example:
// index.js
// this is your application entrypoint
const cache = require('./cache'); // this is your cache utility
const someOtherUtility = require('./utility') // this is another utility which needs access to cache
// i'm setting some cache
cache.set('testCache', 'testCacheValue', (key, value) => {
// ... Do some stuff
// Call a function from someOtherUtility which will return cached data
const d = someOtherUtility.doSomething('testCache');
console.log(d);
// testCacheValue
});
Your utility.js
// utility.js
const cache = require('./cache');
const doSomething = (cacheKey) => {
return cache.get(cacheKey);
};
Also, when using the var keyword, the variable gets reinstanciated when requiring again the file. So its good to use const. Generally speaking, if you don't intend ever to override your variable, use const.
Conclusion: It works exactly as storing an object, the only difference is that the data has a "time to live".
Upvotes: 1
Reputation: 1
When we access using require
keyword, it creates new instance.
You need to refer same instance in your entire app, rather importing it using require()
,
You can use global
variable and set instance in it and can refer it in different module in same application.
For example, let's say our entry point of application is app.js (i.e. you are starting your app with node app.js
command)
In your app, import your secureCache and assign it in global variable
global.secureCache = require('secure-cache.js')
You can access this global variable in any file to set or get cached values global.secureCache.set('mykey','myval')
or var myval = global.secureCache.get('mykey')
Here is more detail about global variable
Upvotes: 0