0
我有一个使用memcached和dalli实现缓存的rails应用程序。Rails缓存获取散列的所有值
我存储的散列结果这样
Rails.cache.write("home_stays[#{home_stay['id']}]", home_stay, expires_in: 5.minutes)
我可以用它的ID特定寄宿获取价值。
我很困惑的是如何从缓存中获得所有的寄宿家庭。
如果我运行Rails.cache.fetch(“home_stays”),我得到零。
有没有办法让所有的缓存哈希值的轨道?
更新: 根据Chakreshwar的建议,我在寄宿家庭模型中添加了以下内容。
def self.home_stay_cache
Rails.cache.fetch("all_home_stays",expires_in: 5.minutes) do
all
end
end
在运行HomeStay.home_stay_cache我获得以下
Cache read: all_home_stays ({:expires_in=>300 seconds})
Cache generate: all_home_stays ({:expires_in=>300 seconds})
NameError: undefined local variable or method `all' for Homestay:class
试过这个并得到 Cacher ead:all_home_stays({:expires_in => 300 seconds}) 缓存生成:all_home_stays({:expires_in => 300秒}) NoMethodError:未定义方法'all'for#<预订:: HomeStaysController:0x0000000735b3d8> from ):13:在'block in index' – RamanSM
尝试更新的答案 –
没有工作!没有。 – RamanSM