So I''m looking at the rails caching features, and I noticed that they''re just a server-side cache (eg, rails caches the outputs of pages/actions/etc and serves those static files instead of doing it dynamically. That''s all well and good, but I''d like to take it a step further and do client-side caching, so that visitor''s browsers will make local caches of the pages. On my old website (using blosxom), I just modified the blosxom script to insert a "page expires in 1 day" HTTP header, which ends up looking something like this: $ curl --head http://rbpark.ath.cx HTTP/1.1 200 OK Date: Thu, 24 Mar 2005 22:53:03 GMT Server: Apache/1.3.33 (Debian GNU/Linux) Expires: Fri, 25 Mar 2005 22:53:04 GMT Content-Type: text/html; charset=ISO-8859-1 Does anybody know how to do this in rails? Thanks. -- One Guy With A Camera http://rbpark.ath.cx
Set the Expires header in @response.headers... e.g. @response.headers[''Expires''] = Time.now + 86400 Cacheing is often more complicated than just "Expires", but the other relevant HTTP headers are set similarly. @response.headers[''Last-Modified''] = @object.updated_at @response.headers[''Cache-Control''] = "public" etc. Andrew On Thu, Mar 24, 2005 at 03:54:17PM -0700, Rob Park wrote:> That''s all well and good, but I''d like to take it a step further and > do client-side caching, so that visitor''s browsers will make local > caches of the pages. On my old website (using blosxom), I just > modified the blosxom script to insert a "page expires in 1 day" HTTP > header, which ends up looking something like this: > > $ curl --head http://rbpark.ath.cx > HTTP/1.1 200 OK > Date: Thu, 24 Mar 2005 22:53:03 GMT > Server: Apache/1.3.33 (Debian GNU/Linux) > Expires: Fri, 25 Mar 2005 22:53:04 GMT > Content-Type: text/html; charset=ISO-8859-1 > > Does anybody know how to do this in rails? Thanks. >
On Thu, 24 Mar 2005 15:17:03 -0800, D Andrew Reynhout <reynhout-Yp+tNMrX9ANBDgjK7y7TUQ@public.gmane.org> wrote:> @response.headers[''Expires''] = Time.now + 86400Thanks. Also, I seem to be having problems with rails'' normal caching... I have this in my controller: caches_page :show, :list, :index But I''m not seeing the cache being created anywhere (searched the whole rails project, the only .html files were in /doc/). I know it''s running in "production" because I have this line in my dispatch.fcgi: ENV[''RAILS_ENV'']="production" If it makes any difference, I have routes set up so that the page I''m trying to cache is accessed by example.com/2005-01-30 for example. Thanks. -- One Guy With A Camera http://rbpark.ath.cx
Rob- You need to make sure the "public" directory is writable by the webserver. Probably you just need to change the permissions using: chmod 777 public Hope this helps. Ben On Thu, 24 Mar 2005 17:25:47 -0700, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thu, 24 Mar 2005 15:17:03 -0800, D Andrew Reynhout > <reynhout-Yp+tNMrX9ANBDgjK7y7TUQ@public.gmane.org> wrote: > > @response.headers[''Expires''] = Time.now + 86400 > > Thanks. > > Also, I seem to be having problems with rails'' normal caching... I > have this in my controller: > > caches_page :show, :list, :index > > But I''m not seeing the cache being created anywhere (searched the > whole rails project, the only .html files were in /doc/). I know it''s > running in "production" because I have this line in my dispatch.fcgi: > > ENV[''RAILS_ENV'']="production" > > If it makes any difference, I have routes set up so that the page I''m > trying to cache is accessed by example.com/2005-01-30 for example. > > Thanks. > > -- > One Guy With A Camera > http://rbpark.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Thu, 24 Mar 2005 19:21:40 -0700, Ben Schumacher <benschumacher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You need to make sure the "public" directory is writable by the > webserver. Probably you just need to change the permissions using: > chmod 777 publicD''oh! Figures it''d be something obvious ;) It even says it was a permission error in the logs, I forgot to check. Anyway, now I have the cache enabled and I can see the cached .html files being created, but I''m observing some strange behavior. What happens is, I have most of my "show" and "list" actions set up so that if you''re logged out, it just lists/shows the items, but if you''re logged in, it displays links to edit and delete those items. If I log in, I can browse the site, and I see those edit and delete links, and I examine the cache files and they are definitely there in the cache files. But then if I log out and browse the same sites, the edit and delete links disappear from the cache files. I checked the timestamps on the .html files, and it appears as though each .html file is simply being re-rendered with each visit to the page. That is to say, rails is just generating this big huge cache of pages without ever actually USING that cache. Is it using some magic to determine when to use the cache and when to render a fresh page and I''m just not noticing? Or am I just doing something wrong? Thanks. -- One Guy With A Camera http://rbpark.ath.cx
On Fri, 25 Mar 2005 00:10:41 -0700, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I checked the timestamps on the .html files, and it appears as though > each .html file is simply being re-rendered with each visit to the > page. That is to say, rails is just generating this big huge cache of > pages without ever actually USING that cache.Ok, I see what the problem is. What''s happening is that the stuff for actually using the cached pages isn''t even part of rails, it''s an apache .htaccess thing. Since I''m using lighttpd, it''s not doing anything with any of the cached pages. If I append ".html" to the end of the URL manually, then I can see the cached pages. And I''ve tested this, I can access the cached pages from a different computer that isn''t logged in and it''ll show the edit/delete stuff. So at least now I see what the problem is, but really, this isn''t cool. In order to actually fix the problem, I would need to: - prevent rails from caching pages rendered by users that are logged in - somehow set up some kind of rewrite in lighttpd that works similarly to the apache one (not hard to do, really, but I had no idea why the caching wasn''t working until reading further into it). Are there any plans to make this more webserver-agnostic, similar to the way routes went from being apache/mod_rewrite only into a more general-purpose rails facility? -- One Guy With A Camera http://rbpark.ath.cx
(sorry for replying to myself so many times) On Fri, 25 Mar 2005 01:44:02 -0700, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So at least now I see what the problem is, but really, this isn''t > cool. In order to actually fix the problem, I would need to: > > - prevent rails from caching pages rendered by users that are logged in > - somehow set up some kind of rewrite in lighttpd that works > similarly to the apache one (not hard to do, really, but I had no idea > why the caching wasn''t working until reading further into it).Yeah, I''ve been fiddling around with this. Getting lighttpd to do the rewrite was trivial, but with that in place there are then two major problems that need to be overcome for rails caching to actually work in my situation: I need to prevent rails from caching pages that were rendered for a logged-in user, and I need somehow for logged in users to bypass the existing cache. The first could probably be done with some simple code somewhere, though I''m not sure how to actually do it. The second is basically impossible without some deep voodoo... since the webserver is just serving a static html file and rails is completely bypassed, there''s no way for any code in rails to actually work around that. I''d need to somehow make lighttpd''s rewrite rules aware of rails'' sessions, and then only do the rewrite if the user is not logged in. I tried using caches_action instead and it had the same problems (though I suppose with caches_action, rails is actually involved, so it would be easier to conditionally disable the cache entirely for logged-in users, but I still don''t know how to do it). Is there any help for me? caches_page is very appealing because it means once one person views the page, it''s cached for everybody (as opposed to regular HTTP caching which only has any effect on returning visitors). All advice is appreciated, thanks. -- One Guy With A Camera http://rbpark.ath.cx