I'm using Nextjs and I cached some API using node-cache. if data doesn't exist in the cache, I request API and put them into cache in server else I get data from the cache. But this API may be updated every 1 month from the backend. I don't how to find out when these data are updated and when should call API and update the cache.
const NodeCache = require("node-cache");
const myCache = new NodeCache();
let navigation = myCache.get("navigation");
let footer = myCache.get("footer");
if (navigation !== undefined) {
response = {
navigation: navigation,
footer: footer,
};
} else {
await Promise.all([promise1]).then(
(res) => {
myCache.set("navigation", res[0].data['navigation-menu'], 3600);
myCache.set("footer", res[0].data['footer-menu'], 3600);
response = {
navigation: res[0].data['navigation-menu'],
footer: res[0].data['footer-menu'],
};
},
);
}
I expire cache every 1 hour but I think this is not the best solution