由于某种原因,以下内容不会在 10 秒后过期
def rcache_value
@random_val_from_cache = Rails.cache.fetch("random_val_from_cache",:expires_in=>10.seconds) do
rand 10000
end
@random_val_from_cache
我在我的应用程序的页脚中输出。我的参数错了吗?我在 Rails 3 上。
由于某种原因,以下内容不会在 10 秒后过期
def rcache_value
@random_val_from_cache = Rails.cache.fetch("random_val_from_cache",:expires_in=>10.seconds) do
rand 10000
end
@random_val_from_cache
我在我的应用程序的页脚中输出。我的参数错了吗?我在 Rails 3 上。
找到了答案。expires_in 已被弃用,fetch
取而代之的是在 write 中设置它。因此,您必须编写检查以查看它是否已存在于缓存中,如果不存在则进行写入
好吧,假设您的示例中的语法错误只是 SO 上的一个错字,那么问题很可能是因为您使用的是实例变量。缓存的值被设置为该实例变量,除非手动这样做,否则不会重置该实例变量,或者除非重新加载类(这在服务器重新启动之前不会发生)。你应该这样做:
def rcache_value
Rails.cache.fetch("random_val_from_cache", :expires_in => 10.seconds) do
rand 10000
end
end
并调用它:
<%= rcache_value %>