在 Rails 中,你把 Sweepers 放在哪里?

发布于 2024-12-29 08:03:05 字数 123 浏览 1 评论 0原文

Rails 中是否有约定将 Sweeper 类放在特定的目录位置?

更新:由于观察者被放入app/models中,我假设sweeper没有什么不同,只要名称始终以“sweeper”结尾即可。

Is there a convention in Rails to put Sweeper classes in a particular directory location?

UPDATE: Since observers are put into app/models, I'm assuming sweepers are no different, as long as the name always ends with "sweeper".

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

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

发布评论

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

评论(3

月下凄凉 2025-01-05 08:03:05

我喜欢将它们放在 app/sweepers 目录中。

我还将 Presenters 放在 app/presenters 目录中...并将 Observers 放在 app/observers 目录中。

I like to put them in the app/sweepers directory.

I also put Presenters in the app/presenters directory...and Observers in the app/observers directory.

菊凝晚露 2025-01-05 08:03:05

尝试将它们放入 app/models 目录中。

Try putting them in the app/models directory.

娇纵 2025-01-05 08:03:05

缓存

清理是一种机制,可以让您避免代码中出现大量的 expire_{page,action,fragment} 调用。它通过将使缓存内容过期所需的所有工作移至
一个 ActionController::Caching::Sweeper 类。此类是一个观察者,它通过回调查找对象的更改,当发生更改时,它会使周围或之后过滤器中与该对象关联的缓存过期。

继续我们的产品控制器示例,我们可以使用扫描器重写它,如下所示:

class StoreSweeper < ActionController::Caching::Sweeper
  # This sweeper is going to keep an eye on the Product model
  observe Product

  # If our sweeper detects that a Product was created call this
  def after_create(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was updated call this
  def after_update(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was deleted call this
  def after_destroy(product)
          expire_cache_for(product)
  end

  private
  def expire_cache_for(record)
    # Expire the list page now that we added a new product
    expire_page(:controller => '#{record}', :action => 'list')

    # Expire a fragment
    expire_fragment(:controller => '#{record}', 
      :action => 'recent', :action_suffix => 'all_products')
  end
end

扫描器必须添加到将使用它的控制器中。因此,如果我们想在调用创建操作时使列表和编辑操作的缓存内容过期,我们可以执行以下操作

class ProductsController < ActionController

  before_filter :authenticate, :only => [ :edit, :create ]
  caches_page :list
  caches_action :edit
  cache_sweeper :store_sweeper, :only => [ :create ]

  def list; end

  def create
    expire_page :action => :list
    expire_action :action => :edit
  end

  def edit; end

end

Sweepers

Cache sweeping is a mechanism which allows you to get around having a ton of expire_{page,action,fragment} calls in your code. It does this by moving all the work required to expire cached content into
na ActionController::Caching::Sweeper class. This class is an Observer that looks for changes to an object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.

Continuing with our Product controller example, we could rewrite it with a sweeper like this:

class StoreSweeper < ActionController::Caching::Sweeper
  # This sweeper is going to keep an eye on the Product model
  observe Product

  # If our sweeper detects that a Product was created call this
  def after_create(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was updated call this
  def after_update(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was deleted call this
  def after_destroy(product)
          expire_cache_for(product)
  end

  private
  def expire_cache_for(record)
    # Expire the list page now that we added a new product
    expire_page(:controller => '#{record}', :action => 'list')

    # Expire a fragment
    expire_fragment(:controller => '#{record}', 
      :action => 'recent', :action_suffix => 'all_products')
  end
end

The sweeper has to be added to the controller that will use it. So, if we wanted to expire the cached content for the list and edit actions when the create action was called, we could do the following:

class ProductsController < ActionController

  before_filter :authenticate, :only => [ :edit, :create ]
  caches_page :list
  caches_action :edit
  cache_sweeper :store_sweeper, :only => [ :create ]

  def list; end

  def create
    expire_page :action => :list
    expire_action :action => :edit
  end

  def edit; end

end

source rails guide

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