保存 redis 哈希而不是字符串
Save redis hash instead of string
我可以在redis中将字典保存为字符串,如下所示:
>>> r.set(
'rt.http://rottentomatoes.com/m/771354525/',
{"Director": "blasco ricardo", "Platform": "RT"}
)
如何将字典直接保存为 dict/hash,这样我就不必使用 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')
我可以在redis中将字典保存为字符串,如下所示:
>>> r.set(
'rt.http://rottentomatoes.com/m/771354525/',
{"Director": "blasco ricardo", "Platform": "RT"}
)
如何将字典直接保存为 dict/hash,这样我就不必使用 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')