hello, unless i missed something, i did not find a good description of how caching is implemented in rails. i would appreciate any pointers if such a description exists. if not, would the experts please clarify this: as far as i could tell, rails can save output of views in to files and grab these files on subsequent requests. when a request comes it is always processed by rails, no matter whether the response is already cached or not. if the response is cached, rails grabs the file and writes its contents in to the response stream. if there is no cache, the usual controller/model/view mechanism is invoked. basically, i am trying to find out whether rails uses the web server to serve cached files or does it itself "manually" (which i believe is less efficient than just letting the server do it). thanks konstantin
Rails-level caching and server-level caching are independent (usually both are used together). On 10/13/05, akonsu <akonsu@gmail.com> wrote:> hello, > > unless i missed something, i did not find a good description of how > caching is implemented in rails. i would appreciate any pointers if > such a description exists. if not, would the experts please clarify > this: > > as far as i could tell, rails can save output of views in to files and > grab these files on subsequent requests. when a request comes it is > always processed by rails, no matter whether the response is already > cached or not. if the response is cached, rails grabs the file and > writes its contents in to the response stream. if there is no cache, > the usual controller/model/view mechanism is invoked. > > basically, i am trying to find out whether rails uses the web server > to serve cached files or does it itself "manually" (which i believe is > less efficient than just letting the server do it). > > thanks > konstantin > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Rails writes the pages you ask it to cache into .html files in the RAILS_ROOT/public directory. When the first request comes in for a page you want cached rails serves the page and writes it to disk. On all subsequent requests the webserver will serve the static .html file directly avoiding rails altogether. If you erase or invalidate the cached file the next request will build it agina in rails and then the webserver takes over again. The way this works is that your rails dispatch.fcgi file is set to be the error handler for your webserver. So if the web server does not find a file on diak that matches the request, it invokes rails as the 404 error handler and rails does its thing. This way you can serve cached files as fats as your webserver and connection will go. HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org On Oct 13, 2005, at 4:01 PM, akonsu wrote:> hello, > > unless i missed something, i did not find a good description of how > caching is implemented in rails. i would appreciate any pointers if > such a description exists. if not, would the experts please clarify > this: > > as far as i could tell, rails can save output of views in to files and > grab these files on subsequent requests. when a request comes it is > always processed by rails, no matter whether the response is already > cached or not. if the response is cached, rails grabs the file and > writes its contents in to the response stream. if there is no cache, > the usual controller/model/view mechanism is invoked. > > basically, i am trying to find out whether rails uses the web server > to serve cached files or does it itself "manually" (which i believe is > less efficient than just letting the server do it). > > thanks > konstantin > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Ah. Good to know. Someone should put this on the wiki if it isn't already there... On 10/13/05, Ezra Zygmuntowicz <ezra@yakima-herald.com> wrote:> Rails writes the pages you ask it to cache into .html files in the > RAILS_ROOT/public directory. When the first request comes in for a > page you want cached rails serves the page and writes it to disk. On > all subsequent requests the webserver will serve the static .html > file directly avoiding rails altogether. If you erase or invalidate > the cached file the next request will build it agina in rails and > then the webserver takes over again. > The way this works is that your rails dispatch.fcgi file is set > to be the error handler for your webserver. So if the web server does > not find a file on diak that matches the request, it invokes rails as > the 404 error handler and rails does its thing. This way you can > serve cached files as fats as your webserver and connection will go. > > HTH- > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > http://yakimaherald.com > 509-577-7732 > ezra@yakima-herald.com > > On Oct 13, 2005, at 4:01 PM, akonsu wrote: > > > hello, > > > > unless i missed something, i did not find a good description of how > > caching is implemented in rails. i would appreciate any pointers if > > such a description exists. if not, would the experts please clarify > > this: > > > > as far as i could tell, rails can save output of views in to files and > > grab these files on subsequent requests. when a request comes it is > > always processed by rails, no matter whether the response is already > > cached or not. if the response is cached, rails grabs the file and > > writes its contents in to the response stream. if there is no cache, > > the usual controller/model/view mechanism is invoked. > > > > basically, i am trying to find out whether rails uses the web server > > to serve cached files or does it itself "manually" (which i believe is > > less efficient than just letting the server do it). > > > > thanks > > konstantin > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > http://yakimaherald.com > 509-577-7732 > ezra@yakima-herald.com > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> file directly avoiding rails altogether. If you erase or invalidate > the cached file the next request will build it agina in rails and > then the webserver takes over again.thank you. what does "invalidate" mean? i understand that if the server does not find the file, it invokes a 404 script. but how does the server know if the file is stale? thanks konstantin
On Oct 13, 2005, at 5:03 PM, akonsu wrote:>> file directly avoiding rails altogether. If you erase or invalidate >> the cached file the next request will build it agina in rails and >> then the webserver takes over again. >> > > thank you. what does "invalidate" mean? i understand that if the > server does not find the file, it invokes a 404 script. but how does > the server know if the file is stale?If you read up in the rails api docs about caches_page and and expire_page you will see that you can cache your pages with a chaches_page declaration in your controller. Then in any method that updates the objects that get used in the cached page you expire the cache and rails erases the cached file for you so on the next request your page gets built dynamically and cached again. Here is an after_save callback method that will invalidate the cache for you automatically when a page that is cached needs to be updated: def after_save(record) list = record.is_a?(List) ? record : record.list expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id) expire_action(:controller => "lists", :action => "all") list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) } end You can find the api docs here: http://api.rubyonrails.com/ . Here are some links to the api docs for caching: ActionController::Caching ActionController::Caching::Actions ActionController::Caching::Fragments ActionController::Caching::Pages ActionController::Caching::Pages::ClassMethods ActionController::Caching::Sweeping Hope that clears thing up a bit- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails