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 
Content-Type: text/html; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "6d4b197046a83baeacae1cff8fddf3db"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 568bf5c179f4e0b17637ee7293828a38
X-Runtime: 0.215760
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Sat, 30 Jun 2012 01:24:06 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTE0ZDY2ZTc5NTAyZjUzNTg5MDQwYzBjMDQ1ZjhlOTIzBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFiRithYkhzckptMG0rZGpVVkV5VWZUbzhWQ0NtNWY5RTZsYU90UU1TS3NNPQY7AEY%3D--ad60759427c977b9c7d1e1ecffa5e5f34dc12200; 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 
Etag: "5fff5bddfd026be9dd9d6c36985dfb54"
Last-Modified: Sat, 30 Jun 2012 01:12:38 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 541fd66d05262d2667ed005894fd08fb
X-Runtime: 0.347406
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Sat, 30 Jun 2012 01:24:06 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTg2YTJjZmE2MDJlOTVlOTUwMTExMjYwYTEzNDk0Yzg1BjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFWNm1zZlpSd2xvWDVqRzBUYVc4Yk0vSklwb2t6TE5rUVgydUJ5Uks1bTlNPQY7AEY%3D--6d1654b5cad53bcfdd204e94538c7b410845e2bc; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "5fff5bddfd026be9dd9d6c36985dfb54"'
HTTP/1.1 304 Not Modified 
Etag: "5fff5bddfd026be9dd9d6c36985dfb54"
Last-Modified: Sat, 30 Jun 2012 01:12:38 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 0cba0231e2bb13adb527ad01130babed
X-Runtime: 0.118855
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Sat, 30 Jun 2012 01:24:07 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY1MGEzYTRiMDAxYWY0M2JhNjUwMTQ2NDdkYjFmZTA1BjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--bab8cb8770adb650e6ffdf8c7b3a0e1f162e45c1; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Sat, 30 Jun 2012 01:12:38 GMT'
HTTP/1.1 304 Not Modified 
Etag: "5fff5bddfd026be9dd9d6c36985dfb54"
Last-Modified: Sat, 30 Jun 2012 01:12:38 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 2d7e5da60324b7e1672b73afd05fa9dd
X-Runtime: 0.134722
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Sat, 30 Jun 2012 01:24:07 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTdlODJhMTZhNTJkNmFjZGQ1OWEzMTdkNTRkZjQ4ODA3BjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--ac846f440cbdad615d730f73af78ada889f40a26; 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 
Etag: "5fff5bddfd026be9dd9d6c36985dfb54"
Last-Modified: Sat, 30 Jun 2012 01:12:38 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 423167f0b0c28e0caacffbe45d7e9eac
X-Runtime: 1.634247
Date: Sat, 30 Jun 2012 01:24:14 GMT
X-Content-Digest: 6b024aeade8876f21c72ed2227494d4c90d13d7b
Content-Length: 6641
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Connection: Keep-Alive
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "5fff5bddfd026be9dd9d6c36985dfb54"'
HTTP/1.1 304 Not Modified 
Etag: "5fff5bddfd026be9dd9d6c36985dfb54"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 423167f0b0c28e0caacffbe45d7e9eac
X-Runtime: 1.634247
Date: Sat, 30 Jun 2012 01:24:14 GMT
X-Content-Digest: 6b024aeade8876f21c72ed2227494d4c90d13d7b
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Connection: close
 

24.3 Active Resources 21.2 Form Helpers