Rails 3:断片を破壊しないスイーパーは、キャッシュが無効になっていると考えています

StackOverflow https://stackoverflow.com/questions/4598293

質問

スイーパーでフラグメントを期限切れにしたいです。スイーパーコールバックは実行されますが、expire_fragmentの呼び出しは何もしません。 nilを返します。キャッシュが構成され、フラグメントが作成され、私のテンプレートで使用されています(ログで確認されました)。私は何が間違っているのですか?

Application.RB

config.cache_store = :mem_cache_store, "XXX.XXX.XXX.XXX", { # I use a real IP
    :compress => true,
    :namespace => "#{Rails.env}_r3"
  }
config.active_record.observers = [:auction_sweeper, :address_sweeper]

production.rb

config.action_controller.perform_caching = true

auction_sweeper.rb

class AuctionSweeper < ActionController::Caching::Sweeper 
  observe Auction

  def after_create(auction)
    Rails.logger.info "AuctionSweeper.expire_details #{auction.id} #{cache_configured?.inspect}=#{perform_caching.inspect}&&#{cache_store.inspect}"
    expire_fragment("auction/#{auction.reference_sid}")
  end
end

ログファイルでは、cache_configured? NILで、Perform_cachingとcache_storeもそうです。

AuctionSweeper.expire_details 12732 nil=nil&&nil

ですから、expire_fragmentのコードが読み取られているため、私のフラグメントは期限切れになっていないと思います。

ファイルActionPack/lib/action_controller/caching/fragments.rb、行87

87:       def expire_fragment(key, options = nil)
88:         return unless cache_configured?
役に立ちましたか?

解決

解決策を見つけました(ハック?) ここ それは@Controllerを設定することを示唆しており、私には機能します。

class AuctionSweeper < ActionController::Caching::Sweeper 
  observe Auction

  def after_create(auction)
    @controller ||= ActionController::Base.new
    Rails.logger.info "AuctionSweeper.expire_details #{auction.id} #{cache_configured?.inspect}=#{perform_caching.inspect}&&#{cache_store.inspect}"
    expire_fragment("auction/#{auction.reference_sid}")
  end
end

また、自分自身へのメモ:スイーパーでもフィルターの前からtrueを返すことを忘れないでください。または、Activerecord :: RecordNotsavedを取得して、理由を疑問に思います。

他のヒント

あなたはあなたを共有しませんでした Rails.env - キャッシュは自然に無効になります developmenttest - たぶんあなたは何かを逃しましたか?

コントローラーのインスタンス変数を設定する代わりに、cache_sweeping_observer.rbという初期イザーを使用します

class CacheSweepingObserver < ActiveRecord::Observer
  include ActiveSupport::Configurable
  include ActionController::Caching

  config_accessor :perform_caching

  class << self
    def config
      ActionController::Base.config
    end
  end
end

それから私は、キャッシュを一掃するためのあらゆる観察者のためにそれから継承します。

class ModelSweeper < CacheSweepingObserver  
  observe Model

  def after_update(model)
    expire_fragment "#{model.id}"
    true
  end
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top