A cache sweeper is an observer with some hooks on the model it is observing:
class MySweeper < ActionController::Caching::Sweeper
observe MyModel
def after_update(my_model)
expire_page(...)
end
end
But documentation and guides also say to declare the sweeper in the controller:
class ApplicationController < ActionController::Base
cache_sweeper :my_sweeper
end
Why is this? Isn't the point of an observer that it observers the model and takes action? Shouldn't the controller not have to be aware of when the cache is expiring or what is causing it?
clarification
my understanding of setting up the sweeper to be an observer is that it means "in all cases, when a MyModel is updated, run this cleanup code"
- Is that accurate?
- If so, then why does
cache_sweeper :my_sweeper
also need to be declared in a controller? What does that do?