我在 3.1.0.rc4(ruby 1.9.2p180(2011-02-18 修订版 30909)[x86_64-darwin10])上遇到 Rails.cache 方法问题。该代码在 2.3.12(ruby 1.8.7(2011-02-18 补丁级别 334)[i686-linux],MBARI 0x8770,Ruby Enterprise Edition 2011.03)上的同一应用程序中运行良好,但在升级后开始返回错误。我还没弄明白为什么。
当尝试缓存具有多个作用域的对象时似乎会发生错误。
此外,无论有多少范围,使用 lambda 的任何范围都会失败。
我曾因这些模式而失败:
Rails.cache.fetch("keyname", :expires_in => 1.minute) do
Model.scope_with_lambda
end
Rails.cache.fetch("keyname", :expires_in => 1.minute) do
Model.scope.scope
end
这是我收到的错误:
TypeError: can't dump hash with default proc
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:627:in `dump'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:627:in `should_compress?'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:559:in `initialize'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:363:in `new'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:363:in `block in write'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:520:in `instrument'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:362:in `write'
from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:299:in `fetch'
from (irb):62
from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我曾尝试使用 :raw => true 选项作为替代方案,但这不起作用,因为 Rails.cache.fetch block 正在尝试缓存对象。
有什么建议吗?提前致谢!
最佳答案
这可能有点冗长,但我不得不花一些时间阅读 Rails 源代码以了解缓存内部机制的工作原理。把事情写下来有助于我的理解,我认为分享一些关于事情如何运作的笔记不会有什么坏处。如果您赶时间,请跳到最后。
为什么会这样
这是 ActiveSupport 中的违规方法:
def should_compress?(value, options)
if options[:compress] && value
unless value.is_a?(Numeric)
compress_threshold = options[:compress_threshold] || DEFAULT_COMPRESS_LIMIT
serialized_value = value.is_a?(String) ? value : Marshal.dump(value)
return true if serialized_value.size >= compress_threshold
end
end
false
end
注意分配给 serialized_value
.如果你在里面闲逛cache.rb
, 你会看到它使用 Marshal在对象进入缓存之前将对象序列化为字节字符串,然后再次编码以反序列化对象。压缩问题在这里并不重要,重要的是Marshal的使用。
问题is that :
Some objects cannot be dumped: if the objects to be dumped include bindings, procedure or method objects, instances of class IO, or singleton objects, a TypeError will be raised.
某些事物具有无法由 Marshal 序列化的状态(例如 OS 文件描述符或 block )。您注意到的错误是:
can't dump hash with default proc
所以你的模型中的某个人有一个实例变量,它是一个散列,并且该散列使用一个 block 来提供默认值。 column_methods_hash
方法使用这样的哈希,甚至将哈希缓存在 @dynamic_methods_hash
中; column_methods_hash
将被公共(public)方法(间接)调用,例如 respond_to?
和 method_missing
.
respond_to?
之一或 method_missing
可能迟早会在每个 AR 模型实例上被调用,并且调用任一方法都会使您的对象不可序列化。因此,AR 模型实例在 Rails 3 中本质上是不可序列化的。
有趣的是,respond_to?
和 method_missing
2.3.8 中的实现也由使用默认值 block 的哈希支持。 2.3.8缓存为"[...]is meant for caching strings."所以你很幸运有一个可以处理整个对象的后端,或者它在你的对象中有 hash-with-procs 之前使用了 Marshal;或者您可能正在使用 MemoryStore
缓存后端,这只不过是一个大哈希。
使用多个 scope-with-lambdas 可能最终将 Procs 存储在您的 AR 对象中;我希望 lambda 与类(或单例类)而不是对象一起存储,但我没有费心分析 respond_to?
的问题。和 method_missing
使 scope
问题无关。
你能做些什么
我认为您一直在缓存中存储了错误的内容并且很幸运。您可以开始正确使用 Rails 缓存(即存储简单生成的数据而不是整个模型)或者您可以实现 marshal_dump
/marshal_load
或 _dump
/_load
Marshal 中概述的方法.或者,您可以使用 MemoryStore 后端之一,并将自己限制为每个服务器进程一个不同的缓存。
执行摘要
您不能依赖于将 ActiveRecord 模型对象存储在 Rails 缓存中,除非您准备好自己处理编码或者您想将自己限制在 MemoryStore 缓存后端。
问题的确切根源在较新版本的 Rails 中已发生变化,但仍有许多 default_proc
实例s 与哈希相关联。
关于ruby-on-rails - Rails 3.1 中的 Rails.cache 错误 - TypeError : can't dump hash with default proc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391855/