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: "b1ef9fe3e46e5b5f3a2f096ad4ce926a"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 50317a1d-d996-4b2f-b6ae-54fed3fc9ecc
X-Runtime: 0.278755
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Date: Fri, 29 Jun 2012 18:56:17 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTA5OWU2YzYyNGZjZjIwNTdjMDkzZGIwYzNjMzkzNmI4BjsAVEkiDGNhcnRfaWQGOwBGaQ9JIhBfY3NyZl90b2tlbgY7AEZJIjFLT0ZpbXBBaVZ6Y3VxUkJ5RkxKa1UyQzltdWhiWXpUWmt3a1VrRmtKOVI4PQY7AEY%3D--37621914e5bdd5cbaf3f5a7b13b8a1ee2f636225; 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: "a802ed315265042dba7dccb7d2eea2a9"
Last-Modified: Fri, 29 Jun 2012 18:39:08 GMT
Date: Fri, 29 Jun 2012 18:56:18 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 263c923a-a826-4e9d-8064-4efe15f8c9df
X-Runtime: 0.280337
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTEzYWEzYzI4MTg2YTQ1NzViY2RmYzNmNmZkYTgwYTNmBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFBSEJIUmQ3eDNEL1Iwc3RNUjA2SXhMYlZEcmNRYW5jZ3pmZDY1RmdaaHJjPQY7AEY%3D--6785d8956994632294a797ad0c434f34a64d3869; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "a802ed315265042dba7dccb7d2eea2a9"'
HTTP/1.1 304 Not Modified 
Etag: "a802ed315265042dba7dccb7d2eea2a9"
Last-Modified: Fri, 29 Jun 2012 18:39:08 GMT
Date: Fri, 29 Jun 2012 18:56:18 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: d3348a09-6d0c-453d-9ea5-4683a5afb112
X-Runtime: 0.132924
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTUwYTE4ZmNmNjNiMGY3MWY2OWMwMGIzYjI1ZDQ0ZWU3BjsAVEkiDGNhcnRfaWQGOwBGaRI%3D--7485435b22d8fc9bcb7e0dd0d0f03bf8a0c5c644; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Fri, 29 Jun 2012 18:39:08 GMT'
HTTP/1.1 304 Not Modified 
Etag: "a802ed315265042dba7dccb7d2eea2a9"
Last-Modified: Fri, 29 Jun 2012 18:39:08 GMT
Date: Fri, 29 Jun 2012 18:56:18 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: fc3e519b-d280-453d-98ec-b212a9bc682c
X-Runtime: 0.123977
Server: WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTIzOWYxZWFlMjNhODFmNDRiNmNmNDQ0NjJjMzMxZWJmBjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--3d64f590e541612d86384f42845966b8b44cd4aa; 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
 
  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load
 
  # Do not compress assets.
  config.assets.compress = false
 
  # Expands the lines which load the assets.
  config.assets.debug = true
 
  # In development, use an in-memory queue for queueing
  config.queue = Rails::Queueing::Queue
end

Restart the server.

curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
Etag: "a802ed315265042dba7dccb7d2eea2a9"
Last-Modified: Fri, 29 Jun 2012 18:39:08 GMT
Date: Fri, 29 Jun 2012 18:56:26 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 41bf43e4-c2aa-410c-91b5-bd541fbf2b6d
X-Runtime: 1.642364
X-Content-Digest: 07f49b327af3b4251bb12ecc3cecc0e9c0e3bb13
Content-Length: 6182
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: "a802ed315265042dba7dccb7d2eea2a9"'
HTTP/1.1 304 Not Modified 
Etag: "a802ed315265042dba7dccb7d2eea2a9"
Date: Fri, 29 Jun 2012 18:56:26 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Request-Id: 41bf43e4-c2aa-410c-91b5-bd541fbf2b6d
X-Runtime: 1.642364
X-Content-Digest: 07f49b327af3b4251bb12ecc3cecc0e9c0e3bb13
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