Rails 3:Sweeper 未销毁碎片,认为缓存已禁用

发布于 2024-10-10 11:57:22 字数 1399 浏览 2 评论 0原文

我想用扫地机清除碎片。清除器回调被执行,但是对expire_fragment的调用什么也不做,因为(我假设)cache_configured?返回零。缓存已配置,片段正在创建并在我的模板中使用(在日志中验证)。我做错了什么?

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.rbuction_sweeper.rb

config.action_controller.perform_caching = true

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 也为 nil。

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?

I want to expire fragments with a sweeper. The sweeper callbacks are executed, but the calls to expire_fragment do nothing, because (I assume) cache_configured? returns nil. Caching is configured and fragments are being created and used in my templates (verified it in the logs). What am I doing wrong?

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

In log files, cache_configured? is nil and so is perform_caching and cache_store.

AuctionSweeper.expire_details 12732 nil=nil&&nil

So I assume, that my fragments are not expired because the code of expire_fragment reads:

File actionpack/lib/action_controller/caching/fragments.rb, line 87

87:       def expire_fragment(key, options = nil)
88:         return unless cache_configured?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野生奥特曼 2024-10-17 11:57:22

我找到了一个解决方案(黑客?) 这里建议设置@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 并想知道为什么。

I found a solution (hack?) here that suggests to set @controller and it works for me.

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

Also a note to myself: remember to return true from before filters also in sweepers or you get ActiveRecord::RecordNotSaved and wonder why.

几味少女 2024-10-17 11:57:22

您没有共享您的 Rails.env - 缓存在 developmenttest 中自然被禁用 - 也许您错过了一些东西?

You didn't share your Rails.env - caching is naturally disabled in development and test - maybe you missed something?

风筝有风,海豚有海 2024-10-17 11:57:22

我没有为控制器设置实例变量,而是使用一个名为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

Instead of setting the instance variable for the controller I use an initializer called 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

Then I inherit from it for any observer that is for sweeping caches.

class ModelSweeper < CacheSweepingObserver  
  observe Model

  def after_update(model)
    expire_fragment "#{model.id}"
    true
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文