views:

2613

answers:

3

Hello everybody,

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

+1  A: 

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 :)

Ryan Bigg
Thanks, works brilliant.
Matt
can you recommend one or are you saying write one from scratch? this answer doesn't seem complete
Brian Armstrong
+2  A: 

I would recommend checking out the existing plugins, the two big ones are cache_fu and cache money. For your use case where you want to load the model with its associations out of cache I would strongly recommend that you try Cache Money, which does it almost automatically:

def parent < ActiveRecord::Base
  has_many children
end

def child < ActiveRecord::Base
  index :parent_id
end

#now you can do the following without ever hitting the DB
parents = Parent.find :all
parents.each{ |p| p.children }

The really big win with Cache Money is that when you scale up to the point where database replication lag becomes an issue, write through caching saves your ass. This is especially important with Rails where replication lag can easily cause 500 errors and generally is a nightmare that you don't want to have.

Sam
A: 

Hi all, I'm studying how to use caching in Heroku for my Rails app. HTTP cache powered by Varnish is superb and I'll use it in all pages without user info but I also want to use a kind of ActiveRecord caching with Memcached using "high livel" plugins such as cache_fu or cache-money.

But it seems that Heroku supports only the memcached gem (http://docs.heroku.com/memcache) and it's a very low level Memcachad API...

Do you have any other solutions?

Thx.

zetarun