1. hash哈希类型的操作
1.1. hset key field value
语法:hset key field value
作用:把key中field域的值设为value
注:如果没有field域,直接添加;如果有,则覆盖原field域的值
例:
127.0.0.1:6379> hset user1 name lisi
(integer) 1
127.0.0.1:6379> hset user1 age 22
(integer) 1
1.2. hget key field
语法:hget key field
作用:返回key中field域的值
127.0.0.1:6379> hset user1 name lisi
(integer) 1
127.0.0.1:6379> hset user1 age 22
(integer) 1
127.0.0.1:6379> hget user1 name
"lisi"
127.0.0.1:6379> hget user1 age
"22"
1.3. hmset key field value [field value ...]
语法:hmset key field value [field value ...]
作用:设置域(field1 ... fieldn)及其对应的值(value1 ... valuen)
返回值:ok
例:
127.0.0.1:6379> hmset user2 name wangwu age 25
OK
1.4. hmget key field [field ...]
语法:hmget key field [field ...]
作用:返回key中域field1 field2 ... 的值
返回值:存在返回对应的值;不存在返回nil
例:
127.0.0.1:6379> hmset user2 name wangwu age 25
OK
127.0.0.1:6379> hmget user2 name age
1) "wangwu"
2) "25"
127.0.0.1:6379> hmget user2 name age sex
1) "wangwu"
2) "25"
3) (nil)
1.5. hgetall key
语法:hgetall key
作用:返回key中所有域与其值
例:
127.0.0.1:6379> hset user1 name lisi
(integer) 1
127.0.0.1:6379> hset user1 age 22
(integer) 1
127.0.0.1:6379> hgetall user1
1) "name"
2) "lisi"
3) "age"
4) "22"
127.0.0.1:6379> hmset user2 name wangwu age 25
OK
127.0.0.1:6379> hgetall user2
1) "name"
2) "wangwu"
3) "age"
4) "25"
1.6. hdel key field [field ...]
语法:hdel key field [field ...]
作用:删除key中field域
返回值:真正删除掉的field(域)的个数
例:
127.0.0.1:6379> hgetall user2
1) "name"
2) "wangwu"
3) "age"
4) "25"
127.0.0.1:6379> hdel user2 age
(integer) 1
127.0.0.1:6379> hgetall user2
1) "name"
2) "wangwu"
127.0.0.1:6379> hdel user2 haha
(integer) 0
127.0.0.1:6379> hmset user2 name tom age 23 score 99
OK
127.0.0.1:6379> hgetall user2
1) "name"
2) "tom"
3) "age"
4) "23"
5) "score"
6) "99"
127.0.0.1:6379> hdel user2 name age score
(integer) 3
127.0.0.1:6379> hmset user2 name tom age 23 score 99
OK
127.0.0.1:6379> hdel user2 name age score haha
(integer) 3
1.7. hlen key
语法:hlen key
作用:返回key中域(field)的个数
注:不存在的key返回0
例:
127.0.0.1:6379> hmset user2 name tom age 23 score 99
OK
127.0.0.1:6379> hlen user2
(integer) 3
127.0.0.1:6379> hmset user3 name lisa age 21
OK
127.0.0.1:6379> hlen user3
(integer) 2
1.8. hexists key field
语法:hexists key field
作用:判断key中是否存在field域
返回值:存在返回1;不存在返回0
例:
127.0.0.1:6379> hmset user3 name lisa age 21
OK
127.0.0.1:6379> hexists user3 name
(integer) 1
127.0.0.1:6379> hexists user3 score
(integer) 0
1.9. hkeys key
语法:hkeys key
作用:返回key的所有域(field)
例:
127.0.0.1:6379> hgetall user3
1) "name"
2) "lisa"
3) "age"
4) "21"
5) "score"
6) "90"
127.0.0.1:6379> hkeys user3
1) "name"
2) "age"
3) "score"
1.10. hvals key
语法:hvals key
作用:返回key的所有域(field)的值(value)
例:
127.0.0.1:6379> hgetall user3
1) "name"
2) "lisa"
3) "age"
4) "21"
5) "score"
6) "90"
127.0.0.1:6379> hvals user3
1) "lisa"
2) "21"
3) "92.5"