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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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.
This topic is locked and can not be replied to.