rfink / sequelize-redis-cache

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

Indicate whether this does automatic invalidation #4

Closed rattrayalex closed 9 years ago

rattrayalex commented 9 years ago

I'm guessing the answer is no, but I was wondering whether this project does automatic cache invalidation, or only ttl. Can the answer be present in the README?

If you are interested in supporting this, but need help implementing it, I might be interested in contributing if you can post a roadmap.

rfink commented 9 years ago

Thanks very much for posting. How do you mean invalidation, for example clearing a cache entry if one of the records references changes? If so, that could be tricky to implement, as it would require use of the app for saving and inserting, as well as tracking all records involved with the fetch. Could you elaborate?

rattrayalex commented 9 years ago

Yes, that's what I mean. And yes, it is tricky :-)

I would assume that anyone wanting to use this kind of lib for invalidation would count on all writes coming from the app, and would handle other writes manually (eg; listening to RabbitMQ events).

An example implementation is django-cache-machine, which lets you extend your ORM models to be Cached models. The equivalent here would be installing beforeUpdate hooks for invalidation. The base implementation is here.

Other examples are django-cacheops, and johnny-cache which do things a little differently.

rfink commented 9 years ago

I really like the idea, I do believe that would change the direction of the library significantly. For example, all new inserts would have to be run against all possible cached records in order to know which to invalidate correctly. I will definitely make an effort to get the README.md updated today to reflect. If you want to take a stab and then make a P.R. (or even just a pseudo-code implementation and email it to me, or post it here, or as a gist), I'd be excited to take a look. At any rate, thanks very much for your request.

rattrayalex commented 9 years ago

Cool! Appreciate the README update and will take a look & a think at how this could be done. I'm currently not a user of sequelize - we're evaluating whether or not to use it - but this would be a fun project regardless.

Also agreed that it would dramatically change the lib, so might make more sense to move to a new project. If you're excited about it though, would prefer to keep it here.

rfink commented 9 years ago

I've updated the README to reflect that this does not currently handle that. If you are interested in taking a stab at that functionality, I think it would be a really cool idea.

As far as sequelize, I recommend it only because there are few, if any, viable alternatives. The closest possible competitor in my opinion is bookshelf.js, but I don't like how it requires a bootstrapped connection just to create models, and it doesn't have a very documented way of defining a schema (I always like checking in my schemas to VCS). Both are promise based, which fits in really well with koa/co.js. Sequelize has migrations, which are pretty cool. There are also fixtures libraries which load in json/xml/yaml data to your db, which makes unit testing a lot easier.

I wish there were a mongoose.js, but for SQL :(

rattrayalex commented 9 years ago

Awesome, thanks so much!

Can I ask - what's lacking in sequelize? I don't have experience with it or mongoose - what makes mongoose better?

rfink commented 9 years ago

Well mongoose is an ORM (ODM?) for mongodb. It has great handling for relations (strangely enough for a non relational db), it has validations, hooks, and quite a rich query model.

Sequelize is actually gaining most of those things as of 2.0+, so it's really coming along. I'm still not a huge fan of the default recommended way of setting up relationships between models (the associate static method), and the nested querying is still a little buggy. But I still maintain that it's the best ORM for relational databases in node.

rattrayalex commented 9 years ago

Awesome, thanks! That makes a lot of sense.

MVMS1994 commented 8 years ago

Hey! is there any functionality to force clear cache or force update the cache? I read the code, clearCache is supposed to clear I guess. If yes, can you explain what's procedure to use? Thanks in advance.

rfink commented 8 years ago

Hello! There is functionality on an individual "query" basis. For example, if you run a simple "where" query with the module, the resulting "cacher" object can be used to clear the cache for the specific query (this is in the test file):

var query = { where: { createdAt: inst.createdAt } };
    var obj = cacher('entity')
      .ttl(1);
    return obj.find(query)
      .then(function(res) {
        obj.cacheHit.should.equal(false);
        var obj2 = cacher('entity')
          .ttl(1);
        return obj2.find(query)
          .then(function(res) {
            should.exist(res);
            obj2.cacheHit.should.equal(true);
            obj2.clearCache().then(function() {
              return done();
            }, onErr);
          }, onErr);
      }, onErr);

This only clears the cache, it does not force an update of the cached results. That is potentially a piece of functionality I could implement, could you give me an example of how you are thinking of using it?

MVMS1994 commented 8 years ago

Thank you so much. This will solve my issue. :+1: :)

rfink commented 8 years ago

Great! Thanks for using.