HSET
将哈希表 hash 中域 field 的值设置为 value 。
如果给定的哈希表并不存在, 那么一个新的哈希表将被创建并执行 HSET 操作。
如果域 field 已经存在于哈希表中, 那么它的旧值将被新值 value 覆盖。
127.0.0.1:6379> hset website google "www.google.com"
(integer) 1
127.0.0.1:6379> hget website google
"www.google.com"
127.0.0.1:6379> hset website google "ww.google.cn"
(integer) 0
127.0.0.1:6379> hget website google
"ww.google.cn"
HSETNX
当且仅当域 field 尚未存在于哈希表的情况下, 将它的值设置为 value 。
如果给定域已经存在于哈希表当中, 那么命令将放弃执行设置操作。
如果哈希表 hash 不存在, 那么一个新的哈希表将被创建并执行 HSETNX 命令。
127.0.0.1:6379> hsetnx database key-value-store Redis
(integer) 1
127.0.0.1:6379> hget database key-value-store
"Redis"
127.0.0.1:6379> hsetnx database key-value-store riak
(integer) 0
127.0.0.1:6379> hget database key-value-store
"Redis"
HGET
HGET 命令在默认情况下返回给定域的值。
如果给定域不存在于哈希表中, 又或者给定的哈希表并不存在, 那么命令返回 nil 。
HEXISTS
检查给定域 field 是否存在于哈希表 hash 当中。
127.0.0.1:6379> hexists website google
(integer) 1
127.0.0.1:6379> hexists website baidu
(integer) 0
HDEL
删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
127.0.0.1:6379> hgetall database
1) "key-value-store"
2) "Redis"
127.0.0.1:6379> hdel database key-value-store
(integer) 1
127.0.0.1:6379> hgetall database
(empty list or set)
HLEN
返回哈希表 key 中域的数量。
127.0.0.1:6379> hset db redis redis.com
(integer) 1
127.0.0.1:6379> hset db mysql mysql.com
(integer) 1
127.0.0.1:6379> hlen db
(integer) 2
127.0.0.1:6379> hset db mongodb mongodb.org
(integer) 1
127.0.0.1:6379> hlen db
(integer) 3
HSTRLEN
返回哈希表 key 中, 与给定域 field 相关联的值的字符串长度(string length)。
如果给定的键或者域不存在, 那么命令返回 0 。
127.0.0.1:6379> hset mybatis f1 "helloworld" f2 "99" f3 "-256"
(integer) 3
127.0.0.1:6379> hstrlen mybatis f1
(integer) 10
127.0.0.1:6379> hstrlen mybatis f2
(integer) 2
127.0.0.1:6379> hstrlen mybatis f3
(integer) 4
HINCRBY
为哈希表 key 中的域 field 的值加上增量 increment 。
增量也可以为负数,相当于对给定域进行减法操作。
如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。
如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。
对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。
本操作的值被限制在 64 位(bit)有符号数字表示之内。
127.0.0.1:6379> hexists counter page_view
(integer) 0
127.0.0.1:6379> hincrby counter page_view 200
(integer) 200
127.0.0.1:6379> hget counter page_view
"200"
HINCRBYFLOAT
为哈希表 key 中的域 field 加上浮点数增量 increment 。
如果哈希表中没有域 field ,那么 HINCRBYFLOAT 会先将域 field 的值设为 0 ,然后再执行加法操作。
如果键 key 不存在,那么 HINCRBYFLOAT 会先创建一个哈希表,再创建域 field ,最后再执行加法操作。
HMSET
同时将多个 field-value (域-值)对设置到哈希表 key 中。
此命令会覆盖哈希表中已存在的域。
如果 key 不存在,一个空哈希表被创建并执行 HMSET 操作。
127.0.0.1:6379> hmset website google google.com yahoo www.yahoo.com
OK
127.0.0.1:6379> hmget website google yahoo
1) "google.com"
2) "www.yahoo.com"
HMGET
返回哈希表 key 中,一个或多个给定域的值。
如果给定的域不存在于哈希表,那么返回一个 nil 值。
因为不存在的 key 被当作一个空哈希表来处理,所以对一个不存在的 key 进行 HMGET 操作将返回一个只带有 nil 值的表。
HKEYS
返回哈希表 key 中的所有域。
127.0.0.1:6379> hkeys website
1) "google"
2) "yahoo"
HVALS
返回哈希表 key 中所有域的值。
127.0.0.1:6379> hvals website
1) "google.com"
2) "www.yahoo.com"
HGETALL
返回哈希表 key 中,所有的域和值。
在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。
127.0.0.1:6379> hgetall website
1) "google"
2) "google.com"
3) "yahoo"
4) "www.yahoo.com"