# config.cache_classes = false config.cache_classes = true ---- 中略 ---------------------------- # config.action_controller.perform_caching = false config.action_controller.perform_caching = true
class CacheController < ApplicationController caches_page :p_cache def p_cache render :text => Time.now end end
# config.action_controller.perform_caching = false config.action_controller.perform_caching = true
caches_page :p_cache_xml_only, :if => Proc.new { |c| c.request.format.xml? } def p_cache_xml_only respond_to do |format| format.html { render :text => Time.now } format.xml { render :text => "<data>" + Time.now.to_s + "</data>" } end end
cache: [GET /cache/p_cache_xml_only/1.xml] miss
before_filter :auth,:only => ['p_cache','a_cache'] # p_cacheとa_cacheにauthフィルタを設定 caches_page :p_cache caches_action :a_cache ---- 中略 ------------ def p_cache render :text => Time.now end def a_cache render :text => Time.now end private def auth ---- BASIC認証するトコ ----- end
現在時刻(キャッシュなし):<%= Time.now %><br /> <% cache(:suffix => 'footer') do %> 現在時刻(キャッシュあり):<%= Time.now %><br /> <% end %>
expire_action :action => 'index'
class BookSweeper < ActionController::Caching::Sweeper observe Book # ここでobserveメソッド登録。 def after_create(book) gc_cache(book) end def after_update(book) gc_cache(book) end def after_destroy(book) gc_cache(book) end private def gc_cache(book) expire_page :controller => 'books', :action => 'index' expire_page :controller => 'books', :action => 'show', :id => book.id end end
class BooksController < ApplicationController caches_page :index, :show cache_sweeper :book_sweeper