我想用 Rails.cache(memory_store) 维护一个用户在线/离线列表。
基本上如果一个请求 /user/heartbeat?name=John
到达 Rails 服务器,它会简单地:
def update_status
name = params.require(:name)
Rails.cache.write(name, Time.now.utc.iso8601, expires_in: 6.seconds)
end
但是我怎样才能像下面一样获取存储在 Rails.cache 中的所有数据?
def get_status
# wrong codes, as Rails.cache.read doesn't have :all option.
ary = Rails.cache.read(:all)
# deal with array ...
end
我用谷歌搜索了一段时间,似乎 Rails.cache 没有提供直接获取所有数据的方法。或者有更好的方法来存储数据?
我正在使用
Rails 5.0.2
.谢谢你的时间!
最佳答案
您可以使用代码获取所有 key :
keys = Rails.cache.instance_variable_get(:@data).keys
关于ruby-on-rails - rails : how to get all key-values from Rails. 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786882/