[LRUG] Rails caching

Kevin Fullerton kevin.fullerton at shotgun-suicide.co.uk
Sat May 3 10:28:05 PDT 2008


Hi there,

I've been playing around with Ruby and specifically Rails for a year or so now, and I've just started on a decent-size personal project.

I'm looking at making it as snappy and responsive as possible so I'm trying to build caching for repetitive data fragments into it from as early a stage as possible.

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.

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)

Cheers!

Kevin

--
Go to work, send your kids to school, follow fashion, act normal, walk on the pavement, watch TV, save for your old age, obey the law.  Repeat after me: I am free



More information about the Chat mailing list