Agile Web Development with Rails, Edition 4

24.3 Active Resources 21.2 Form Helpers

22 Caching

Restart the server.

curl --silent --dump - --output /dev/null http://localhost:3000/
HTTP/1.1 200 OK 
Content-Type: text/html; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "7375f8465c374cc3564aa9a5c242a7ae"
Cache-Control: max-age=0, private, must-revalidate
X-Runtime: 0.276303
Content-Length: 6611
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
Date: Sun, 09 Dec 2012 05:32:00 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTE2YTE2ODVmZmM2N2ZmMjFjMmM1MjY1MzE5ZGMyOGViBjsAVEkiDGNhcnRfaWQGOwBGaRBJIhBfY3NyZl90b2tlbgY7AEZJIjFOMTJIK0RqSmxML2UwTU1jWGM0VHZFdllyTENyZVlCS1g5NUNqZy9ZeHNBPQY7AEY%3D--1d32d40211c0013df2c86434fe171ddc7cb5f7d8; path=/; HttpOnly
 

add a method to return the latest product

edit app/models/product.rb
  def self.latest
    Product.order('updated_at desc').limit(1).first
  end

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 --dump - --output /dev/null http://localhost:3000/
HTTP/1.1 200 OK 
Etag: "3e06c3071102f4f6f833d69470259b7f"
Last-Modified: Sun, 09 Dec 2012 05:12:50 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.359012
Content-Length: 6611
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
Date: Sun, 09 Dec 2012 05:32:01 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJWUyMWFjOTI1NmY0MGM2ZmE3YTYxNmVhZmI4MDdhYzQ2BjsAVEkiDGNhcnRfaWQGOwBGaRFJIhBfY3NyZl90b2tlbgY7AEZJIjFxNHNheXNNc0pkWmZ5NCtBVjU5TGFNbHE4dmM0YUg3Zlk4bDVLQlNsOGdVPQY7AEY%3D--48028f6cc2fa619e6c535c4ef8d7f66058201ffb; path=/; HttpOnly
 
curl --silent --dump - --output /dev/null http://localhost:3000/ -H 'If-None-Match: "3e06c3071102f4f6f833d69470259b7f"'
HTTP/1.1 304 Not Modified 
Etag: "3e06c3071102f4f6f833d69470259b7f"
Last-Modified: Sun, 09 Dec 2012 05:12:50 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.329923
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
Date: Sun, 09 Dec 2012 05:32:02 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTk5YzkzZmM1NTEyZTBhZGZkNTIzNjQxNTM3OWQ2OWQwBjsAVEkiDGNhcnRfaWQGOwBGaRM%3D--9c58254a77a3f4e266ec9a144a8920f1a2d467c4; path=/; HttpOnly
 
curl --silent --dump - --output /dev/null http://localhost:3000/ -H 'If-Modified-Since: Sun, 09 Dec 2012 05:12:50 GMT'
HTTP/1.1 304 Not Modified 
Etag: "3e06c3071102f4f6f833d69470259b7f"
Last-Modified: Sun, 09 Dec 2012 05:12:50 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.248740
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
Date: Sun, 09 Dec 2012 05:32:02 GMT
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWRmNDUxMTgwYWQwOTJmYTE0YjAzMTA1ZTYzZDU1NWJlBjsAVEkiDGNhcnRfaWQGOwBGaRQ%3D--960ebe9f9b7805969a42c5230200f296ac3f658c; 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.

rake middleware
use Rack::Cache
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000003a79420>
use Rack::Runtime
use Rack::MethodOverride
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::RemoteIp
use Rack::Sendfile
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
run Depot::Application.routes
curl --silent --dump - --output /dev/null http://localhost:3000/
HTTP/1.1 200 OK 
Etag: "3e06c3071102f4f6f833d69470259b7f"
Last-Modified: Sun, 09 Dec 2012 05:12:50 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 2.397177
Date: Sun, 09 Dec 2012 05:32:16 GMT
X-Content-Digest: 09fb566dde373639cd896a60bfc2114119bb6c44
Content-Length: 6611
Age: 11
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
Connection: Keep-Alive
 
curl --silent --dump - --output /dev/null http://localhost:3000/ -H 'If-None-Match: "3e06c3071102f4f6f833d69470259b7f"'
HTTP/1.1 304 Not Modified 
Etag: "3e06c3071102f4f6f833d69470259b7f"
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 2.397177
Date: Sun, 09 Dec 2012 05:32:16 GMT
X-Content-Digest: 09fb566dde373639cd896a60bfc2114119bb6c44
Age: 11
X-Rack-Cache: fresh
Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
 

24.3 Active Resources 21.2 Form Helpers