[cross-posting to rails and typo lists] Hi, I''m tinkering with the code for typo, a rails-based blog engine. Typo has an XmlController with actions "rss" and "atom" which generate feed.xml files, and it uses a caches_page declaration to cache the files that get generated by those actions. It also has a Sweeper (Observer) class that expires those cached files when a blog post is created or modified: http://typo.leetsoft.com/trac/file/trunk/app/controllers/xml_controller.rb http://typo.leetsoft.com/trac/file/trunk/app/models/blog_sweeper.rb The normal behavior is that the files get deleted when expire_cache is called, and then re-created the next time the XmlController action is invoked. Now, I''m trying to force the files to be re-cached immediately after the expire_cache call from the Sweeper class. However I could not find a way to do that successfully. I''ve tried several ways, including instantiating the controller in the sweeper and calling the actions directly (which doesn''t work), and the closest I got was using render_component_as_string from the base ApplicationController. That sort-of works in that it at least it calls the proper XmlController action, but it has two problems: the pages aren''t cached, and also the response type changes to xml (which confuses the browser since the response is a redirect). I wonder if anyone has a good idea of how the caching and stuff works, and what I can do to force certain controller actions to run and get cached as if they had been invoked by the browser, but instead invoking them from within an observer or controller. I''m using rails 0.12.1 on ruby 1.8.2. Thanks in advance for any help, please let me know if you need any more info. --Matias
Ezra Zygmuntowicz
2005-Jun-26 02:09 UTC
Re: calling controller and caching from a sweeper class
On Jun 25, 2005, at 7:01 PM, Matias Pelenur wrote:> [cross-posting to rails and typo lists] > > Hi, > I''m tinkering with the code for typo, a rails-based blog engine. > Typo has an XmlController with actions "rss" and "atom" which > generate feed.xml files, and it uses a caches_page declaration to > cache the files that get generated by those actions. It also has a > Sweeper (Observer) class that expires those cached files when a > blog post is created or modified: > > http://typo.leetsoft.com/trac/file/trunk/app/controllers/ > xml_controller.rb > http://typo.leetsoft.com/trac/file/trunk/app/models/blog_sweeper.rb > > The normal behavior is that the files get deleted when expire_cache > is called, and then re-created the next time the XmlController > action is invoked. > > Now, I''m trying to force the files to be re-cached immediately > after the expire_cache call from the Sweeper class. However I could > not find a way to do that successfully. > > I''ve tried several ways, including instantiating the controller in > the sweeper and calling the actions directly (which doesn''t work), > and the closest I got was using render_component_as_string from the > base ApplicationController. That sort-of works in that it at least > it calls the proper XmlController action, but it has two problems: > the pages aren''t cached, and also the response type changes to xml > (which confuses the browser since the response is a redirect). > > I wonder if anyone has a good idea of how the caching and stuff > works, and what I can do to force certain controller actions to run > and get cached as if they had been invoked by the browser, but > instead invoking them from within an observer or controller. > > I''m using rails 0.12.1 on ruby 1.8.2. > Thanks in advance for any help, please let me know if you need any > more info. > > --Matias > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >You could always do something like this: system("wget http://www.myrailsapp.com/controller/action >> /dev/null") Kind of a hack but it would probably do what you are trying to do... -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Matias Pelenur
2005-Jun-27 08:00 UTC
Re: calling controller and caching from a sweeper class
Just wanted to post again before giving up on this... Is there a way to call a controller action directly and have the paged cached? See below for the whole story. So far the only solutions proposed are doing a system(wget), but there must be a better one... In any case even that option doesn''t work; the reason I have this problem is because I want to password-protect (via Basic auth) the web app, but allow free access only to those feed .xml files. Because of the way auth goes through Apache, and because I''m using FastCGI so everything goes through dispatch.fcgi, I can only do what I want if I always have the physical cached files. In any case, even without this convoluted scenario of mine, there must be a way to do what I want... Imagine if some cached pages took a long time to generate; you might want to force-refresh them when there''s a change to the model? Thanks, matias Matias Pelenur wrote:> [cross-posting to rails and typo lists] > > Hi, > I''m tinkering with the code for typo, a rails-based blog engine. > Typo has an XmlController with actions "rss" and "atom" which generate > feed.xml files, and it uses a caches_page declaration to cache the files > that get generated by those actions. It also has a Sweeper (Observer) > class that expires those cached files when a blog post is created or > modified: > > http://typo.leetsoft.com/trac/file/trunk/app/controllers/xml_controller.rb > http://typo.leetsoft.com/trac/file/trunk/app/models/blog_sweeper.rb > > The normal behavior is that the files get deleted when expire_cache is > called, and then re-created the next time the XmlController action is > invoked. > > Now, I''m trying to force the files to be re-cached immediately after the > expire_cache call from the Sweeper class. However I could not find a way > to do that successfully. > > I''ve tried several ways, including instantiating the controller in the > sweeper and calling the actions directly (which doesn''t work), and the > closest I got was using render_component_as_string from the base > ApplicationController. That sort-of works in that it at least it calls > the proper XmlController action, but it has two problems: the pages > aren''t cached, and also the response type changes to xml (which confuses > the browser since the response is a redirect). > > I wonder if anyone has a good idea of how the caching and stuff works, > and what I can do to force certain controller actions to run and get > cached as if they had been invoked by the browser, but instead invoking > them from within an observer or controller. > > I''m using rails 0.12.1 on ruby 1.8.2. > Thanks in advance for any help, please let me know if you need any more > info. > > --Matias >
Matias Pelenur
2005-Jun-27 08:01 UTC
Re: calling controller and caching from a sweeper class
Just wanted to post again before giving up on this... Is there a way to call a controller action directly and have the paged cached? See below for the whole story. So far the only solutions proposed are doing a system(wget), but there must be a better one... In any case even that option doesn''t work; the reason I have this problem is because I want to password-protect (via Basic auth) the web app, but allow free access only to those feed .xml files. Because of the way auth goes through Apache, and because I''m using FastCGI so everything goes through dispatch.fcgi, I can only do what I want if I always have the physical cached files. In any case, even without this convoluted scenario of mine, there must be a way to do what I want... Imagine if some cached pages took a long time to generate; you might want to force-refresh them when there''s a change to the model? Thanks, matias Matias Pelenur wrote:> [cross-posting to rails and typo lists] > > Hi, > I''m tinkering with the code for typo, a rails-based blog engine. > Typo has an XmlController with actions "rss" and "atom" which generate > feed.xml files, and it uses a caches_page declaration to cache the files > that get generated by those actions. It also has a Sweeper (Observer) > class that expires those cached files when a blog post is created or > modified: > > http://typo.leetsoft.com/trac/file/trunk/app/controllers/xml_controller.rb > http://typo.leetsoft.com/trac/file/trunk/app/models/blog_sweeper.rb > > The normal behavior is that the files get deleted when expire_cache is > called, and then re-created the next time the XmlController action is > invoked. > > Now, I''m trying to force the files to be re-cached immediately after the > expire_cache call from the Sweeper class. However I could not find a way > to do that successfully. > > I''ve tried several ways, including instantiating the controller in the > sweeper and calling the actions directly (which doesn''t work), and the > closest I got was using render_component_as_string from the base > ApplicationController. That sort-of works in that it at least it calls > the proper XmlController action, but it has two problems: the pages > aren''t cached, and also the response type changes to xml (which confuses > the browser since the response is a redirect). > > I wonder if anyone has a good idea of how the caching and stuff works, > and what I can do to force certain controller actions to run and get > cached as if they had been invoked by the browser, but instead invoking > them from within an observer or controller. > > I''m using rails 0.12.1 on ruby 1.8.2. > Thanks in advance for any help, please let me know if you need any more > info. > > --Matias >
Lachlan Laycock
2006-Aug-08 07:15 UTC
[Rails] Re: calling controller and caching from a sweeper class
I am trying to do the same thing... seems like even the typo guys could''nt get it to work... Did you have any luck? see: http://www.typosphere.org /trac/changeset/1072?format=diff&new=1072 +class PageCache < ActiveRecord::Base + cattr_accessor :public_path + @@public_path = ActionController::Base.page_cache_directory + + def self.sweep(pattern) + destroy_all("name like ''#{pattern}''") + end + + def self.sweep_all + destroy_all + end + + private + + after_destroy :expire_cache + + def expire_cache + # It''d be better to call expire_page here, except it''s a + # controller method and we can''t get to it. + path = PageCache.public_path + "/#{self.name}" + + logger.info "Sweeping #{self.name}" + delete_file(path) + end + + def delete_file(path) + File.delete(path) if File.file?(path) + end +end