When a request is processed in Rails 4, it creates a MD5 hash of the entire page and include the digest in the headers.
[cc lang=”ruby”]
header[‘Etag’] = Digest::MD5.hexdigest(body)
[/cc]

In following requests, it will compare generated Etag with previously generated ones, and if a matching is found, it will respond with 304 HTTP response(Not modified). The page will be retrieved from browser’s cache history and will avoid sending the full response again to client side.

We can use the fresh_when method and pass an array of items to create more complex cachings. For example, based on current user and his age.
[cc lang=”ruby”]
class ProfilesController < ApplicationController def show @profile = Profile.find(params[:id]) fresh_when([@profile, curernt_user.id, current_user.age]) end end [/cc] But sometimes, we want to reuse part of this array and don’t have to repeat it every time. In these cases, we can extract some tags and apply to everything. [cc lang="ruby"] class ProfilesController < ApplicationController etag { current_user.id } etag { current_user.age } def show @profile = Profile.find(params[:id]) fresh_when([@profile]) end end [/cc]