Skip to content
A ruby on rails (>= 2.1) plugin that extends the cache store with an in memory cache to enhance caching performance and solves a race conditions. (replaces extended_fragment_cache)
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



This plugin extends any cache store configured in rails with a process local in memory cache. It is intended to be a Rails 2.2 replacement for the extended_fragment_cache plugin ( which does essentially the same but doesn't work with the new caching infrastructure in Rails >= 2.1.

Adding a local cache to the usual remote cache (memcache, file etc.) has two major advantages over the default behaviour (for fragment caching):

* you usually need two cache requests per http request: one in the controller to determine if the cached fragment is still there, one in the view where the fragment is actually rendered - that means the number of cache roundtrips get cut in half
* the following scenario is avoided: request comes into controller, controller fetches fragment from remote cache, does not assign certain variables as the cache is primed, cache is expired by another process, view gets rendered, cache fragment is deleted - boom


script/plugin install git://

the plugin automatically replaces the current cache storage with a proxy that handles the local caching and delegates the rest to the original storage.


The plugin provides a method in your controller called when_fragment_expired, which takes a key name and a block:

class MyController
  def index
    when_fragment_expired 'my_index', 60.minutes do
      @data = Data.find :all

The code in the block only gets executed when the cache returns no data for the given key. In the view you do the normal

<%- cache 'my_index' do -%>
  <%- @data.each do |data| -%>
    <%= data.content %>
  <%- end -%>
<%- end -%>

This loads the fragment from the local cache and renders it. If the cache returns nil the block gets executed, written to the cache and rendered as ususal in rails. Before every request, the local cache is cleared so no stale data gets into your application.

By the way with memcache you can expire your cache fragments by time like this: (this is not part of this plugin but the default behavior)

<%- cache 'my_index', 60.minutes do -%>
  <%- @data.each do |data| -%>
    <%= data.content %>
  <%- end -%>
<%- end -%>

This will expire the fragment after 60 minutes.


Copyright (c) 2008 Alexander Lang, Thilo Utke, released under the MIT license


email: alex[at], twitter: langalex, blog:, skype: langalex
Something went wrong with that request. Please try again.