#93 Action Caching

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

你可能感兴趣的:(cache,Rails)