redis 数据结构之 Hashes
Hashes 看起来就像我们所期待的那样,是一种 ”字段-值“ 表现的数据结构,下面是一个列子:
> hmset user:1000 username antirez birthyear 1977 verified 1
OK
> hget user:1000 username
"antirez"
> hget user:1000 birthyear
"1977"
> hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"
Hashes 在表示对象上非常的方便,而且它对放入其中的字段数没有限制(除非内存不够用了),所以我们可以在我们的应用程序中以很多不同的方式使用 Hashes。命令HMSET 设置 Hashes 的多个字段,而 HGET 检索单个字段。 HMGET 类似于 HGET,但返回一组值:
> hmget user:1000 username birthyear no-such-field
1) "antirez"
2) "1977"
3) (nil)
还有一些命令能够对各个字段执行操作,例如 HINCRBY:
> hincrby user:1000 birthyear 10
(integer) 1987
> hincrby user:1000 birthyear 10
(integer) 1997
在这个网址可以找到完整的 Hashes 的命令列表:full list of hash commands in the documentation,值得注意的是,small Hashes(即,具有小值的一些元素)以特殊方式编码在存储器中,使得它们非常有效。