Prev | Current Page 271 | Next

Brad Ediger

"Advanced Rails"

Sweepers inherit from
ActionController::Caching::Sweeper and implement the standard callback methods.
Other Systems | 181
class PersonSweeper < ActionController::Caching::Sweeper
observe Person
def after_save(record)
expire_cache(record)
end
def after_destroy(record)
expire_cache(record)
end
def expire_cache(record)
# Actions to take when +record+ is changed or destroyed
expire_page :controller => 'person', :action => 'show', :id => record.id
expire_page :controller => 'person', :action => 'list'
end
end
The sweeper must be activated by name from the controller:
class PersonController < ApplicationController
caches_page :list, :show, :new
cache_sweeper :person_sweeper, :only => [ :create, :update, :destroy ]
end
Because sweepers bridge between the model and controller, it makes sense to create
a new directory for the caching-related classes. Place the sweeper in app/cachers, and
add the following line to your environment.rb file:*
config.load_paths += %W(#{RAILS_ROOT}/app/cachers)
As long as your files are named according to standard Rails conventions (Person-
Sweeper is defined in person_sweeper.rb), Rails will autoload the corresponding file
when an unknown symbol such as PersonSweeper is first encountered. The load_paths
option simply adds to the list of locations that Dependencies searches. We covered
Dependencies in detail in Chapter 2.
Other Systems
The remainder of this chapter is a collection of miscellaneous performance tips and
solutions to common problems.


Pages:
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283