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: "4a3d158bcfbe93a84cb7c9e3933b0ca8"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: be6e50ec502ed34531bb6e4ee730bb61
X-Runtime: 0.309125
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Sat, 29 Oct 2011 07:27:11 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTM5MGQzZjBmOGU0YWIzMTAzMzJlMWQ5ZTZhNjQ4MGZiBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFKcERWRlBnL2wydGYwQmRFRk1WS0xPQXBvb3lrdENiTlRZK2VWdXBKSnc4PQY7AEY%3D--6430417467a5f191d544f547de0eda120b41a102; 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: "522e835c50636fb6758a30d3e1365659"
Last-Modified: Sat, 29 Oct 2011 07:11:56 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 47962d990a5a3c0ce7114393e20bdddc
X-Runtime: 0.287773
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Sat, 29 Oct 2011 07:27:11 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTMyN2VhODU4MWMyZDNkMWIyZWMyMDIxOWVmNzhlYjgwBjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFzVnI3VG9nSUhYdDNnTndPTk5hWitZbUxDblVvai9jbHdDdEZ4b0FOaXhvPQY7AEY%3D--7ad6ee756ec7245972131120cd04ac86b93c19da; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "522e835c50636fb6758a30d3e1365659"'
HTTP/1.1 304 Not Modified 
Etag: "522e835c50636fb6758a30d3e1365659"
Last-Modified: Sat, 29 Oct 2011 07:11:56 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: c3412e631947bb1273dcdaa30e732372
X-Runtime: 0.161221
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Sat, 29 Oct 2011 07:27:12 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTA2ODZmOTgyMmU2MjIwMmYyYmVlZjM1MzExNjc3YjNlBjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--aef1809e9dc2b3cdca4f39a15688bb6664c58bdc; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Sat, 29 Oct 2011 07:11:56 GMT'
HTTP/1.1 304 Not Modified 
Etag: "522e835c50636fb6758a30d3e1365659"
Last-Modified: Sat, 29 Oct 2011 07:11:56 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 6432703cde71811314a933b64c919763
X-Runtime: 0.251877
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Sat, 29 Oct 2011 07:27:12 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTlkNWMwZTk0OWZiNDFmMjFjZjgzM2ViYTgwZjNjZDY4BjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--6433e51b888aa0034d4bb98111f2fa0dc19c9fe0; 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 ActiveRecord models
  config.active_record.mass_assignment_sanitizer = :strict
 
  # 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: "522e835c50636fb6758a30d3e1365659"
Last-Modified: Sat, 29 Oct 2011 07:11:56 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: fa99804831d9f9bd301ea145beb4a7e7
X-Runtime: 2.360872
Date: Sat, 29 Oct 2011 07:27:23 GMT
X-Content-Digest: 5828e61c2370d1b121a888ec12920f704a85be2b
Content-Length: 6639
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWMzYTUxYzA2Mjk1Mjc1NDI0NWJhOWY4YjQ3ZDMwMWE0BjsAVEkiDGNhcnRfaWQGOwBGaRVJIhBfY3NyZl90b2tlbgY7AEZJIjFxV0MvcnRWazR1NEFTOWRpVnpOWTRFN1Q5WE1pTFc2SHM0Q1U4Z2s0UkJvPQY7AEY%3D--f73e9b223013c6b1104ee33faa030755e9ebd82e; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "522e835c50636fb6758a30d3e1365659"'
HTTP/1.1 304 Not Modified 
Etag: "522e835c50636fb6758a30d3e1365659"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: fa99804831d9f9bd301ea145beb4a7e7
X-Runtime: 2.360872
Date: Sat, 29 Oct 2011 07:27:23 GMT
X-Content-Digest: 5828e61c2370d1b121a888ec12920f704a85be2b
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWMzYTUxYzA2Mjk1Mjc1NDI0NWJhOWY4YjQ3ZDMwMWE0BjsAVEkiDGNhcnRfaWQGOwBGaRVJIhBfY3NyZl90b2tlbgY7AEZJIjFxV0MvcnRWazR1NEFTOWRpVnpOWTRFN1Q5WE1pTFc2SHM0Q1U4Z2s0UkJvPQY7AEY%3D--f73e9b223013c6b1104ee33faa030755e9ebd82e; path=/; HttpOnly
 

24.3 Active Resources 21.2 Form Helpers