我可以在redis中将字典保存为字符串,如下所示:
>>> r.set(
'rt.http://rottentomatoes.com/m/771354525/',
{"Director": "blasco ricardo", "Platform": "RT"}
)
我如何将字典直接保存为字典/哈希,这样我就不必使用 json.loads
将其读入字典?目前,如果我执行 r.get()
,我会得到字符串形式的字典:
>>> r.get('rt.http://rottentomatoes.com/m/771354525/')
'{"Director": "blasco ricardo", "Platform": "RT"}'
最佳答案
查看hmset
HMSET 'rt.http://rottentomatoes.com/m/771354525/' Director "blasco ricardo" Platform "RT"
然后你可以用
检索它HGETALL rt.http://rottentomatoes.com/m/771354525/
或者一个特定的字段用
HGET rt.http://rottentomatoes.com/m/771354525/ Director
在 python 中会是
r.hmset('rt.http://rottentomatoes.com/m/771354525/', {'Director': 'blasco ricardo', 'Platform': 'RT'})
r.hgetall('rt.http://rottentomatoes.com/m/771354525/')
r.hget('rt.http://rottentomatoes.com/m/771354525/', 'Director')
关于python - 保存 redis 哈希而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29158838/