hset [key] [filed1] [value1] … [fieldn] [valuen]
将一个或多个field-value对设置到哈希表中,此命令会覆盖哈希表中已存在的字段,返回新创建的字段数量.
Redis 2.4 版本以前只能写入一个字段
127.0.0.1:6379> hset hset-key a 1 b 2
(integer) 2
127.0.0.1:6379> hset hset-key a 3 b 4 c 5
(integer) 1
127.0.0.1:6379> hgetall hset-key
1) "a"
2) "3"
3) "b"
4) "4"
5) "c"
6) "5"
hsetnx [key] [field] [value]
为哈希表中不存在的字段赋值,字段如果已经存在则操作无效,操作成功返回1,失败返回0,如果key不存在,则会创建一个新哈希表.
每次只能操作一个字段
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "5"
127.0.0.1:6379> hsetnx hset-key a 3
(integer) 1
127.0.0.1:6379> hsetnx hset-key c 6
(integer) 0
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "5"
3) "a"
4) "3"
hget [key] [field]
返回哈希表中某个字段的值,如果字段不存在,则返回nil
127.0.0.1:6379> hgetall hset-key
1) "a"
2) "3"
3) "b"
4) "4"
5) "c"
6) "5"
127.0.0.1:6379> hget hset-key a
"3"
127.0.0.1:6379> hget hset-key e
(nil)
hmset [key] [filed1] [value1] … [fieldn] [valuen]
将一个或多个field-value对设置到哈希表中,此命令会覆盖哈希表中已存在的字段,如果哈希表不存在则会创建一个空哈希表.如果执行成功,返回OK.
127.0.0.1:6379> hmset hmset-key a 1 b 2 c 4 d 4 e 5
OK
hmget [key] [field1] [filed2] … [fieldn]
返回哈希表中一个或多个字段的值,如果字段不存在,则返回nil
127.0.0.1:6379> hmget hmset-key a b c d e
1) "1"
2) "2"
3) "4"
4) "4"
5) "5"
127.0.0.1:6379> hmget hmset-key f
1) (nil)
hkeys [key]
获取哈希表中的所有字段,key不存在时返回一个空表
127.0.0.1:6379> hkeys hmset-key
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
hvals [key]
获取哈希表中的所有值,key不存在时返回一个空表
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "5"
3) "a"
4) "3"
127.0.0.1:6379> hvals hset-key
1) "5"
2) "3"
127.0.0.1:6379> hvals hset-key a
(error) ERR wrong number of arguments for 'hvals' command
127.0.0.1:6379> hvals hset-key c
(error) ERR wrong number of arguments for 'hvals' command
127.0.0.1:6379> exists hset-key2
(integer) 0
127.0.0.1:6379> hvals hset-key2
(empty list or set)
hexists [key] [field]
判断哈希表中是否有某个字段,存在返回1,不存在或者key为空返回0
127.0.0.1:6379> hkeys hmset-key
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> hexists hmset-key a
(integer) 1
127.0.0.1:6379> hexists hmset-key f
(integer) 0
# key不存在
127.0.0.1:6379> exists hmset-key2
(integer) 0
127.0.0.1:6379> hexists hmset-key2 a
(integer) 0
hgetall [key]
获取哈希表中的所有字段和值,以列表形式返回结果,如果key不存在,返回空列表
127.0.0.1:6379> hgetall hmset-key
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "4"
7) "d"
8) "4"
9) "e"
10) "5"
hlen [key]
返回哈希表中字段的数量,key不存在时返回0
127.0.0.1:6379> hgetall hset-key
1) "a"
2) "3"
3) "b"
4) "4"
5) "c"
6) "5"
127.0.0.1:6379> hlen hset-key
(integer) 3
# key不存在
127.0.0.1:6379> exists hset-key2
(integer) 0
127.0.0.1:6379> hlen hset-key2
(integer) 0
hdel [key] [field1] [field2] … [fieldn]
删除哈希表中一个或多个字段,不存在的字段将会被忽略,返回成功删除的字段数量
127.0.0.1:6379> hgetall hset-key
1) "a"
2) "3"
3) "b"
4) "4"
5) "c"
6) "5"
127.0.0.1:6379> hdel hset-key a b f g
(integer) 2
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "5"
hincrby [key] [field] [increment]
为指定哈希表中的指定字段的值增加increment,返回增长后的值
increment 可以为负数
哈希表不存在时会先创建一个哈希表
字段不存在时会添加字段,值初始化为0
如果字段存储的值是字符串,将返回一个错误
本操作的值被限制在64位(bit)有符号数字表示之内
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "8"
3) "a"
4) "3"
5) "b"
6) "bbb"
127.0.0.1:6379> hincrby hset-key a 5
(integer) 8
127.0.0.1:6379> hincrby hset-key a -1
(integer) 7
127.0.0.1:6379> hincrby hset-key d 1
(integer) 1
127.0.0.1:6379> hincrby hset-key b 1
(error) ERR hash value is not an integer
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "8"
3) "a"
4) "7"
5) "b"
6) "bbb"
7) "d"
8) "1"
hincrbyfloat [key] [field] [increment]
为指定哈希表中的指定字段的值增加increment,返回增长后的值
increment 是浮点数增量值,可以为负数
哈希表不存在时会先创建一个哈希表
字段不存在时会添加字段,值初始化为0
如果字段存储的值是字符串,将返回一个错误
本操作的值被限制在64位(bit)有符号数字表示之内
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "8"
3) "a"
4) "7"
5) "b"
6) "bbb"
7) "d"
8) "1"
127.0.0.1:6379> hincrbyfloat hset-key a 1.5
"8.5"
127.0.0.1:6379> hincrbyfloat hset-key c -1.5
"6.5"
127.0.0.1:6379> hincrbyfloat hset-key f -1.5
"-1.5"
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "6.5"
3) "a"
4) "8.5"
5) "b"
6) "bbb"
7) "d"
8) "1"
9) "f"
10) "-1.5"
127.0.0.1:6379> hincrbyfloat hset-key b -1.5
(error) ERR hash value is not a float
hscan [key] [cursor] match [pattern] count [count]
迭代哈希表中的元素
此命令测试时,count参数无效,无论count参数是几,都会返回全部匹配的数据
127.0.0.1:6379> hgetall hset-key
1) "c"
2) "6.5"
3) "a"
4) "8.5"
5) "b"
6) "bbb"
7) "d"
8) "1"
9) "f"
10) "-1.5"
11) "aaa"
12) "888"
127.0.0.1:6379> hscan hset-key 0
1) "0"
2) 1) "c"
2) "6.5"
3) "a"
4) "8.5"
5) "b"
6) "bbb"
7) "d"
8) "1"
9) "f"
10) "-1.5"
11) "aaa"
12) "888"
127.0.0.1:6379> hscan hset-key 0 match a
1) "0"
2) 1) "a"
2) "8.5"
127.0.0.1:6379> hscan hset-key 0 match a*
1) "0"
2) 1) "a"
2) "8.5"
3) "aaa"
4) "888"
127.0.0.1:6379> hscan hset-key 0 match a* count 1
1) "0"
2) 1) "a"
2) "8.5"
3) "aaa"
4) "888"