# Rails page caching with separately cached global elements

**URL:** <https://www.ruby-forum.com/t/rails-page-caching-with-separately-cached-global-elements/168723>\
**Category:** Rails\
**Created:** [May 25, 2009, 3:50am UTC](https://www.ruby-forum.com/t/rails-page-caching-with-separately-cached-global-elements/168723 "2009-05-25T03:50:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rooster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rooster](https://www.ruby-forum.com/u/rooster)\
**Post date:** [May 25, 2009, 3:50am UTC](https://www.ruby-forum.com/t/rails-page-caching-with-separately-cached-global-elements/168723/1 "2009-05-25T03:50:15Z")

</div>

I’m trying to build an optimized caching setup for my app.

Large parts of the app will use full page caching (using something  
like Ben Scofield’s Progressive Enhancement approach  
[http://www.slideshare.net/bscofield/page-caching-resurrected.](http://www.slideshare.net/bscofield/page-caching-resurrected.))

The issue is: I’d like to include some global elements on all pages,  
where the global element changes much more rapidly that most other  
pages on the site, and I don’t want to expire nearly all the pages on  
the site because a minor global element needs to be updated.

An example of this is “10 most recent blog posts.” shown on every  
page. The RailsEnvy tutorial on caching  
([http://www.railsenvy.com/2007/2/28/rails-caching-tutorial](http://www.railsenvy.com/2007/2/28/rails-caching-tutorial) ) uses  
this example as a case where you _would_ need to clear the whole  
cache, so maybe I’m being a little too creative here.

I was hoping to do this with SSI as apache serves the cached pages, as  
in:

This doesn’t work because:  
1: On the rails side, if page caching is turned off, the include is  
ignored and the element is empty when the page is rendered  
dynamically.

2: If the page is cached, but the element included via ssi doesn’t  
exist on the file system, it results in a routing error.

Google says some people have tried this, but I haven’t seen a howto or  
successful report of this approach. After fumbling around and  
searching for a couple hours, I get the sense that this is trickier  
than it sounds:

- Knowing when the SSI include statement should be processed by rails  
or by apache2
- Getting apache to ssi include a file that doesn’t exist and needs to  
be generated on the fly
- Getting rails to include the ssi content served to the client, by  
not including the ssi for the content cached to the file system

So, I’m looking for alternative approaches or more input on this  
approach. Is something like this even possible now? One thing that may  
make  
this work is if I can get the ssi to only conditionally include the  
file if the file exists.

Something like this:

```
<!--#include virtual="/global_elements/top10.html" -->

```

```
 Non-dynamic text to go here.

```

But SSI doesn’t seem to support file tests or some similar workaround  
that I’ve found

I _could_ (and may) use a javascript layering approach on the client  
side,  
but then the content in the global elements would not be visible to  
spiders used to build search indexes, which is not really desirable.

Thanks for any feedback.

–  
Steven H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
