【发布时间】:2011-09-17 12:34:28
【问题描述】:
我在 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 patchlevel 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 块正在尝试缓存对象。
有什么建议吗?提前致谢!
【问题讨论】:
-
为什么要缓存一个范围?缓存实际数据不是更好吗?尝试在每个作用域的末尾添加
all方法。Model.scope_with_lambda.all -
@Oleander - 是的,根据下面的响应,我们之前似乎很幸运能够将模型对象存储到缓存中。我们将重新编码以缓存数据而不是对象。感谢您的想法!
标签: ruby-on-rails ruby ruby-on-rails-3 caching memcached