7

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

更新:由于观察员被放入app/models,我假设清扫器没有什么不同,只要名称总是以“清扫器”结尾。

4

3 回答 3

4

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

我还放入了Presentersapp /presenters目录……和app/observers目录。Observers

于 2012-01-26T19:26:21.177 回答
0

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

于 2012-01-26T19:27:24.573 回答
-1

清扫车

缓存清除是一种机制,它允许您绕过代码中的大量 expire_{page,action,fragment} 调用。它通过将使缓存内容过期所需的所有工作移动到 na ActionController::Caching::Sweeper 类中来实现这一点。此类是一个观察者,它通过回调查找对象的更改,当发生更改时,它会使与该对象关联的缓存在周围或后过滤器中过期。

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

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

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

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

源导轨指南

于 2019-07-28T03:24:35.093 回答