Module: ActionView

Defined in:
lib/extended_fragment_cache.rb

Overview

Content Interpolation for Fragment Caching

Many modern websites mix a lot of static and dynamic content. The more dynamic content you have in your site, the harder it becomes to implement caching. In an effort to scale, you’ve implemented fragment caching all over the place. Fragment caching can be difficult if your static content is interleaved with your dynamic content. Your views become littered with cache calls which not only hurts performance (multiple calls to the cache backend), it also makes them harder to read. Content interpolation allows you substitude dynamic content into cached fragment.

Take this example view: <% cache(“/first_part”) do %>

This content is very expensive to generate, so let's fragment cache it.<br/>

<% end %> <%= Time.now %><br/> <% cache(“/second_part”) do %>

This content is also very expensive to generate.<br/>

<% end %>

We can replace it with: <% cache(“/only_part”, {}, => Time.now) do %>

This content is very expensive to generate, so let's fragment cache it.<br/>
__TIME_GOES_HERE__<br/>
This content is also very expensive to generate.<br/>

<% end %>

The latter is easier to read and induces less load on the cache backend.

We use content interpolation at Zvents to speed up our JSON methods. Converting objects to JSON representation is notoriously slow.

Unfortunately, in our application, each JSON request must return some unique data. This makes caching tedious because 99% of the content returned is static for a given object, but there’s a little bit of dynamic data that must be sent back in the response. Using content interpolation, we cache the object in JSON format and substitue the dynamic values in the view.

This plugin integrates Yan Pritzker’s extension that allows content to be cached with an expiry time (from the memcache_fragments plugin) since they both operate on the same method. This allows you to do things like:

<% cache(“/only_part”, => 15.minutes) do %>

This content is very expensive to generate, so let's fragment cache it.

<% end %>

Defined Under Namespace

Modules: Helpers