Ryan, your 'casts always get me revved up for a long day of refactoring. You don't talk down to your audience and you don't have a huge rub-ego, and that keeps me coming back for more. I even bought a couple Peepcodes! Well done sir, and keep them coming.
This may be a really dumb question but since the sweeper observes the State model, why is it necessary to also declare the cache_sweeper in the controller?
@Peter D, that's a really good question. I'm not entirely sure why a sweeper behaves differently than an observer. It even has the observe line. My guess is it has something to do with the scope in which it is called. It needs access to the request object which is available to the controller.
@Ben, you can find the snippet here: http://pastie.caboo.se/144239
Maybe someday I'll release a TM bundle with all my snippets, but right now they aren't organized well enough.
I don't like having the cached files appear in public/ -- it makes life hard for doing svn (a whole bunch of spurious cached files appear there). I also don't like the idea of setting public/ world writeable (or even having world-writeable files in the cache) on the production end.
After some effort, I got all this working under nginx and rails 2.0; see instructions here if you're interested:
http://pastie.caboo.se/145116
You don't specifically add the "caches_page" command to the states controller, just the "cache_sweeper" command. Is that in place of the "caches_page" command?
Is it possible to observe more than one model in a sweeper and have different events carried out depending on which model changes?
For example:
class StateSweeper < ActionController::Caching::Sweeper
observe State, Foo
def after_save(state)
expire_cache(state)
end
def before_save(foo)
if foo.someparameter > 1
expire_cache(state)
end
end
def expire_cache(state)
expire_page :controller => 'javascripts', :action => 'dynamic_states', :format => 'js'
end
end
I've tried doing something like the above but i doesn't work - I can't figure out how to differentiate between which model changes, a State model or Foo model - the before_save and after_save functions are triggered whether the model being changed is a State or a Foo ...
I could probable use instanceof? inside each action, but this seems a little messy.
@eddie, I would say that it is possible, the parameter passed to the active record observer methods (after_save and after_destroy) is the record which can be tested to check what it is, check out the api documentation, it has an example using two models: http://api.rubyonrails.com/classes/ActionController/Caching/Sweeping.html
How do you suggest removing cache for a large number of items and multiple conditions. For example domain.com/:controlller/:action/:id/:season/:collection. This url could leave hundreds of cached files.
I see the docs suggest looping through each item but in my situation this is not reasonable.
My application has blog section, where the updates will be happen often (lets assume 5 min once).people always comment for the blog. so if i delete the cached page every time while the comment has entered, what is the point of doing caching in this case. is there any other technique available to overcome this?
maybe you should release a TM Bundle with all your nifty snippets in :-)
This episode is not showing in the "Full" podcast, its at episode 88. However, the iPod feed is current.
Ryan, your 'casts always get me revved up for a long day of refactoring. You don't talk down to your audience and you don't have a huge rub-ego, and that keeps me coming back for more. I even bought a couple Peepcodes! Well done sir, and keep them coming.
This may be a really dumb question but since the sweeper observes the State model, why is it necessary to also declare the cache_sweeper in the controller?
Peter D, I was wondering the same thing? Are we missing something?
Peter D and Garrett Heaver:
I don't necesseraly wants to do the sweeping job in every controller. It's a matter of features and needs.
So if you want to enable the sweeping jog, you have to tell it in the given controller using cache_sweeper.
Please post the sweeper snippet!
@Peter D, that's a really good question. I'm not entirely sure why a sweeper behaves differently than an observer. It even has the observe line. My guess is it has something to do with the scope in which it is called. It needs access to the request object which is available to the controller.
@Ben, you can find the snippet here: http://pastie.caboo.se/144239
Maybe someday I'll release a TM bundle with all my snippets, but right now they aren't organized well enough.
This is, as usual, quite awesome.
I don't like having the cached files appear in public/ -- it makes life hard for doing svn (a whole bunch of spurious cached files appear there). I also don't like the idea of setting public/ world writeable (or even having world-writeable files in the cache) on the production end.
After some effort, I got all this working under nginx and rails 2.0; see instructions here if you're interested:
http://pastie.caboo.se/145116
This screencast and the before are great.
I just want to share a tip for a small caching challenge that I had.
usually I set the cache directory to "public/cache", but this "break" the caching of dynamic javascripts.
A solution that I found is to redefine @page_cache_directory on javascripts_controller
Check it here: http://pastie.caboo.se/150570
Hope it may be useful.
Marcus
I have a problem when trying to use this tutorial :(
I get this error message :
uninitialized constant StateSweeper::State
RAILS_ROOT: C:/developement/ruby/minotaur
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing'
app/sweepers/state_sweeper.rb:3
-e:2:in `load'
-e:2
I double checked and I don't see what I forgot :(
Sweeping doc is not really helpful either:
http://rails.rubyonrails.com/classes/ActionController/Caching/Sweeping.html
any idea ?
Ok never mind I got it.
We need to have an object "State" in models/state.rb and I don't have such an object in my project.
The pastie for textmate helped me to understand http://pastie.caboo.se/144239 ;)
How would I do this for a RESTful resource? Specifically, would I just put the path in the expire_page command?
def expire_cache(article)
expire_page article_path(article)
end
Another question:
You don't specifically add the "caches_page" command to the states controller, just the "cache_sweeper" command. Is that in place of the "caches_page" command?
Is it possible to observe more than one model in a sweeper and have different events carried out depending on which model changes?
For example:
class StateSweeper < ActionController::Caching::Sweeper
observe State, Foo
def after_save(state)
expire_cache(state)
end
def before_save(foo)
if foo.someparameter > 1
expire_cache(state)
end
end
def expire_cache(state)
expire_page :controller => 'javascripts', :action => 'dynamic_states', :format => 'js'
end
end
I've tried doing something like the above but i doesn't work - I can't figure out how to differentiate between which model changes, a State model or Foo model - the before_save and after_save functions are triggered whether the model being changed is a State or a Foo ...
I could probable use instanceof? inside each action, but this seems a little messy.
@eddie, I would say that it is possible, the parameter passed to the active record observer methods (after_save and after_destroy) is the record which can be tested to check what it is, check out the api documentation, it has an example using two models: http://api.rubyonrails.com/classes/ActionController/Caching/Sweeping.html
How do you suggest removing cache for a large number of items and multiple conditions. For example domain.com/:controlller/:action/:id/:season/:collection. This url could leave hundreds of cached files.
I see the docs suggest looping through each item but in my situation this is not reasonable.
list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
Thanks for the great screencast!!!!
My application has blog section, where the updates will be happen often (lets assume 5 min once).people always comment for the blog. so if i delete the cached page every time while the comment has entered, what is the point of doing caching in this case. is there any other technique available to overcome this?
Hello, movie was cropped.
Would you adjust the width of screencasts?