swaminadhan
Joined: 14 Sep 2009 Posts: 84
|
Posted: Sat May 29, 2010 12:52 pm Post subject: About sweepers! |
|
|
Hello friends!
While am looking about the caching functionality, i came to see about sweepers which is quite interesting. Let us see what is sweepers and its use.
sweepers :
sweepers observe both our models and controllers.In documentation they mentioned as it is half observers.But we can define model callbacks like after_save,after_update,after_create .. etc as well as we can also define before/after callbacks for any controller action like after_create, after_update.
Here we need to look at this, if you only define model callbacks, you don’t need to call the cache_sweeper method on your individual controllers.Instead of doing this we can add our sweeper directly to config.active_record.observers in your config/environment.rb.
There are only two reasons you’d want to call cache_sweeper to register your sweeper:
1. if you want to add callbacks to a controller’s actions; and second, if you need access to the current controller in your sweeper’s model-callback .
methods.
2. you want to call cache_sweeper from your ApplicationController, otherwise you’ll end up observing models that might change from controllers you didn’t explicitely add the cache_sweeper line to, and will depends on a controller variable that doesn’t exist.
so from the above we can say that the Sweepers may implement all ActiveRecord::Observer callbacks. To make these work, you could simply add your sweeper to config.active_record.observers and Sweepers may create before/after callbacks for any controller action. To make these work, you need to use the cache_sweeper method on the controllers in question.
Thank you |
|