Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

24.3 Active Resources 21.2 Form Helpers

22 Caching

Restart the server.

curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
X-Ua-Compatible: IE=Edge
Etag: "7d0ecd9a70241cf4c9e7561ebfcb097c"
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Fri, 25 May 2012 01:20:07 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: e2ee2e5196b53d7c5b4922ff02311596
X-Runtime: 0.341941
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMTlNc1dyR2ZlMEN2MEFUTkV0UmhRQnRtdSt6M3FCZFdiTDF6ZHV4QnNXak09Ig9zZXNzaW9uX2lkIiU0MTc3ODc0NTE2ZmIyMTExZTVlYTBmMTE3NjBhNDA1OCIMY2FydF9pZGkQ--43aabb5fc69b35e20b67344b1ffec85e38b812a6; path=/; HttpOnly
 

add a method to return the latest product

edit app/models/product.rb

set ETAG and LastModified headers on the response

edit app/controllers/store_controller.rb
 
class StoreController < ApplicationController
  skip_before_filter :authorize
  def index
    if params[:set_locale]
      redirect_to store_path(:locale => params[:set_locale])
    else
      @products = Product.order(:title)
      @cart = current_cart
    end
 
    latest = Product.latest
    fresh_when :etag => latest, :last_modified => latest.created_at.utc
    expires_in 10.minutes, :public => true
  end
end
curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
X-Ua-Compatible: IE=Edge
Etag: "b25c74035e32103a89a624a4ae95d215"
Last-Modified: Fri, 25 May 2012 01:11:21 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Fri, 25 May 2012 01:20:08 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: aa627edf905898be300a8f6b009001c1
X-Runtime: 0.378531
Content-Length: 0
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMWhoSDFTaEZCSUdDNFBwMUloOHJRMEphaGdZNndNQWUxcHlhWmplcUU4SGs9Ig9zZXNzaW9uX2lkIiUxODE4ZTllZjI0ZDU0NTI2M2NkNGE2NTI5N2U0YmJkMyIMY2FydF9pZGkR--47ab47c3aa748f7ab98d0bd7a58b905071714827; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "b25c74035e32103a89a624a4ae95d215"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "b25c74035e32103a89a624a4ae95d215"
Last-Modified: Fri, 25 May 2012 01:11:21 GMT
Date: Fri, 25 May 2012 01:20:08 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 5a85310c665c797f2a9bd915faedb7e4
X-Runtime: 0.112129
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlMWIzNzU2YWVkNDBmY2M2ZjcwNzRiZTM0MWFlYWRhOGMiDGNhcnRfaWRpEw%3D%3D--55e7e328e5ee8cb66934ac59a06f0d6f06a8fad1; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Fri, 25 May 2012 01:11:21 GMT'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "b25c74035e32103a89a624a4ae95d215"
Last-Modified: Fri, 25 May 2012 01:11:21 GMT
Date: Fri, 25 May 2012 01:20:08 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 60846a60ef5eec3350967c1a5c84774e
X-Runtime: 0.113239
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlNjY3Mzc5NDlkYmI3MzRmMzcwNTdhNDg2NGM3ZjhmNDIiDGNhcnRfaWRpFA%3D%3D--995cb9998203eec1cb9449b6bb073320f9691de0; path=/; HttpOnly
 

Turn on caching in development

edit config/environments/development.rb
Depot::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb
 
  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false
 
  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true
 
  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = true
 
  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false
 
  # Don't actually send emails
  config.action_mailer.delivery_method = :test
  #
  # Alternate configuration example, using gmail:
  #   config.action_mailer.delivery_method = :smtp
  #   config.action_mailer.smtp_settings = {
  #     address:        "smtp.gmail.com",
  #     port:           587, 
  #     domain:         "domain.of.sender.net",
  #     authentication: "plain",
  #     user_name:      "dave",
  #     password:       "secret",
  #     enable_starttls_auto: true
  #   } 
 
  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log
 
  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin
 
  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict
 
  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5
 
  # Do not compress assets
  config.assets.compress = false
 
  # Expands the lines which load the assets
  config.assets.debug = true
end

Restart the server.

curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
X-Ua-Compatible: IE=Edge
Etag: "b25c74035e32103a89a624a4ae95d215"
Last-Modified: Fri, 25 May 2012 01:11:21 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
X-Rack-Cache: fresh
X-Content-Digest: b0b24a50986ba8a96596730b2e18fb0cb7c9abf1
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 7d623335f7b43da113493976d81681a9
Date: Fri, 25 May 2012 01:20:18 GMT
X-Runtime: 1.077670
Content-Length: 6641
Cache-Control: max-age=600, public
Age: 0
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "b25c74035e32103a89a624a4ae95d215"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "b25c74035e32103a89a624a4ae95d215"
X-Rack-Cache: fresh
X-Content-Digest: b0b24a50986ba8a96596730b2e18fb0cb7c9abf1
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 7d623335f7b43da113493976d81681a9
Date: Fri, 25 May 2012 01:20:18 GMT
X-Runtime: 1.077670
Cache-Control: max-age=600, public
Age: 0
 

24.3 Active Resources 21.2 Form Helpers