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: "72ef9bf6a079e579a329c66a6f6fb46b"
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Oct 2012 07:21:10 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 4ee34ee7ee8b5a14a959430e85e1d75c
X-Runtime: 0.236571
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMUo2RDlLMUxlY1JITUtoWTk2QmZja29BR2RaRm9HeGQ0ZXRPYjdSZ1IxY3M9Ig9zZXNzaW9uX2lkIiUzYzllZWY3Zjg5ZTc2MzFmYjgzMmZjOTY0MmY3NTMzMSIMY2FydF9pZGkR--addd613b0fec41b106e79384c4b0da4f1df02a8e; 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: "c67882c13ea7ece3ef83ed06e4459f19"
Last-Modified: Thu, 11 Oct 2012 07:11:35 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Oct 2012 07:21:10 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 8189622a02c507e26e5bc7470eb38a23
X-Runtime: 0.382453
Content-Length: 0
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMU5DUDhTbVVjVWVlR3VBcTFkT0w5YVNYeGpGMTNkaHVuWjZHWW5KbUdNbVE9Ig9zZXNzaW9uX2lkIiUxMzNiNTRjYzI2YzAxMzU0MTdiZDIwYzJhNTU0YWNiZCIMY2FydF9pZGkS--413e18b88e6dc28e7855d281147e38d1a39facc0; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "c67882c13ea7ece3ef83ed06e4459f19"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "c67882c13ea7ece3ef83ed06e4459f19"
Last-Modified: Thu, 11 Oct 2012 07:11:35 GMT
Date: Thu, 11 Oct 2012 07:21:10 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 848a39e22aafec2b97fcf3f264215507
X-Runtime: 0.097101
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlODViM2ZiNzZhNGNkNGM4NThlN2Y3MzQ5NDRjYzVhZjkiDGNhcnRfaWRpFA%3D%3D--3c8b72237697656b5d86dd1039edd8ff42afed87; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Thu, 11 Oct 2012 07:11:35 GMT'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "c67882c13ea7ece3ef83ed06e4459f19"
Last-Modified: Thu, 11 Oct 2012 07:11:35 GMT
Date: Thu, 11 Oct 2012 07:21:11 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: f1445781b7063d89a7d782d80cac60b8
X-Runtime: 0.105718
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlOGU3NmVmYWM5NWVhYjE0MjU0NGM1N2IxZTdhMWI5OTgiDGNhcnRfaWRpFQ%3D%3D--cb6374311e188843a4ab4bb7edf710610a835b90; 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: "c67882c13ea7ece3ef83ed06e4459f19"
Last-Modified: Thu, 11 Oct 2012 07:11:35 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
X-Rack-Cache: fresh
X-Content-Digest: 2d0a82570359aa2c3774e9f7d23dc7a80eabc429
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 081f2b0aac20dc096588175c2af2ed98
Date: Thu, 11 Oct 2012 07:21:20 GMT
X-Runtime: 1.165782
Content-Length: 6611
Cache-Control: max-age=600, public
Age: 0
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "c67882c13ea7ece3ef83ed06e4459f19"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "c67882c13ea7ece3ef83ed06e4459f19"
X-Rack-Cache: fresh
X-Content-Digest: 2d0a82570359aa2c3774e9f7d23dc7a80eabc429
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Request-Id: 081f2b0aac20dc096588175c2af2ed98
Date: Thu, 11 Oct 2012 07:21:20 GMT
X-Runtime: 1.165782
Cache-Control: max-age=600, public
Age: 0
 

24.3 Active Resources 21.2 Form Helpers