简体   繁体   中英

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 mi 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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM