I am trying to cache a paginated API call. To fetch the data from Mongo DB using EXPRESS API.
Currently I am storing a page wise data in cache, with key structure mentioned below,
Key :1
{
model: 'bookmarks',
userId: '609bf4a1e9df4334acb2da7a',
collection: '0',
page: '1'
}
Key 2
{
model: 'bookmarks',
userId: '609bf4a1e9df4334acb2da7a',
collection: '0',
page: '2'
}
and so on.
My use case is , if user has made an update to data fetched for particular collection example (0) , I intend to delete all the keys w.r.t. the collection present in the Redis client.
So far I have been able to get all the keys using 'keys *', I can filter and delete data from here, but as recommended in docs it's not recommended for production.
redisClient.keys('*', function (err, keys) {
if (err) return console.log(err);
console.log('keys', keys);
let newArray = [];
console.log('keys', newArray);
keys.forEach(function (key) {
newArray.push(JSON.parse( key));
});
console.log('keys', newArray);
})
I am new to node-redis. Need help to understand the commands to get the key matching a pattern {model:'bookmarks',collection:'0',userId:'1234:'} so that I can clear cache for all the occurrences.