[LRUG] Rails caching

Pat Allan pat at freelancing-gods.com
Sat May 3 18:31:42 PDT 2008


On 04/05/2008, at 3:28 AM, Kevin Fullerton wrote:

> I wanted to see if anyone had any thoughts if what I'm doing at  
> present was correct, and the best way to deal with caching for it.
>
> In my application.html.erb layout, I'm including a partial which I  
> want displayed on all pages, regardless which controller the user is  
> on - to do so I've got a before_filter in my application.rb  
> controller which gets the relevant data into a variable, using  
> read_fragment to read this information from the cache if possible -  
> I'm planning on using a sweeper to expire the information in the  
> cache when it's needed.
>
> I've also added caches_action for the index and show actions in the  
> controller that deals with this data, which again I'm going to use a  
> sweeper to expire the cache when the data is added/updated/deleted.
>
> For the partial, I'm including it in my layout within a <%  
> cache(:controller => 'base', :action => 'latest_items') %> block -  
> what I'd assumed is that expiring this fragment would only cause the  
> data to be re-read from the database once, but it seems to re-read  
> it once for each cached action in the controller - thinking about  
> it, this makes sense as the action cache needs to be re-built with  
> the new information included in the layout.

Do you have everything in your before_filter method for the fragment  
within an 'unless read_fragment(:controller => 'base', :action =>  
'latest_items'); ... end' statement? That'll save the queries for it  
being run when the fragment exists.

> What I really want to know is if there's any links/blogs that deal  
> with the best way of doing caching this way, if I'm looking at doing  
> it the right way and also if I should worry about caching as much  
> now or rather wait to see what performance is like in the real world  
> (personally I think slight overkill in terms of caching is better at  
> the beginning rather than struggling to implement it once the site  
> starts running slowly)

Sounds like you're approaching it pretty much as Rails expects you to.  
As to whether it's overkill - I'd probably leave out much more caching  
until you see the site getting slow. It's definitely not something to  
worry too much about right now.

As for references, just in case you haven't come across them:
http://www.railsenvy.com/2007/2/28/rails-caching-tutorial
http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2

As caching becomes more important, there are further ways to tweak it:
http://blog.allen.com.au/2007/8/8/the-caching-gap-tm
http://blog.allen.com.au/2007/8/28/out-of-bandwidth-cache-expiry

But they're almost certainly not worth focusing on at this point.

Hope this is helpful - but keep in mind I am not a Caching Expert.

-- 
Pat
e: pat at freelancing-gods.com    || m: 0413 273 337
w: http://freelancing-gods.com || p: 03 9386 0928
discworld: http://ausdwcon.org || skype: patallan



More information about the Chat mailing list