Action caching behaves much like page caching except it processes the controller filters. You can also make it conditional as seen in this episode.
# products_controller.rb
cache_sweeper :product_sweeper, :only => [:create, :update, :destroy]
caches_action :index, :cache_path => :index_cache_path.to_proc
#...
private
def index_cache_path
if admin?
'/admin/products'
else
'/public/products'
end
end
# config/environments/development.rb
config.action_controller.perform_caching = true
# config/environment.rb
config.load_paths << "#{RAILS_ROOT}/app/sweepers"
# app/sweepers/product_sweeper.rb
class ProductSweeper < ActionController::Caching::Sweeper
observe Product
def after_save(product)
expire_cache(product)
end
def after_destroy(product)
expire_cache(product)
end
def expire_cache(product)
expire_action '/admin/products'
expire_action '/public/products'
end
end
本文介绍了一种类似于页面缓存但处理控制器过滤器的行动缓存技术,并展示了如何使其成为条件性的,通过产品控制器中的示例代码说明了如何配置缓存动作及路径。
103

被折叠的 条评论
为什么被折叠?



