Forum: Ruby on Rails Semi-dynamic page and action cache

Ae59947a383b0cb287069a38e09369aa?d=identicon&s=25 sdillard (Guest)
on 2011-02-27 22:11
(Received via mailing list)
I am trying to figure out the best way to handle caching in a fairly
unique scenario.  I've read through the various caching posts, but
nothing really matches what I am trying to do.

Let's assume there are two models, company and site, and each has a
string (short_name) that will be used for routing.  So we can do
either of the following:
http(s)://{company.short_name}.{mydomain}.com/{site.short_name}, or
http(s)://{mydomain}.com/{company.short_name}/{site.short_name}

Both of these are mapped to a single controller and action that
figures out what to render based on the various attributes in the
models.  But, once it renders, I'd like to cache the content if the
site is set to use caching.  I'd like to cache the action rather than
the site if the site requires authentication.  And in some cases, no
caching should be used.  When I try to do this in my controller
action, a page gets cached, but it's empty and it isn't used.  If I do
this after I render the view, the page does get written to the cache,
but then rails is not picking it up and still performs all the logic
then re-writes to the cache.  Is there any way around this?

    if @site.cache_page?
        unless @site.requires_authentication?
          cache_page
        else
          cache_action
        end
      end

I realize that normally one would cache the action in the class def,
but I can't really do that, since I don't want to always return the
cached page anytime that action is called in the controller, and I
don't want to have to have a controller and action for every site.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.