天天看点

redis hash命令操作

/*********redis hash命令操作 **********/

1.删除一个或多个hash域

hdel key field [field ...]

summary: delete one or more hash fields

since: 2.0.0

127.0.0.1:6379> hdel hash1 name

(integer) 1

2.判断一个域是否存在

hexists key field

summary: determine if a hash field exists

127.0.0.1:6379> hexists hash1 name

(integer) 0 #不存在返回0

127.0.0.1:6379> hexists hash1 name2

(integer) 1 #存在返回1

3.获取一个hash域

hget key field

summary: get the value of a hash field

127.0.0.1:6379> hget hash1 name2

"name22"

4.获取hash中所有的域和值

hgetall key

summary: get all the fields and values in a hash

127.0.0.1:6379> hgetall hash1

1) "name"  #field名

2) "jing"  #value值

3) "name2" #field名

4) "jing2" #value值

5) "name3" #field名

6) "jing3" #value值

5.增加域的值(如果是int型),增加increment

hincrby key field increment

summary: increment the integer value of a hash field by the given number

6.增加域的值(float型),增加increment

hincrbyfloat key field increment

summary: increment the float value of a hash field by the given amount

since: 2.6.0

127.0.0.1:6379> hset hash1 num1 10

127.0.0.1:6379> hincrby hash1 num1 2

(integer) 12

7.获取hash的所有域

hkeys key

summary: get all the fields in a hash

127.0.0.1:6379> hkeys hash1

1) "name2"

2) "name3"

3) "num1"

8.获取hash中域的数据

hlen key

summary: get the number of fields in a hash

127.0.0.1:6379> hlen hash1

(integer) 3

9.获取所有给定域的值

hmget key field [field ...]

summary: get the values of all the given hash fields

127.0.0.1:6379> hmget hash1 name2 num1

1) "name22"

2) "12"

10.设置多个hash域对应多个hash值

hmset key field value [field value ...]

summary: set multiple hash fields to multiple values

127.0.0.1:6379> hmset hash1 num2 12 name4 jing4 num3 33

ok

 1) "name2"

 2) "name22"

 3) "name3"

 4) "name33"

 5) "num1"

 6) "12"

 7) "num2"

 8) "12"

 9) "name4"

10) "jing4"

11) "num3"

12) "33"

11.设置hash域的字符串值

hset key field value

summary: set the string value of a hash field

12.只有当该域不存在时,设置hash的域的值

hsetnx key field value

summary: set the value of a hash field, only if the field does not exist

13.获取hash的所有值

hvals key

summary: get all the values in a hash

127.0.0.1:6379> hvals hash1

2) "name33"

3) "12"

4) "12"

5) "jing4"

6) "33"

继续阅读