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 
X-Ua-Compatible: IE=Edge
Etag: "d07ce5afdc6689f3cd1d7bcc907abb04"
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Fri, 25 May 2012 04:20:55 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.409272
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMXpEK0hBS1VuSXVFUGVWdThnZjJNbVFjMG1FYTBHVUNuSkIza0lhekVORDg9Ig9zZXNzaW9uX2lkIiVlMzdmNzUzZDcyYTAwNDY5MDBhNDdjOGJlNjE3MTZlMiIMY2FydF9pZGkQ--db4c87dfeefbdd3fcbc1d14158a96e2827e39114; 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 
X-Ua-Compatible: IE=Edge
Etag: "9c33c1dc0e4fc92fdf667d1ac3be7db7"
Last-Modified: Fri, 25 May 2012 04:11:30 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Fri, 25 May 2012 04:20:56 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.276837
Content-Length: 0
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMW9mMkJ5SjZSeDR5dnphWVVqTkxlcmIrV3ZtT3hUYmplTGtIeEpzcWhrZ2s9Ig9zZXNzaW9uX2lkIiU1MjlkMmUzNDE1ZjRjODU2MzdiMWVjMDM1NmRmM2MzMSIMY2FydF9pZGkR--9c1499886618c0050319e0497bf07172a30fba8f; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "9c33c1dc0e4fc92fdf667d1ac3be7db7"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "9c33c1dc0e4fc92fdf667d1ac3be7db7"
Last-Modified: Fri, 25 May 2012 04:11:30 GMT
Date: Fri, 25 May 2012 04:20:56 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.270070
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlYjIzZjc4NGMyMWNjZWEzYWNjOTZlYzY4NzYzMjUyMTciDGNhcnRfaWRpEw%3D%3D--48fe31d949c6f49708ec9a494bee03772b8158a2; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Fri, 25 May 2012 04:11:30 GMT'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "9c33c1dc0e4fc92fdf667d1ac3be7db7"
Last-Modified: Fri, 25 May 2012 04:11:30 GMT
Date: Fri, 25 May 2012 04:20:56 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.176556
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlYWU0ZTIyODM0NThmNzkxNjY0NTNhNDg5MmY5OTA3NzAiDGNhcnRfaWRpFA%3D%3D--9dff38789a954b14e9cd4484f5c7d24e9a217b82; 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 
X-Ua-Compatible: IE=Edge
Etag: "9c33c1dc0e4fc92fdf667d1ac3be7db7"
Last-Modified: Fri, 25 May 2012 04:11:30 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
X-Rack-Cache: fresh
X-Content-Digest: f5a59c9a31f65879acda54ba034c7f329cc1d014
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
Date: Fri, 25 May 2012 04:21:07 GMT
X-Runtime: 1.255984
Content-Length: 6641
Cache-Control: max-age=600, public
Age: 0
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "9c33c1dc0e4fc92fdf667d1ac3be7db7"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "9c33c1dc0e4fc92fdf667d1ac3be7db7"
X-Rack-Cache: fresh
X-Content-Digest: f5a59c9a31f65879acda54ba034c7f329cc1d014
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
Date: Fri, 25 May 2012 04:21:07 GMT
X-Runtime: 1.255984
Cache-Control: max-age=600, public
Age: 0
 

24.3 Active Resources 21.2 Form Helpers