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: "dbc89da02227cce33a88cec8c168f72f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: bc210b1eab59ec9e292e2cc368f85cc6
X-Runtime: 0.300876
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Fri, 25 May 2012 10:23:04 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWYwODdjZDA1OTNhMTgxYzNkYmM0ZjdlMzlkYTk5NTJlBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFzM0xuL1Y2Qy96U3IrTDM1aGo2MGZ1OGxIU3pvRG1VRGwrQkFsdHJ0UmxVPQY7AEY%3D--e94e9b0065847ccd63f86d552480b98e2628b7c2; 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: "6b0d4d942badfd22c90987fae3c95b6b"
Last-Modified: Fri, 25 May 2012 10:11:55 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 3f54dfb6c0889c0089bdf568392cbfe9
X-Runtime: 0.584531
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Fri, 25 May 2012 10:23:05 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTA0MGIzM2Q0Nzk0ODgyZmU2MTNiOTE0Mjg0NGMyNTQ2BjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFCNHc3WVJrL1MxWWhKYm8vYnpRNU0rK1lKUHF0UmFhNm1PdE1vS1NNWXpJPQY7AEY%3D--b5076f9fd6f067b6821f53f91f2fab15501abfb4; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "6b0d4d942badfd22c90987fae3c95b6b"'
HTTP/1.1 304 Not Modified 
Etag: "6b0d4d942badfd22c90987fae3c95b6b"
Last-Modified: Fri, 25 May 2012 10:11:55 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: c2469f1c2352baf7e7106aaa24ec4514
X-Runtime: 0.162572
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Fri, 25 May 2012 10:23:05 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTUxMDAwNGNiMjhhYzBlZDlhYjQ0MGUwM2U4ZDQwNzQ1BjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--32bb40aa6846654d00e7a9a62e128eaab4379b30; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Fri, 25 May 2012 10:11:55 GMT'
HTTP/1.1 304 Not Modified 
Etag: "6b0d4d942badfd22c90987fae3c95b6b"
Last-Modified: Fri, 25 May 2012 10:11:55 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: aa8118d3b7aa4bddeff5f42b3ddd7aa3
X-Runtime: 0.118985
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Fri, 25 May 2012 10:23:05 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTk1YTdjZTFiOWE1ZjBlZGMxMWY5ZGRmNGJkZGYzMWFlBjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--e92775e05cf18dad3f1af06e0c3c21db2859905e; 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: "6b0d4d942badfd22c90987fae3c95b6b"
Last-Modified: Fri, 25 May 2012 10:11:55 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: ad27c0be8293904ed7ae1cb60e1b9714
X-Runtime: 1.612992
Date: Fri, 25 May 2012 10:23:13 GMT
X-Content-Digest: 373aaff55bb2f476c88244b389bfaa61d82084e8
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: "6b0d4d942badfd22c90987fae3c95b6b"'
HTTP/1.1 304 Not Modified 
Etag: "6b0d4d942badfd22c90987fae3c95b6b"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: ad27c0be8293904ed7ae1cb60e1b9714
X-Runtime: 1.612992
Date: Fri, 25 May 2012 10:23:13 GMT
X-Content-Digest: 373aaff55bb2f476c88244b389bfaa61d82084e8
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