Reputation: 31
I am trying to add cache functionality to the nodejs.
I want to have code like this,
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('success from source');
});
function cache(req,res,next) {
console.log('Inside mycache:' + req.params.id);
client.get('basement' + req.params.id, function (error, result) {
if (error) {
console.log(error);
throw error;
} else {
if(result !== null && result !== '') {
console.log('IN Cache, fetching from cache and returning it');
console.log('Result:' + result);
res.send('success from cache');
} else {
console.log('Not in Cache, so trying to fetch from source ');;
next();
}
}
});
}
I want to apply a middleware function named cache to the request received by the /basement/:id route.
The cache function will receive the key as its parameter. Inside the cache I want to check for existence of cache and if so return it from there, otherwise I want to call the actual route handler. The key is based on one or more request parameters.
In this way, i will end up writing a separate cache function for every handler in my app.
The logic inside my cache function is generic expect for the key, which is based on the actual request object and may differ from method to method.
So, I want to have a generic cache function which can take the key as a parameter, so that I can have code like this,
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('sucess from source');
});
I mean i will pass the cache key to the cache function. So, the cache function can be generic.
But, if I change my cache function like below as so as to receive the cache key, it does not work.
function cache(cachekey,req,res,next) {
}
Seems like I cannot have another parameter in my cache function to receive the passed parameter.
I would like to pass the cache key as a parameter to the function.
If someone has faced a similar issue, can you please help me on this.
Upvotes: 3
Views: 7648
Reputation: 40444
But, if I change my cache function like below as so as to receive the cache key, it does not work.
You can't because it's not a valid express middleware (It's actually an error middleware), express will pass: req
, res
, next
in that order. and err
, req
, res
, next
for error middlewares.
Your cache function
will need to return a middleware instead, so you can pass a key to it.
I wanted to create a generic cache function which can take any cache key. In this case it was id, but other cases may have different keys
function cache(key, prefix = '') {
// Or arrange the parameters as you wish to suits your needs
// The important thing here is to return an express middleware
const cacheKey = prefix + req.params[key];
return (req, res, next) => {
console.log('Inside mycache:' + cacheKey);
client.get(cacheKey , function(error, result) {
if (error) {
console.log(error);
return next(error); // Must be an error object
}
if (result !== null && result !== '') {
console.log('IN Cache, fetching from cache and returning it');
console.log('Result:' + result);
return res.send('success from cache');
}
console.log('Not in Cache, so trying to fetch from source ');;
next();
});
}
}
And now you can use it like this:
app.get('/basement/:id', cache('id', 'basement'), (req, res) => { /* ... */ });
app.get('/other/:foo', cache('foo', 'other'), (req, res) => { /* ... */ });
Upvotes: 8