7

RailsにSweeperクラスを特定のディレクトリの場所に配置するための規則はありますか?

更新:オブザーバーが入れられるのでapp/models、名前が常に「sweeper」で終わる限り、スイーパーも同じだと思います。

4

3 に答える 3

4

私はそれらをapp/sweepersディレクトリに置くのが好きです。

Presentersまた、app/presentersディレクトリとObserversapp /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クラスに移動することによって行われます。このクラスは、コールバックを介してオブジェクトへの変更を検索するオブザーバーであり、変更が発生すると、フィルターの周囲または後にそのオブジェクトに関連付けられたキャッシュが期限切れになります。

製品コントローラーの例を続けると、次のようなスイーパーで書き直すことができます。

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

ソースレールガイド

于 2019-07-28T03:24:35.093 に答える