hash类型详细讲解

hash类型详细讲解

setget还有del

# hset 添加key-value
# hmset 批量添加key-value
# hget 获取单个字段
# hmget 获取多个字段
# hgetall 获取hash全部值
127.0.0.1:6379[3]> flushdb
OK
127.0.0.1:6379[3]> hset myhash name ohmydream
(integer) 1
127.0.0.1:6379[3]> hset myhash age 29
(integer) 1
127.0.0.1:6379[3]> hget myhash age
"29"
127.0.0.1:6379[3]> hgetall myhash
1) "name"
2) "ohmydream"
3) "age"
4) "29"
127.0.0.1:6379[3]> hmset myhash address xiᄀᆵan email xxxx@qq.com
OK
127.0.0.1:6379[3]> hgetall myhash
1) "name"
2) "ohmydream"
3) "age"
4) "29"
5) "address"
6) "xi\xa1\xafan"
7) "email"
8) "xxxx@qq.com"

获取长度

# hlen
127.0.0.1:6379[3]> hgetall myhash
1) "name"
2) "ohmydream"
3) "age"
4) "29"
5) "address"
6) "xi\xa1\xafan"
127.0.0.1:6379[3]> hlen myhash
(integer) 3

判断是否存在

# 判断字段是否存在
127.0.0.1:6379[3]> hexists myhash name
(integer) 1
127.0.0.1:6379[3]> hexists myhash email
(integer) 0

获取所有的字段和值

# hkeys 获取是所有的field
# hvals 获取所有的值
127.0.0.1:6379[3]> hkeys myhash
1) "name"
2) "age"
3) "address"
127.0.0.1:6379[3]> hvals myhash
1) "ohmydream"
2) "29"
3) "xi\xa1\xafan"

值增加/不存在创建

# hincrby 指定增量
# hsetnx 不存在则创建
127.0.0.1:6379[3]> hset myhash number 1
(integer) 1
127.0.0.1:6379[3]> hincrby myhash number 3
(integer) 4
127.0.0.1:6379[3]> hget myhash number
"4"
127.0.0.1:6379[3]> hdecrby myhash number 1
(error) ERR unknown command `hdecrby`, with args beginning with: `myhash`, `number`, `1`,
127.0.0.1:6379[3]> hsetnx myhash number 100
(integer) 0
127.0.0.1:6379[3]> hsetnx myhash number1 100
(integer) 1

应用:

用来存储变更的数据 用户信息。hash更适用于对象的存储,String更适合字符串的存储

posted @ 2022-02-24 21:02  Oh,mydream!  阅读(120)  评论(0编辑  收藏  举报