Mongoose redis caching Mongoose redis caching mongoose mongoose

Mongoose redis caching


clearKey(Book.collection.collectionName) In short, it will clear all the cache for the collection.


TLDR

In your case this.hashKey = JSON.stringify(options.key || this.mongooseCollection.name); is collectionName


https://redis.io/commands/hget

Returns the value associated with field in the hash stored at key.

clearKey(hashKey) {    client.del(JSON.stringify(hashKey));}

https://redis.io/commands/del

Removes the specified keys. A key is ignored if it does not exist.

So when you call clearKey(Book.collection.collectionName); it calls client.del which will delete all the records for that particular collection. as the complete hash is deleted.


To delete specific fields not the full hash :-

https://redis.io/commands/HDEL

Removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.