- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (8)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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 < ActionController::Caching::Sweeper observe List, Item def after_save(record) list = record.is_a?(List) ? record : record.list expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id) expire_action(:controller => "lists", :action => "all") list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) } end end
The sweeper is assigned in 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 for expiring those caches.
You can also name an explicit class in the declaration of a sweeper, which is needed if the sweeper is in a module:
class ListsController < ApplicationController caches_action :index, :show, :public, :feed cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ] end
Better explanation for ActionController sweepers
The first sentence in this description is confusing. http://codelevy.com/articles/2008/03/04/rails-caching-sweepers-controllers-and-models explains it more clearly.
if you get NameError: uninitialized constant ActionController::Caching::Sweeper
I got hit with this on an upgrade. Had a reference to ApplicationController::Base in development.rb (prod as well) which caused this problem http://rails.lighthouseapp.com/projects/8994/tickets/1977
Fix was to remove the loading of ApplicationController::Base and put it in an initializer (where it should have been).