• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

###caches_pageとの違い
caches_actionはcaches_pageと似ていますが、依然としてすべてのリクエストがAction Packを通って処理されるようになっています。これは認証や他の制限をつけたいときに便利です。

###"/lists"と"/lists.xml"は別々にキャッシュされる
また、http://blog.s21g.com/listsとhttp://blog.s21g.com/lists.xmlは異なるリクエストとして処理され、別々にキャッシュされます。つまり、:action => "lists"のキャッシュが無効になったからといって、:actioin => "list", :format => ":xml"も無効になるわけではありません。

###キャッシュの無効化
expire_actionでキャッシュを無効化できます。Sweepwerを使って処理、タイミングを書く。caches.rbのサンプルを抜き出します。
rails>>
class ListSweeper < ActionController::Caching::Sweeper
observe List, Item

def after_save(record)
   list = record.is_a?(List) ? record : record.list
   expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
   expire_action(:controller => "lists", :action => "all")
    list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
  end

end

class ListsController < ApplicationController
caches_action :index, :show, :public, :feed
cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
end
<<--

###Refs
vendor/rails/actionpack/lib/action_controller/caching.rb
http://scottstuff.net/presentations/rails-caching/ かなり詳しい
http://www.ibm.com/developerworks/web/library/wa-rails1/
http://www.hostingrails.com/forums/rails_coding_thread/386

posted by satoko satoko on Sat 28 Jun 2008 at 01:20 with 0 comments