python - 保存 redis 哈希而不是字符串

标签 python redis

我可以在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/

相关文章:

python3 queue.PriorityQueue 变化?

python - 列表推导式提示

redis - 使用redis pub/sub连接Tornado进程

node.js - Redis 队列的多个 node.js 消息接收器

python - 如何刷新已经打开的网页

python - 按日期查找值并对它们求和

php - laravel v4.2 中 Redis 服务器连接错误

multithreading - 为什么 Redis 是单线程的(事件驱动)

Python set Union 和 set Intersection 的操作方式不同?

Redis在获取键值时响应各种$值