ruby-on-railsrubycachingactioncontrollersweeper

Action caching is not expiring correctly, even when I can see it's being called


I've got a sweeper that's supposed to expire a few action caches. Even though the debugger stops immediately before the call to expire_action, it's not actually expiring the action. Any idea what could be going on?

Here are the relevant sweeper and controller.

#company_sweeper.rb (in 'models' directory)

class CompanySweeper < ActionController::Caching::Sweeper
  observe Company

  def after_save(company)
    expire_cache(company) if company.final_save && company.valid?
  end

  def expire_cache(company)

    debugger                                              <= #debugger stops here!
                                                             right before the call
                                                             I'm trying to make.

    expire_action :controller => 'reports', 
                  :action => 'full_report'
  end
end

#reports_controller.rb

class ReportsController < ApplicationController
  layout false
  caches_action :full_report, :supplier_list, :service_categories
  cache_sweeper :company_sweeper

  def full_report
      #do stuff...
  end
end

The way I know it's not expiring is that the full report returns old data, and responds nearly instantaneously. Weird, right?


Solution

  • Do you have a cache_sweeper declaration in your CompaniesController, too? The sweeper must be included in the controller that performs lifecycle actions on the model in question. Unless you do things with Company instances in ReportsController, the cache_sweeper line doesn't belong there.

    Action caching includes an implicit host name. If the two hits are coming in on different host names, caching is done under one and expiration under a different one.