'does npm node-cache value can be set in one file and access in another
i am working on a nodejs app where i have to maintain an api token for 24hr and then refersh it, currently i am writing that token on json file in every update and it's not good for security, then i came to know about npm node-cache module but it seems it's value cannot be accessed in whole project(like setting in one file and accessing in another), is this how it works or m i missing something?
eg file 1
const NodeCache = require("node-cache");
const myCache = new NodeCache();
myCache.set("token", {token: "123", expirein: 123});
const test2 = require("./test2.js");
console.log(test2);
eg file 2 (test2.js)
const NodeCache = require("node-cache");
const myCache = new NodeCachae();
let c = myCache.get("token");
module.exports = c;
Solution 1:[1]
Don't create a new cache in each file. Create one global cache and use it everywhere.
cache.js
const NodeCache = require("node-cache");
module.exports = new NodeCache();
index.js
const myCache = require("./cache");
myCache.set("token", {token: "123", expirein: 123});
const test2 = require("./test2.js");
console.log(test2);
test2.js
const myCache = require("./cache");
let c = myCache.get("token");
module.exports = c;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jabaa |