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: "5d8805f38529889f7cbb9be2d37f8824"
Cache-Control: max-age=0, private, must-revalidate
X-Runtime: 0.335481
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Fri, 25 May 2012 16:30:22 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTM4MGZiOGE0OTc3N2IwOTk0NGEyMjZiMzliZDUwM2FiBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFraVlwVGlpUUcvcDBTZjhobXdBc016NVJNOVVyS3RGeDFiOHVoMThzcE9NPQY7AEY%3D--b752f58381d5b12e78ceecd1d23dfbcf64db9fe7; 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: "662820ad99365ede95aef5527f315e42"
Last-Modified: Fri, 25 May 2012 16:12:21 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.315978
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Fri, 25 May 2012 16:30:22 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTZiYTk2ODdjZTZlYzYwNGVlNDVhMDZhMjQwZWU2ZWFlBjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFRYVhwUE5qbWRwQktXM3ppczFva0xFSGV1MHNYUHc0WFFCNjQvYkszWm5zPQY7AEY%3D--1007dad426984796fcf59b71932231db7a501674; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "662820ad99365ede95aef5527f315e42"'
HTTP/1.1 304 Not Modified 
Etag: "662820ad99365ede95aef5527f315e42"
Last-Modified: Fri, 25 May 2012 16:12:21 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.255340
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Fri, 25 May 2012 16:30:23 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWZmOTk4YWZmMzUxMTA0MDlkMjMwYzc0MTQ5OGU2ZjcwBjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--0cc522f58be5993563041ac0727623e2d7066114; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Fri, 25 May 2012 16:12:21 GMT'
HTTP/1.1 304 Not Modified 
Etag: "662820ad99365ede95aef5527f315e42"
Last-Modified: Fri, 25 May 2012 16:12:21 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.252049
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Fri, 25 May 2012 16:30:23 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWYxMDc1ZWUwMjgxNWYzODcwNWU5M2MzZmM0NTM1NWJhBjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--72411c952642b03488e4a8d7c2ede1e3dd0e8518; 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
 
  # 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: "662820ad99365ede95aef5527f315e42"
Last-Modified: Fri, 25 May 2012 16:12:21 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 2.504903
Date: Fri, 25 May 2012 16:30:35 GMT
X-Content-Digest: d7e191569490b3ead069daeb133627076880fac6
Content-Length: 6641
Age: 1
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Connection: Keep-Alive
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "662820ad99365ede95aef5527f315e42"'
HTTP/1.1 304 Not Modified 
Etag: "662820ad99365ede95aef5527f315e42"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 2.504903
Date: Fri, 25 May 2012 16:30:35 GMT
X-Content-Digest: d7e191569490b3ead069daeb133627076880fac6
Age: 1
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
 

24.3 Active Resources 21.2 Form Helpers