sequelize-redis-cache icon indicating copy to clipboard operation
sequelize-redis-cache copied to clipboard

Implement cache invalidation

Open KieronWiltshire opened this issue 8 years ago • 3 comments

So what I'd like to see implemented, is if the "create" method or similar is executed, then the cache will be invalidated.

For example, if I do a findAll, then a create, and repeat the findAll, I'd like to be able to retrieve the latest included within that find.

Is this possible?

KieronWiltshire avatar Mar 21 '17 12:03 KieronWiltshire

Yes this is possible, you can achieve this by adding hooks and implement the logic for invalidation there.

Example:

Model.hook('afterCreate', function(instance, options) {
  // Invalidate/update cache here
};

Model.hook('afterDestroy', function(instance, options) {
  // Invalidate/update cache here
}; 

Model.hook('afterUpdate', function(instance, options) {
  // Invalidate/update cache here
};

Model.hook('afterSave', function(instance, options) {
  // Invalidate/update cache here
};

Model.hook('AfterUpsert', function(created, options) {
  // Invalidate/update cache here
}; 

ColonelBundy avatar Jun 19 '17 18:06 ColonelBundy

Thanks man!

KieronWiltshire avatar Jul 17 '17 22:07 KieronWiltshire

@KieronWiltshire If you are using Sequelize 4, check out that module:

https://github.com/idangozlan/sequelize-redis

It's a full solution for caching + invalidating cache easily, and as much as I know it's the only Sequelize 4 caching module (right now).

Disclaimer: I'm the author of that module and I'm using that on production for daily traffic of 1m unique users.

idangozlan avatar Jan 22 '18 18:01 idangozlan