rfink / sequelize-redis-cache

Small fluent interface for caching sequelize database query results in redis more easily
MIT License
175 stars 47 forks source link

Implement cache invalidation #32

Open KieronWiltshire opened 7 years ago

KieronWiltshire commented 7 years ago

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?

ColonelBundy commented 7 years ago

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
}; 
KieronWiltshire commented 7 years ago

Thanks man!

idangozlan commented 6 years ago

@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.