RailsにSweeper
クラスを特定のディレクトリの場所に配置するための規則はありますか?
更新:オブザーバーが入れられるのでapp/models
、名前が常に「sweeper」で終わる限り、スイーパーも同じだと思います。
RailsにSweeper
クラスを特定のディレクトリの場所に配置するための規則はありますか?
更新:オブザーバーが入れられるのでapp/models
、名前が常に「sweeper」で終わる限り、スイーパーも同じだと思います。
私はそれらをapp/sweepersディレクトリに置くのが好きです。
Presenters
また、app/presentersディレクトリとObservers
app /observersディレクトリにも配置します。
app/models
それらをディレクトリに入れてみてください。
キャッシュスイープは、コードで大量の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
ソースレールガイド