views:

47

answers:

1

I'd love to use page caching on a Rails site I run. The information on each page is mostly constant, but the queries that need to be run to collect the information are complicated and can be slow in some cases.The only obstacle to using page caching is that the administrative interface is built into the main site so that admin operations can be performed without leaving the page of interest.

I'm using Apache+mod_rails (Passenger). Is there a way to indicate to Apache that .html files should be ignored when the current user either has a session variable or a cookie named 'admin'*? The session variable need not be evaluated by Apache for validity (since it will be evaluated by Rails in this case).

+1  A: 

Is there a way to indicate to Apache that .html files should be ignored when the current user either has a session variable or a cookie named 'admin'*?

I believe it is not really possible. Even if it is, I guess should be very tricky.

Instead you can use Action Caching. A quote from docs:

One of the issues with page caching is that you cannot use it for pages that require checking code to determine whether the user should be permitted access.

This sounds like exactly your case.


But if you still really need Page Caching via web server, I think you better implement separate pages for admin and non-admin. This is because of one reason. When you enable Page Caching rails doesn't process the request at all and thus there is no way to know if user is authenticated or not.


You probably can overcome this using Dynamic Page Caching. The idea is basically to add the "admin" part from the JavaScript. I don't personally like this a lot though.


One more update: quick search brought me to this article.
The idea is to cache page conditionally and plug mod_rewrite to serve admin pages.

Will work for you but is pretty dirty solution.

Dmytrii Nagirniak
Yes, I know about the other caching schemes that Rails offers, but it is significantly more work to use action or fragment caching rather than the page caching 'catch-all' solution. Thanks, though.
Ron Gejman
Maybe, dynamic page caching would give you a tip. Updated the answer.
Dmytrii Nagirniak
Haha, yeah, I've looked into that before. It's even more complicated and nasty than fragment caching. I'm looking for a system that I can effectively just plugin to the existing code with little modification.
Ron Gejman
Hmm, then you perhaps could do what I (again) added to the answer.
Dmytrii Nagirniak
Exactly what I was looking for. Thank you! Let me make sure it works and then I'll accept the answer (so as not to mislead people who see this Q/A down the line).
Ron Gejman