2

I have action caching working on my Sites index, and set up a SiteSweeper that works fine:

# app/controllers/admin/sites_controller.rb
class Admin::SitesController < Admin::BaseController
  cache_sweeper :site_sweeper, :only => [:create, :update, :destroy]
  caches_action :index, :cache_path => '/admin/sites'
  ...

# app/sweepers/site_sweeper.rb
class SiteSweeper < ActionController::Caching::Sweeper
  observe Site

  def after_save(site)
    expire_cache(site)
  end

  def after_destroy(site)
    expire_cache(site)
  end

  def expire_cache(site)
    expire_action '/admin/sites'
  end
end

But I also want to expire /admin/sites whenever any Publishers are saved or destroyed. Is it possible to have a PublisherSweeper expire the Sites index with something like this?

# app/sweepers/publisher_sweeper.rb
class PublisherSweeper < ActionController::Caching::Sweeper
  observe Publisher

  def after_save(publisher)
    expire_cache(publisher)
  end

  def after_destroy(publisher)
    expire_cache(publisher)
  end

  def expire_cache(publisher)
    expire_action '/admin/sites'
  end
end

I know I can just call expire_action '/admin/sites' within the various Publisher actions. I'm just wondering if sweepers have this capability (to keep my controllers a bit cleaner).

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
jhiro009
  • 576
  • 4
  • 13

1 Answers1

5

One sweeper can observe many Models, and any controller can have multiple sweepers.

I think you should change your logic to use something like that:

class SiteSweeper < ActionController::Caching::Sweeper
  observe Site, Publisher
  (…)
end

On PublishersController

  cache_sweeper :site_sweeper, :admin_sweeper

So you don't repeat the logic of cleaning the /admin/site. Call it AdminSweeper, so when something goes wrong you know the only one place that expired the "/admin/sites" action.

Henry Mazza
  • 764
  • 1
  • 6
  • 18
  • Oh so a sweeper has to observe Site in order to expire a SiteController action. Thanks! – jhiro009 Dec 23 '10 at 20:57
  • The sweeper has to observe all models which are used by the controller to render its view. For instance if a RSS controller shows new articles and new comments mixed together it must observe both Comment and Article models so it expires it's view if anyone changes. – Henry Mazza Dec 24 '10 at 12:10
  • Do we need to create another file with admin_sweeper ? – Shilpi Agrawal May 11 '15 at 10:00