Module: ActionController::Caching::Sweeping

Defined in:
lib/action_controller/caching.rb

Overview

Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change. They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:

class ListSweeper < ActiveRecord::Observer
  observe List, Item

  def after_save(record)
    @list = record.is_a?(List) ? record : record.list
  end

  def filter(controller)
    controller.expire_page(:controller => "lists", :action => %w( show public feed ), :id => @list.id)
    controller.expire_action(:controller => "lists", :action => "all")
    @list.shares.each { |share| controller.expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
  end
end

The sweeper is assigned on the controllers that wish to have its job performed using the cache_sweeper class method:

class ListsController < ApplicationController
  caches_action :index, :show, :public, :feed
  cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
end

In the example above, four actions are cached and three actions are responsible of expiring those caches.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

:nodoc:



376
377
378
379
# File 'lib/action_controller/caching.rb', line 376

def self.append_features(base) #:nodoc:
  super
  base.extend(ClassMethods)
end