モデルに応じて間接的にコントローラのアクション キャッシングを適切に使用する方法についてのアイデアが失われています。
バックグラウンド シナリオ:
製品オブジェクトの単純な Rails モデルを持つ
class Product < ActiveRecord::Base
...
end
フィードのリクエストを処理し、Product インスタンスを使用してビューをレンダリングするコントローラー
class FeedsController < ActionController::Base
# caches index action of a controller, generates large response
cache_action :index
def index
@products = Product.all
respond_to do |format|
format.xml
end
end
# need to clear cached `index' and fill cache again
# PUT request is routed to `update'
def update
expire_action :action => :index, :format => :xml
# ?? how call `index' again to update cache ??
render nothing: true
end
end
ここで、製品モデルの変更を監視する必要があります。変更が検出された場合は、FeedsController からのキャッシュされたアクションが無効になり、更新されたデータで満たされていることを確認してください。私がエキゾチックなことを何も求めていないことを願っています。
これまでのところ、次のオブザーバーで終了しました。
class FeedSweeper < ActionController::Caching::Sweeper
observe Product
def after_update(product)
# Not found other way than directly remove cached view
# for FeedsController
# No `@control' instance available here
Rails.cache.delete_matched /feeds\/.+\.xml$/
# How to invoke FeedsController#index from here ??
# To fill updated data in cache
end
end
- HTTP サーバーが 30 秒にハードコードされたタイムアウト エラーをスローするため、単純に GET 要求を送信してキャッシュされていないフィードをフェッチすることはできません。
- 製品モデルの変更を監視しているため、製品に接続されていない FeedsController のコールバックを作成する意味がありません。そのインスタンスを使用するだけです
独立したモデルの変更を監視し、他の独立したコントローラーアクションのキャッシュを更新する方法についてアドバイスはありますか?