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