We'll respond shortly.
This post was originally written by Brian Takita.
Page caching is an easy way to get massive performance and scalability increases with little up front effort.
Of course, when page caching, a number of design changes are necessary. For example, server side session data cannot be used to render data on cached pages.
Rails provides the flash hash to easily render alert messages and errors. In the controller you can write to the flash hash:
flash[:error] = "You cannot go there" # or flash[:notice] = "Welcome to eternity"
and render the flash hash in the view. Typically the rendering happens on a layout:
<div id="error_div_id" class="flash flash_error"><%= flash[:error] %></div> <div id="notice_div_id" class="flash flash_notice"><%= flash[:notice] %></div>
There are some strange quirks with using flash such as needing to use FlashHash#now when rendering the response without redirecting.
Everything works great until you need to page cache the landing page.
For example, lets say you page cache your home page. After logging in, you are redirected to the home page with the flash notice “Logged in successfully”. When page caching, this solution does not work because the request is responded to by the Web Server (i.e. Apache) and does not reach the Rails App server (i.e. Mongrel).
This means the view does not get a chance to render the flash error and notice.
There are a couple of solutions to this problem.
Introducing Cacheable Flash
To solve the problem using cookies on Peer to Patent, we wrote the Cacheable Flash plugin. The plugin allows you to set the flash hash as normal on the controller. It handles converting the flash hash into cookies. All you need to do is include the CacheableFlash module into your controller.
class ApplicationController < ActionController::Base include CacheableFlash # ... end
The Flash.transferFromCookies method:
The Flash.writeDataTo method:
Here is how the life cycle of the login works:
if current_user flash[:notice] = "Welcome to Eternity" end
There is also a side benefit — you don’t have to use FlashHash#now because storing the flash in cookies to be rendered and erased by the client makes this unnecessary.
The Cacheable Flash plugin is on the Pivotal RB project page (http://rubyforge.org/projects/pivotalrb). You can install it by running:
ruby script/plugin install svn://rubyforge.org/var/svn/pivotalrb/cacheable_flash/trunk
It will copy flash.js, cookie.js, and json.js if you do not already have these files.
Happy Page Caching!!
Thanks to Josh Susser for pairing with me on this.