Rails 清扫机可以跨不同控制器工作吗?

发布于 2024-10-08 10:05:50 字数 1109 浏览 1 评论 0原文

我在我的站点索引上进行了操作缓存,并设置了一个工作正常的 SiteSweeper:

# 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

但我也希望在保存或销毁任何发布者时使 /admin/sites 过期。 PublisherSweeper 是否可以通过类似的方式使站点索引过期?

# 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

我知道我可以在各种发布者操作中调用 expire_action '/admin/sites' 。我只是想知道扫地机是否有这种功能(让我的控制器保持干净一点)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-10-15 10:05:50

一台扫地机可以观察多个模型,任何控制器可以有多个扫地机。

我认为你应该改变你的逻辑以使用类似的东西:

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

On PublishersController

  cache_sweeper :site_sweeper, :admin_sweeper

所以你不会重复清理 /admin/site 的逻辑。将其称为 AdminSweeper,这样当出现问题时,您就知道唯一一个使“/admin/sites”操作过期的位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文