I know using the global
variable is discouraged in node, but assuming I need to store some data globally, which is a worse approach:
using global
Using the require cache directly to store an object, i.e.
require.cache["myGlobalVar"] == {};
I suspect that option 2 is worse because the require cache is intended to be used for modules.
More details: I thought about using require("myLibrary").myGlobalVar but that would require having myLibrary accessible to all the files calling it, which might not be possible in practice. I'm making a code coverage tool so I can expect the user to install "myLibrary" in their dev/test modules, but not necessarily in their src node_modules directory, to which the instrumented code files would refer.
Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.
Node cache is faster to store and retrieve data with a TTL (Time To Live). Things to consider before choosing the right Caching technique: Security risks for storing and retaining the data in the cache. Data object serialization is really required for storing the data.
Avoid global variables or minimize the usage of global variables in JavaScript. This is because global variables are easily overwritten by other scripts. Global Variables are not bad and not even a security concern, but it shouldn't overwrite values of another variable.
Global variables are variables that can be declared with a value, and which can be accessed anywhere in a program. The scope of global variables is not limited to the function scope or any particular JavaScript file. It can be declared at one place and then used in multiple places.
Why don't you just create an in memory cache that all of your other code can then reference?
var memoryCache = module.exports = function () {
var cache = {};
return {
get: function (key) { return cache[key]; },
set: function (key, val) { cache[key] = val; }
}
}
You can then use the cache from any place by require'ing it.
var cache = require('../cache/memoryCache')();
cache.set('1234', 'value I want to share');
cache.get('1234'); // 'value I want to share'
The answer from Bill didn't work for me because it can in fact not be used "from any place by require'ing it". This is because it's exporting a function that is called on each require and thus creating a blank new (empty) cache each time.
The solution is exporting an instance of the function...
// cache/memoryCache.js
module.exports = function () {
var cache = {};
return {
get: function (key) { return cache[key]; },
set: function (key, val) { cache[key] = val; }
}
}();
...and then requiring that very instance each time.
// some other file
var cache = require('../cache/memoryCache');
cache.set('1234', 'value I want to share');
cache.get('1234'); // 'value I want to share'
(Note the difference: the pair of parenthesis "()" is now at the end of the function definition in the module and not after the require.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With