0

我正在尝试修补 railsRails.cache.fetch/read方法,并在每次获取/读取发生时添加我的额外日志,但我遇到了一些其他错误,我无法通过谷歌搜索任何答案。

这是我的猴子补丁代码

module ActiveSupport
    module Cache
        class Store
            alias_method :old_fetch, :fetch
            alias_method :old_read, :read

            def fetch(name, options=nil)
                Rails.logger.info "Memcached Hotkey Fetching: #{name}"
                Rails.logger.info caller
                old_fetch(name, options)
            end

            def read(name, options=nil)
                Rails.logger.info "Memcached Hotkey Reading: #{name}"
                Rails.logger.info caller
                old_read(name, options)
            end
        end
    end
end

这是特拉维斯失败的地方

class B < ActiveRecord::Base
  def self.cache
    Rails.cache.fetch(CACHE_KEY, :expires_in => 1.hour) do
      all
    end
  end

某处有代码调用B.cache.each do |x| blablabla end

Error message:  You have a nil object when you didn't expect it! (NoMethodError),
You might have expected an instance of Array.
The error occurred while evaluating nil.each

问题是我做错了什么?我只是在 Store 下修补两个方法,但为什么它似乎覆盖了调用 .cache 的所有内容

4

0 回答 0