ruby-on-railsapachepassengermod-railspage-caching

Rails page caching with intra-page administration


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).


Solution

  • 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.