etag snippets

How to support conditional gets in Rails

Tagged caching, rails, last_modified, etag, fresh  Languages ruby

From the When to Tell Your Kids About Client Caching RailsConf presentation (PDF)

def fresh?(response)
def stale?(:etag => @object, :last_modified => updated_at.utc)
def not_modified?(modified_at)
def etag_matches?(etag)
class PeopleController < ApplicationController
    def show
        @person = Person.find(params[:id])
        response.last_modified = @person.updated_at.utc
        response.etag = @person 
        return head(:not_modified) if request.fresh?(response)

        respond_to do |wants|
            #...
        end
    end
end
response.etag = @person # => “5cb44721b6ce18857ff6900486dc4aba”
@person.cache_key # => "people/5-20071224150000"
class PeopleController < ApplicationController
    def show
        @person = Person.find(params[:id])
        if stale?(:etag => @person, :last_modified => @person.updated_at.utc)
            respond_to do |wants|
            #...
            end
        end
    end
end