私のRails 3アプリは、指定された期間変更され、残りのライフタイムでは静的(変更なし)になるページを生成します(スポーツのスコアボードを考えてみてください)。
これはページ全体をキャッシュする絶好の機会のように思われるので、応答の Last-Modified 部分を使用してキャッシュが無効になったことを示す Rack::Cache を選択しました。
キャッシュはうまく機能します。Last-Modified フィールドがリクエストの If-Modified-Since フィールドより後の日時で更新され、レスポンスがステータス 200 (304 ではなく) を生成した場合でも、ブラウザは引き続きサーバーにキャッシュされているページのバージョン。
サーバーのデバッグログに表示される内容は次のとおりです。
App 16638 stdout: Started GET "/games/2014/2/10" for xx.xx.xx.xxx at 2014-02-11 04:04:11 +0000
App 16638 stdout: Processing by GamesController#index as HTML
App 16638 stdout: Parameters: {"year"=>"2014", "month"=>"2", "day"=>"10"}
App 16638 stdout: Game Load (2.8ms) SELECT "games".* FROM "games" WHERE "games"."date" = '2014-02-10'
App 16638 stdout: Game Load (2.8ms) SELECT "games".* FROM "games" WHERE "games"."id" = 877 LIMIT 1
App 16638 stdout: Team Load (1.3ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 10 LIMIT 1
App 16638 stdout: Team Load (0.7ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 23 LIMIT 1
App 16638 stdout: Game Load (4.2ms) SELECT "games".* FROM "games" WHERE "games"."id" = 875 LIMIT 1
App 16638 stdout: Team Load (0.6ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 17 LIMIT 1
App 16638 stdout: Team Load (5.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 2 LIMIT 1
App 16638 stdout: Game Load (7.0ms) SELECT "games".* FROM "games" WHERE "games"."id" = 874 LIMIT 1
App 16638 stdout: Team Load (0.7ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 9 LIMIT 1
App 16638 stdout: Team Load (7.9ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 27 LIMIT 1
App 16638 stdout: Game Load (0.7ms) SELECT "games".* FROM "games" WHERE "games"."id" = 876 LIMIT 1
App 16638 stdout: Team Load (0.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 18 LIMIT 1
App 16638 stdout: Team Load (0.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 11 LIMIT 1
App 16638 stdout: Game Load (0.6ms) SELECT "games".* FROM "games" WHERE "games"."id" = 873 LIMIT 1
App 16638 stdout: Team Load (4.0ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 12 LIMIT 1
App 16638 stdout: Team Load (0.6ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 8 LIMIT 1
App 16638 stdout: Game Load (5.4ms) SELECT "games".* FROM "games" WHERE "games"."id" = 872 LIMIT 1
App 16638 stdout: Team Load (0.6ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 28 LIMIT 1
App 16638 stdout: Team Load (3.9ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = 19 LIMIT 1
App 16638 stdout: Latest game: 2014-02-11 03:44:50 UTC
App 16638 stdout: Cache read: views/xxxx/games/2014/2/10
App 16638 stdout: Dalli::Server#connect 127.0.0.1:11211
App 16638 stdout: Read fragment views/xxxxx/games/2014/2/10 2.6ms
App 16638 stdout: Completed 200 OK in 618.2ms (ActiveRecord: 128.8ms)
App 15100 stderr: cache: [GET /games/2014/2/10] stale, invalid, store
App 16638 stdout: Started GET "/assets/bootstrap.css" for 66.55.150.181 at 2014-02-11 04:04:12 +0000
App 16638 stdout: Served asset /bootstrap.css - 304 Not Modified (12ms)
App 15100 stderr: cache: [GET /assets/bootstrap.css] stale, valid, store
App 15100 stderr: cache: [GET /assets/application-85cd667c6ae785b5d80f452fe6ad811e.js] fresh
App 15100 stderr: cache: [GET /assets/application-50dd9f33494a58a079e7417a68763e42.css] fresh
リクエスト/レスポンスの例を次に示します (200 ステータスを参照)。
Request URL:http://xxxxxxx.com/games/2014/2/10
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:_gamestory_app_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJWIwYjAwNWQ3NTBmYTk4YjQ0YWRjYjEwMWQ5Y2ZjYTA2BjsAVEkiEF9jcx3JmX3Rva2VuBjsARkkiMXdKNWZZUWFhZDVHN3hEeEJjaWxYclp1NmN4OGUrSkI4VzJ2eXdWOUsrc1E9BjsARg%3D%3D--6f99b71bca632560c069c371fdb6a4477a26dfa3
Host:xxxxxxxxx.com
If-Modified-Since:Tue, 11 Feb 2014 03:43:06 GMT
If-None-Match:"e282d8be52fbe437d3c2d3908288100d"
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17
Response Headersview source
Age:0
Cache-Control:public
Connection:keep-alive
Content-Length:10411
Content-Type:text/html; charset=utf-8
Date:Tue, 11 Feb 2014 04:04:11 GMT
ETag:"d6d4c1f573a56353e0abbe14de76e306"
Last-Modified:Tue, 11 Feb 2014 03:44:50 GMT
Server:nginx/1.4.4 + Phusion Passenger 4.0.29
Status:200 OK
X-Content-Digest:7b2caeed19cc4674669f4c386271054427342b5e
X-Powered-By:Phusion Passenger 4.0.29
X-Rack-Cache:stale, invalid, store
X-Request-Id:c19584efff9941bf5c456cb68d85b192
X-Runtime:0.661659
X-UA-Compatible:IE=Edge,chrome=1
関連するコントローラーでは、特定のアクションに before_filter を使用し、そのフィルターでは、ページの最後に更新されたオブジェクトで fresh_when を使用します。これにより、応答で正しい日付/時刻が生成されるように見えますが、考えられるエラーのコードを含めています (簡潔にするために show アクションを削除しました)。
class GamesController < ApplicationController
helper ApplicationHelper
before_filter :set_index_cache_headers, :only => [:index]
caches_action :index
def set_index_cache_headers
@date = Date.today
if params[:year] && params[:month] && params[:day]
@date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
else
most_recent_game = Game.where(status: [:final, :in_progress]).order("date DESC").first
if most_recent_game != nil
@date = most_recent_game.date
end
end
@games = Game.where(:date => @date).compact
@game_infos = []
@games.each do |g|
@game_infos.push GameInfo.new(g.id)
end
@latest_game = @games.max_by{|g| g.updated_at}
Rails.logger.debug "Latest game: #{@latest_game.updated_at}"
fresh_when(@latest_game, public: true)
end
def index
end
end