文章目录
- Redis之hash类型
- 1. 设置一个字段/获取一个字段
- 2. 获取所有字段值
- 3. 判断字段是否存在
- 4. 设置多个字段/获取多个字段
- 5. 只获取字段名/字段值
- 6. 获取某个key内全部数量
- 7. 增加数字
- 8. 删除key内字段
- 9. 字段不存在时赋值
- 10. 应用场景
Redis之hash类型
redis的hash类型,就像Java中的Map<String, Map<Object,Object>>
1. 设置一个字段/获取一个字段
设置一个字段:HSET key field value
获取一个字段:HGET key field
127.0.0.1:6379> hset user id 1
(integer) 1
127.0.0.1:6379> hget user id
"1"
2. 获取所有字段值
HGETALL key
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
3. 判断字段是否存在
HEXISTS key field
存在返回1,不存在返回0.
127.0.0.1:6379> hexists user id
(integer) 1
127.0.0.1:6379> hexists user name
(integer) 0
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
4. 设置多个字段/获取多个字段
设置多个字段:HMSET key field value [field value …]
获取多个字段:HMGET key field [field …]
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
127.0.0.1:6379> hmset user name zhangsan address xian score 99
OK
127.0.0.1:6379> hmget user id name
1) "1"
2) "zhangsan"
127.0.0.1:6379> hmget user id name address score
1) "1"
2) "zhangsan"
3) "xian"
4) "99"
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
3) "name"
4) "zhangsan"
5) "address"
6) "xian"
7) "score"
8) "99"
5. 只获取字段名/字段值
获取字段名:HKEYS key
获取字段值:HVALS key
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
3) "name"
4) "zhangsan"
5) "address"
6) "xian"
7) "score"
8) "99"
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "address"
4) "score"
127.0.0.1:6379> hvals user
1) "1"
2) "zhangsan"
3) "xian"
4) "99"
127.0.0.1:6379> hvals person
(empty list or set)
127.0.0.1:6379> hkeys person
(empty list or set)
6. 获取某个key内全部数量
HLEN key
127.0.0.1:6379> hgetall user
1) "id"
2) "1"
3) "name"
4) "zhangsan"
5) "address"
6) "xian"
7) "score"
8) "99"
127.0.0.1:6379> hlen user
(integer) 4
7. 增加数字
HINCRBY key field increment
127.0.0.1:6379> hget user id
"1"
127.0.0.1:6379> hincrby user id 5
(integer) 6
127.0.0.1:6379> hget user id
"6"
8. 删除key内字段
HDEL key field
127.0.0.1:6379> hgetall user
1) "id"
2) "6"
3) "name"
4) "zhangsan"
5) "address"
6) "xian"
7) "score"
8) "99"
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "address"
4) "score"
127.0.0.1:6379> hdel user address
(integer) 1
127.0.0.1:6379> hgetall user
1) "id"
2) "6"
3) "name"
4) "zhangsan"
5) "score"
6) "99"
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "score"
9. 字段不存在时赋值
HSETNX key field value
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "score"
127.0.0.1:6379> hsetnx user address xian
(integer) 1
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "score"
4) "address"
127.0.0.1:6379> hvals user
1) "6"
2) "zhangsan"
3) "99"
4) "xian"
127.0.0.1:6379> hgetall user
1) "id"
2) "6"
3) "name"
4) "zhangsan"
5) "score"
6) "99"
7) "address"
8) "xian"
127.0.0.1:6379> hsetnx user score 98
(integer) 0
127.0.0.1:6379> hgetall user
1) "id"
2) "6"
3) "name"
4) "zhangsan"
5) "score"
6) "99"
7) "address"
8) "xian"
10. 应用场景
- 购物车
用户id为100的用户购物车,买了商品item1 1件, 商品item2 1件,然后增加商品item2 到4件,查看用户总共买了多少商品,每个商品分别是多少。
127.0.0.1:6379> hset shopcar:uid100 item1 1
(integer) 1
127.0.0.1:6379> hset shopcar:uid100 item2 1
(integer) 1
127.0.0.1:6379> hincrby shopcar:uid100 item2 3
(integer) 4
127.0.0.1:6379> hlen shopcar:uid100
(integer) 2
127.0.0.1:6379> hgetall shopcar:uid100
1) "item1"
2) "1"
3) "item2"
4) "4"