We are in the process of testing the new version of MeinProf.de and crossed a very interesting bug.
We are using fragment caching to speed up the serving of the university homepages. So far this is working great and helps a lot with coping with extra load.
The university homepages are served by unis/show.html.erb:
...
<%= render :partial => 'shared/user_info' %>
...
<% cache "uni_page_homepage_#{@uni.id}", :expires_in => 1.hour do %>
<%= render :partial => 'unis/header' %>
<%= render :partial => 'tabs_header' %>
<div class="uni_desc">
<div class="tab-content">
...
<!-- a lot of uni content here-->
<% end %>
The partial unis/_header.html.erb looks like this:
...
<% content_for("breadcrumb") do %>
<%= uni_breadcrumb(uni, department) %>
<div class="breakcrumb">
...
</div>
<% end %>
<div class="subcolumns">
<h1><%=h uni.name %></h1>
<div class="c08l badge">
<p>
<strong>Homepage:</strong>....
....
</p>
So far so good. When we deployed to our staging host we noticed that the university header was not always right. On the first request it was correct and on every subsequent request the header was missing the breadcrumb. Errors like this sounds like caching issues and it took us a while to figure it out.
The problem is the content_for block inside the partial. Once you think about it, it makes a lot of sense. During the first request the show-template gets evaluated. It calls the partial and the partial executes the content_for block. This block access the template binding/variables to inject content. The partial finishes rendering and stores the rendered content in memcached. Everything fine.
On the next request Rails loads up the action and renders the view. During the view rendering the fragment will be loaded from memcached. No bug here.
So why is the breadcrumb not showing up? Because the content_for block will not be executed. It is a side effect of the partial-rendering and the partial will not be rendered once it is cached. Only the resulting HTML-fragment will be loaded from the cache but the code-block is not executed. This means that there is not call to inject extra content to the main view.
So before you wrap some view code in fragment caching remember: do not fragment cache content_for-blocks. They will not be called. Always check cached views for content_for-blocks and move them outside the cache-call.
