#89 Page Caching

Page caching is an efficient way to cache stateless content. In this episode I will show you how to cache the dynamic javascript we created last week.
# javascripts_controller.rb
caches_page :dynamic_states

# config/environments/development.rb
config.action_controller.perform_caching = true

# config/environment.rb
config.load_paths << "#{RAILS_ROOT}/app/sweepers"

# app/sweepers/state_sweeper.rb
class StateSweeper < ActionController::Caching::Sweeper
observe State

def after_save(state)
expire_cache(state)
end

def after_destroy(state)
expire_cache(state)
end

def expire_cache(state)
expire_page :controller => 'javascripts', :action => 'dynamic_states', :format => 'js'
end
end

# states_controller.rb
cache_sweeper :state_sweeper, :only => [:create, :update, :destroy]
發佈了108 篇原創文章 · 獲贊 0 · 訪問量 2935
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章