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, this is something i have been very interested in lately. thanks.
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.
@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.
Good job! Thanks for the screencast. Has definitely saved me a few hours of work...
@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?
Hi..
It is a good and informative post..
The way of writing is very nice
Hi..
thanks for sharing such a nice post...It is really a nice idea...
Thanks for the useful code :) Its well hacked together for what I'm looking for :D
I might port it to python later :P
Thank u for sharing,very good and useful for me.I will have eyes on your post,and i am Looking forward to more useful info!
Thanks for the review. 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.
[url=http://www.toptoys2trade.com/power-balance-wholesale-2-c-40/ ]power balance[/url],
I found your blog on Yahoo and I just wanted to say that I think your writing is simply stunning! Thanks again for providing this content for free.
Good stuff! We need to have an object "State" in models/state.rb and I don't have such an object in my project.
That is a idea, I am agree with the opinions of this articles about
Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets
My friendS told me that this blog is competitive. i will continue to read.
I have never read such a wonderful article and I am coming back tomorrow to continue reading.
The article is worth reading, I like it very much. I will keep your new articles.
These information helps me consider some useful things, keep up the good work.
This article is informative and interesting,I enjoy reading it.
It is my pleasure to read this page,I look forward to reading more.
I really like this website , and hope you will write more ,thanks a lot for your information.
So if you want to enable the sweeping jog, you have to tell it in the given controller using cache_sweeper.
So far I have tried using a Privileges table that holds a user_id, role_id and account_id, but I can't find a way to find out if the current user within the current account belongs to a certain role.






