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: "8488c3c7d0283cba90868bc80f039070"
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Sat, 30 Jun 2012 13:23:07 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.415120
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMUoxVHdBWHFqenVuV0JJakVnZjRWRzgxN0x4c2d2R2VhODJZOHVXcjJwRzA9Ig9zZXNzaW9uX2lkIiVmYjY1YTZhOThmMjgzOGNlNTMxNmNmMmI2ZmZiMjIzYSIMY2FydF9pZGkQ--6c501add9e9a162ba97bb1e67240064b7af17f83; 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: "baa512ee6988ddf0cd2398dd0639b8eb"
Last-Modified: Sat, 30 Jun 2012 13:13:01 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Sat, 30 Jun 2012 13:23:08 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.296159
Content-Length: 0
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7CCIQX2NzcmZfdG9rZW4iMS85UTJxTitqN3o0M0xQQlNvWmRpdVFBUW5FMnhsVHRBQmN3YzVRbTR2SFk9Ig9zZXNzaW9uX2lkIiU4OGZmNDgyZWMwMzgzYWVjMzliMzZhYTM4OGY2ZDk5NiIMY2FydF9pZGkR--a5ac37e0d59f005ae04f69ed4470f8bfd6a2d595; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "baa512ee6988ddf0cd2398dd0639b8eb"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "baa512ee6988ddf0cd2398dd0639b8eb"
Last-Modified: Sat, 30 Jun 2012 13:13:01 GMT
Date: Sat, 30 Jun 2012 13:23:09 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.375435
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlMTBjNzZhOWUyMDEzNWRiYWE5ZWM0NTc3MGQ5ZjA5NzgiDGNhcnRfaWRpEw%3D%3D--7e79a84c0bd527cf0c9a06d8daad2170a1e0306c; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Sat, 30 Jun 2012 13:13:01 GMT'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "baa512ee6988ddf0cd2398dd0639b8eb"
Last-Modified: Sat, 30 Jun 2012 13:13:01 GMT
Date: Sat, 30 Jun 2012 13:23:09 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
X-Runtime: 0.225814
Cache-Control: max-age=600, public
Set-Cookie: _depot_session=BAh7ByIPc2Vzc2lvbl9pZCIlOTVkZDQyYzJjYTU2NzEwNTU5MWQ2MDRhODAzZTAyNmEiDGNhcnRfaWRpFA%3D%3D--6db084194e176020c8a13404ea01c1b257e0c35a; 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: "baa512ee6988ddf0cd2398dd0639b8eb"
Last-Modified: Sat, 30 Jun 2012 13:13:01 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
X-Rack-Cache: fresh
X-Content-Digest: 3b640c85629588a75bd8a64e8754da7f62399b90
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
Date: Sat, 30 Jun 2012 13:23:19 GMT
X-Runtime: 1.222352
Content-Length: 6641
Cache-Control: max-age=600, public
Age: 1
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "baa512ee6988ddf0cd2398dd0639b8eb"'
HTTP/1.1 304 Not Modified 
X-Ua-Compatible: IE=Edge
Etag: "baa512ee6988ddf0cd2398dd0639b8eb"
X-Rack-Cache: fresh
X-Content-Digest: 3b640c85629588a75bd8a64e8754da7f62399b90
Server: WEBrick/1.3.1 (Ruby/1.8.7/2011-06-30)
Date: Sat, 30 Jun 2012 13:23:19 GMT
X-Runtime: 1.222352
Cache-Control: max-age=600, public
Age: 1
 

24.3 Active Resources 21.2 Form Helpers