Question

I'm not quite sure how to ensure my caching is working, but I'm pretty sure it's not. I have a users controller with an index action that I'm action caching until a new user is created. Here's the code:

UsersController < ApplicationController
  caches_action :index
  def index
    @users = User.all
  end

  def create
    expires_action :index
    ...
  end
end

Now, in my logs when I access the index action, I see:

Cached fragment hit: views/localhost:3000/users (0.0ms)
Filter chain halted as [#<ActionController::Filters::AroundFilter:0xe2fbd3 @identifier=nil, @kind=:filter, @options={:only=>#<Set: {"index", "new"}>, :if=>nil, :unless=>nil}, @method=#<Proc:0x186cb11@/Users/bradrobertson/.rvm/gems/jruby-1.5.3/gems/actionpack-2.3.10/lib/action_controller/caching/actions.rb:64>>] did_not_yield.

I'm not sure what the filter chain halted ... did_not_yield is all about and I also see that that select * from users... is getting called each time, which is not what I expected.

Can someone enlighten me as to what's going on here and why this isn't behaving as I would expect? ie. why the User.all would be running when that whole action's output should have been cached ?

Was it helpful?

Solution

The filter chain halted message means that there is an around filter that stops the action from calling. This is likely to be the action cache, that stops the real action from happening. It did not yield to the action, because it found something in the cache, as suggested by the message above it.

The User.all shouldn't be run at all, because it is in the action, but any before filters will get run. If your page is behind some form of authentication, than the authentication check might have triggered the SQL call. So you might want to double check where the SQL log really comes from.

Also, the proper syntax (according to the rails guide at least) for expiring is:

expire_action :action => :index

More info: Rails Guide

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top