hset
设置 hash field 为指定值,如果 key 不存在,则先创建。
hset myHash field1 Hello
hsetnx
设置 hash field 为指定值,如果 key 不存在,则先创建。如果 field 已经存在,返回 0,nx 是 not exist 的意思。
hsetnx myHash field Hello
第一次执行是成功的,但第二次执行相同的命令失败,原因是 field 已经存在了。
hmset
同时设置 hash 的多个 field。
hmset myHash field1 Hello field2 World
hget
获取指定的 hash field。
hget myHash field1 hget myHash field2 hget myHash field3
由于数据库没有 field3,所以取到的是一个空值 nil 。
hmget
获取全部指定的 hash filed。
hmget myHash field1 field2 field3
由于数据库没有 field3,所以取到的是一个空值 nil 。
hincrby
指定的 hash filed 加上给定值。
hincrby myHash field3 -8
在本例中我们将 field3 的值从 20 降到了 12,即做了一个减 8 的操作。
hexists
测试指定 field 是否存在。
hexists myHash field1 hexists myHash field9
通过上例可以说明 field1 存在,但 field9 是不存在的。
hlen
返回指定 hash 的 field 数量。
hlen myHash
通过上例可以看到 myHash 中有 4 个 field。
hdel
删除指定 hash 的 field 。
hdel myHash field1
hkeys
返回 hash 的所有 field。
hkeys myHash
说明这个 hash 中有 3 个 field 。
hvals
返回 hash 的所有 value。
hvals myHash
说明这个 hash 中有 3 个 field 。
hgetall
获取某个 hash 中全部的 filed 及 value。
hgetall myHash
可见,一下子将 myHash 中所有的 field 及对应的 value 都取出来了。