views:

74

answers:

1

I'm fetching and manipulating XML from twitter and flickr in my rails app. The results appear on every page and the parsing is handled in the Application Controller with Hpricot and open-uri.

This is my first experiment with action caching and it doesn't seem to be working. I'm in dev mode using WEBRick. Everything appropriate is set to true in development.rb.

Here's what's in the controller:

  before_filter :twitter, :flickr
  caches_action :twitter, :flickr

Nothing shows up in /tmp/cache, and it's clear that Hpricot is doing it's thing on every page load.

Thanks in advance.

+2  A: 

The default cache store is memory for action and fragment caching so nothing would appear in tmp/cache. You can change that to file_store for debugging purposes.

I'd recommend gem installing mongrel and using that instead of webrick in development mode. It's faster and it provides better information at a glance than webrick.

When actions are cached, all before filters still run.

What it looks like you are doing is running your twitter and flickr actions as plain old methods in the before_filter. This will not invoke caching. You should look into fragment caching and change your controller to be:

class SomeController
  before_filter :twitter, :flickr

  protected
  def twitter
    unless read_fragment('twitter')
      ... do stuff ...
    end
  end
end

#_twitter.erb
<% cache('twitter') do %>
  render the relevant stuff
<% end %>
Ben Hughes
+1 Something similar worked for me in the past.
Swanand