You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to build a simple cmd line tool to invalidate the caches. I wrap node-cache in a middleware and use it in the route index js like this, where it checks if the key (query string) exists or not, if yes, returns the cached response otherwise go to next step.
I am using Commander.js as a tool to build my cmd line on top of it. The idea is to invalidate caches if there are any.
So I exported cache object that is created in middleware to ensure it's same instance that is used in the middleware to set and get cache. However in the actual command line tool js file, when listing cache keys, there is none of them whereas it actually should have keys that were previously cached.
Middleware file looks like:
const cache = new NodeCache();
export { cache };
export default (duration) => {
const middleware = (req, res, next) => {
// Assert that only GET request is cached
if (req.method !== 'GET') {
return next();
}
// Check whether the key exists
const key = req.originalUrl;
const cachedResponse = cache.get(key);
const allKeys = cache.keys();
const stats = cache.getStats();
console.log(allKeys);
console.log(stats);
if (cachedResponse) {
return res.send(cachedResponse);
}
}
// Set the key and body as well as duration and move on to next if key does not exist
res.originalSend = res.send;
res.send = (body) => {
res.originalSend(body);
cache.set(key, body, duration);
};
return next();
};
middleware.unless = unless;
return middleware;
};
Command line file looks like:
#!/usr/bin/env node
import { Command } from 'commander';
import { cache } from '../middlewares/routeCache';
const program = new Command();
program
.name('invalidate-cache')
.description('CLI to invalidate any local cache')
.version('0.0.1', '-v, --vers', 'output the current version');
program
.command('invalidate')
.description('Invalidate any specified local json cache')
.argument('<string>', 'cache key to be invalidated')
.option('-d, --delete [key]', 'delete the given cache key')
.option('-a, --all', 'delete all cache keys')
.option('-s, --separator <char>', 'delete the cache keys by providing separator character', ',')
.action((key, options) => {
// delete cached keys
cache.del(key);
});
program
.command('list')
.description('List all the available cache keys')
.option('-l, --list', 'display all the available keys')
.action((key, options) => {
// list all cached keys
cache.keys();
});
program.parse();
Does anyone know why it is like that? Is that because when importing a module every time, it actually created a new instance of NodeCache?
The text was updated successfully, but these errors were encountered:
Each file execution is considered a seperate process so when you executed server.js which created the instance of NodeCache in middleware will only be accessible within that process(modules used in server.js)
When an external file(cli.js) uses the same middleware file outside of the server process to get the NodeCache instance, it will be considered as a new process and node will re-instantiate NodeCache
Hi everyone 馃憢
I am trying to build a simple cmd line tool to invalidate the caches. I wrap node-cache in a middleware and use it in the route index js like this, where it checks if the key (query string) exists or not, if yes, returns the cached response otherwise go to next step.
router.use('/v1/profile', rateLimiter(TEN_MIN_IN_MILLESEC, 30), Cache(TWO_HOURS_IN_SECOND), profile);
I am using Commander.js as a tool to build my cmd line on top of it. The idea is to invalidate caches if there are any.
So I exported cache object that is created in middleware to ensure it's same instance that is used in the middleware to set and get cache. However in the actual command line tool js file, when listing cache keys, there is none of them whereas it actually should have keys that were previously cached.
Middleware file looks like:
Command line file looks like:
Does anyone know why it is like that? Is that because when importing a module every time, it actually created a new instance of NodeCache?
The text was updated successfully, but these errors were encountered: