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: "b6bd24cc7f1105fbffccc51197eecf91"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 398cf596-37bd-4e40-881d-8e0ad464fe72
X-Runtime: 0.203029
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Date: Sun, 12 Feb 2012 17:36:41 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTU2Mzk5ZTQyODVkNmNjNGY5MzFhMThlNTIzMGUwYTdiBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFBdndOMW1KUlRhejBmVkdnOUl2bXNhUzJ0bFJxNDUrRVQ2MTNLbEl3cTZRPQY7AEY%3D--988823095a96e736dad72f56bb5419162062fa2e; 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: "ffb94568f1d6063e921fe420df5ea062"
Last-Modified: Sun, 12 Feb 2012 17:25:07 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 6ab44d96-2811-48cf-af55-9269b4d88990
X-Runtime: 1.255764
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Date: Sun, 12 Feb 2012 17:36:43 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTM0MDc3MTkwNDFhYjZiMWZlZDA1ZTg2MzQ1YTk5NWRiBjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFESk9KcGMwYWkwbEkweXVIQ0QyRVBaVzkzeWRXL3FHWGxxeWpTS0F2SFJJPQY7AEY%3D--60a4786a52568a54eb29df9b3dab1ffeae505c5f; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "ffb94568f1d6063e921fe420df5ea062"'
HTTP/1.1 304 Not Modified 
Etag: "ffb94568f1d6063e921fe420df5ea062"
Last-Modified: Sun, 12 Feb 2012 17:25:07 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 85326587-24cd-4195-b41d-f98cafd506b9
X-Runtime: 0.135205
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Date: Sun, 12 Feb 2012 17:36:43 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWI1OWNiMjk5MmI0NzE2MjUyYTA5NTc2MTg2ZTQ2Njc4BjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--a01098f7d87a061fb6e7a1d03cfeeadb3dedac55; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Sun, 12 Feb 2012 17:25:07 GMT'
HTTP/1.1 304 Not Modified 
Etag: "ffb94568f1d6063e921fe420df5ea062"
Last-Modified: Sun, 12 Feb 2012 17:25:07 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: d3865fb0-432a-41dd-9a9d-9cbca56f2984
X-Runtime: 0.131515
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Date: Sun, 12 Feb 2012 17:36:43 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTliZmMxZDI2ZWNmNTJmNmRjMjA4MzIxMDA4NDUyM2NkBjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--8fa735ece6753fb3f07dee7f5e6ec9202cc665a9; 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
 
  # 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: "ffb94568f1d6063e921fe420df5ea062"
Last-Modified: Sun, 12 Feb 2012 17:25:07 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 7e1ca924-4cb1-4206-830f-e353cb28ff6b
X-Runtime: 0.643567
Date: Sun, 12 Feb 2012 17:36:50 GMT
X-Content-Digest: 32b0a5d3e91dfd12d9fa5a119ca027a520f6c12e
Content-Length: 6641
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWM5MWVlZTQ4ZTY3YWI1MGY3NjFhZjQ0ZGE2ZTFkZGFiBjsAVEkiDGNhcnRfaWQGOwBGaRVJIhBfY3NyZl90b2tlbgY7AEZJIjEvWVUweHpPdjZCZmdTTUQ1YThQaFhRMTlSUFBYWVlLVWNSbTgrYUFkc2FJPQY7AEY%3D--821abe780e27a4770f9a622b1e43da0c3e22bcbc; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "ffb94568f1d6063e921fe420df5ea062"'
HTTP/1.1 304 Not Modified 
Etag: "ffb94568f1d6063e921fe420df5ea062"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 7e1ca924-4cb1-4206-830f-e353cb28ff6b
X-Runtime: 0.643567
Date: Sun, 12 Feb 2012 17:36:50 GMT
X-Content-Digest: 32b0a5d3e91dfd12d9fa5a119ca027a520f6c12e
Age: 0
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/2.0.0/2012-02-12)
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWM5MWVlZTQ4ZTY3YWI1MGY3NjFhZjQ0ZGE2ZTFkZGFiBjsAVEkiDGNhcnRfaWQGOwBGaRVJIhBfY3NyZl90b2tlbgY7AEZJIjEvWVUweHpPdjZCZmdTTUQ1YThQaFhRMTlSUFBYWVlLVWNSbTgrYUFkc2FJPQY7AEY%3D--821abe780e27a4770f9a622b1e43da0c3e22bcbc; path=/; HttpOnly
 

24.3 Active Resources 21.2 Form Helpers