查看列表保存为redis哈希

时间:2014-07-21 19:01:02

标签: redis

我已将列表保存到哈希中,但无法弄清楚如何检索哈希的内容。首先,这里有一些代码来创建列表:

127.0.0.1:6379> LPUSH list1 'dc:39:79:ab:cd:ef' (integer) 1 127.0.0.1:6379> LPUSH list1 '2014-07-21' (integer) 2 127.0.0.1:6379> LPUSH list1 'Success' (integer) 3 127.0.0.1:6379> LPUSH list1 'Miscellaneous notes about the install. Can be as long as you want' (integer) 4

现在我创建一个哈希并将一个键的值赋给list1:

127.0.0.1:6379> hset hash 'RKT1234' list1 (integer) 1

如何打印保存在哈希[' RKT1234']中的整个列表?

127.0.0.1:6379> hgetall hash 1) "RKT1234" 2) "list1"

127.0.0.1:6379> hvals hash 1) "list1"

感谢

1 个答案:

答案 0 :(得分:1)

您似乎试图将列表存储为哈希值。由于Redis不支持嵌套数据结构,因此您没有按照自己的想法进行操作。在hset hash 'RKT1234' list1中,您不存储列表,只是字符串名称:“list1”。

为了获取列表的内容,您需要首先从哈希中获取列表的名称,然后在第二次调用中获取列表的内容。

所以你的序列看起来像这样:

# returns "list1"
hvals hash 
lrange list1 0 -1
# returns the contents of list1

干杯

相关问题