1,hset 命令:
hset
redis 127.0.0.1:6379> hset user name 'zhangsan' # 设置值为zhangsan
(integer) 1
redis 127.0.0.1:6379> hset user name 'lisi' # 将值覆盖为lisi
(integer) 0
redis 127.0.0.1:6379>
2,hget 命令:
hget
redis 127.0.0.1:6379> hget user name
"lisi"
3,hsetnx 命令:
hsetnx
redis 127.0.0.1:6379> hget user name
"zhangsna"
redis 127.0.0.1:6379> hsetnx user name 'lisi'
(integer) 0 # 将name域的值设置为lisi时操作失败,因为name域已经存在
redis 127.0.0.1:6379> hsetnx user age 22 # 将age域的值设置为22,操作成功,应为age域不存在
(integer) 1
redis 127.0.0.1:6379> hget user name # 取出name的值,并没有被修改
"zhangsna"
redis 127.0.0.1:6379> hget user age
"22"
redis 127.0.0.1:6379>
4,hmset 命令:
hmset
redis 127.0.0.1:6379> hmset product name 'computer' price '3200' size '14inch'
OK
5,hmget 命令:
hmget
redis 127.0.0.1:6379> hmget product name price size contact
1) "computer" # name的值
2) "3200" # price的值
3) "14inch" # size的值
4) (nil) # contact的值在hash表中不存在,返回nil
6,hgetall 命令:
hgetall 返回hash表中key的所有域的值。
redis 127.0.0.1:6379> hgetall product
1) "name"
2) "computer"
3) "price"
4) "3200"
5) "size"
6) "14inch"
6,hlen 命令:
hlen 返回hash表中key中所有域的总数。当key值不存在时,返回0.
redis 127.0.0.1:6379> hlen product
(integer) 3 # product中域的数量为3
redis 127.0.0.1:6379> hlen amimal
(integer) 0 # 未定义amimal,所以返回的是0
6,hgetall 命令:
hgetall
7,hexists 命令:
hexists
redis 127.0.0.1:6379> hexists product name
(integer) 1 # product中存在name域
redis 127.0.0.1:6379> hexists product contact
(integer) 0 # product中不存在contact域
8,hkeys 命令:
hkeys
redis 127.0.0.1:6379> hkeys product
1) "name"
2) "price"
3) "size"
redis 127.0.0.1:6379> hkeys amimal
(empty list or set)
redis 127.0.0.1:6379>
9, hvals 命令:
hvals
redis 127.0.0.1:6379> hvals product
1) "computer"
2) "3200"
3) "14inch"
redis 127.0.0.1:6379> hvals amimal
(empty list or set)
10, hincrby 命令:
hincrby
redis 127.0.0.1:6379> hincrby product price 200
(integer) 3400 # price增加200
redis 127.0.0.1:6379> hincrby product final 3200
(integer) 3200 # 域final本不存在,初始化为0,然后增加3200
redis 127.0.0.1:6379> hincrby product price -100
(integer) 3300 # price减去100
11,hincrbyfloat 命令:
同上,不过步长可设为float
12,hdel 命令:
hdel
redis 127.0.0.1:6379> hgetall product
1) "name"
2) "computer"
3) "price"
4) "3300"
5) "size"
6) "14inch"
7) "final"
8) "3200"
redis 127.0.0.1:6379> hdel product size
(integer) 1
redis 127.0.0.1:6379> hgetall product
1) "name"
2) "computer"
3) "price"
4) "3300"
5) "final"
6) "3200"
redis 127.0.0.1:6379> hdel product final price
(integer) 2
redis 127.0.0.1:6379> hgetall product
1) "name"
2) "computer"
13,hlen
hlen hsetname: