Alex Rivera | Logout

Best practices for using memcached in Rails?

Asked 2009-05-09T07:36:27.023
14

as database transcations in our app are getting more and more time consuming, we have started to use memcached to reduce the amount of queries passed to MySQL.

All in all, it works fine and really saves a lot of time.

But as caching was "silently appearing" as a workaround to give the app more juice, a lot of our models now contain code like this:

def self.all_cached

  Rails.cache.fetch('object_name') {

    find(

      :all,
      :include => [associations])

    }

end

This is getting more and more a pain as filling and flushing the cache happens in several classes accross the application.

Now, I was wondering if there was a better way to abstract memcached logic to make it more powerful and easy to use across all needed models?

I was thinking about having some kind of memcached-module which is included in all needed modules.

But before playing around, I thought: Let's ask experts first :-)

Thanks

Matt

Edit
Report

1 Answer

5

Yes, having a memcached module that you include into all the models that you need it in I think is the best solution. Your line of thinking is superb :)

answered 2009-05-09T10:32:22.700

Your Answer