Redis数据结构之哈希
目录
Redis数据结构之哈希
哈希类型是指键值对里的value本身存储的也是一个个的KV键值对,类似于python中的dict和java中的map集合。
写入
HSET
127.0.0.1:6379> HSET people name df sex null add diqiu
3
获取数据
HGET
127.0.0.1:6379> HGET people sex
null
127.0.0.1:6379> HGET people name
df
127.0.0.1:6379> HGET people add
diqiu
修改数据
HSET
127.0.0.1:6379> HSET people sex man
0
127.0.0.1:6379> HGET people sex
man
删除数据
HDEL
127.0.0.1:6379> HDEL people sex
1
127.0.0.1:6379> HGET people name
df
127.0.0.1:6379> HGET people sex
127.0.0.1:6379> HGET people add
diqiu
删除所有数据
DEL
127.0.0.1:6379> DEL people
1
127.0.0.1:6379> hget people name
127.0.0.1:6379> HGET people add
查看key中指定的field是否存在
HEXISTS
127.0.0.1:6379> HSET people name df sex null add diqiu
3
127.0.0.1:6379> HEXISTS people name
1
127.0.0.1:6379> HEXISTS people high
0
若value中没有相应的field ,则创建
HSETNX
127.0.0.1:6379> HGET people name
df
127.0.0.1:6379> HSETNX people name ffgg
0
127.0.0.1:6379> HGET people name
df
127.0.0.1:6379> HGET people high
127.0.0.1:6379> HSETNX people high 180
1
127.0.0.1:6379> HGET people high
180
获取多个值
HMGET
127.0.0.1:6379> HSET people name df sex null add huoxing
3
127.0.0.1:6379> HMGET people name sex add
df
null
huoxing
获取所有的key和value
HGETALL
127.0.0.1:6379> HGETALL people
name
df
sex
null
add
huoxing
获取所有的key
HKEYS
127.0.0.1:6379> HKEYS people
name
sex
add
获取所有的value
HVALS
127.0.0.1:6379> HVALS people
df
null
huoxing
计数
增加
HINCRBY
127.0.0.1:6379> HGET people id
1
127.0.0.1:6379> HINCRBY people id 1
2
127.0.0.1:6379> HINCRBY people id 1
3
127.0.0.1:6379> HINCRBY people id 1
4
减少
HINCRBY
127.0.0.1:6379> HGET people id
4
127.0.0.1:6379> HINCRBY people id -1
3
127.0.0.1:6379> HINCRBY people id -1
2
127.0.0.1:6379> HINCRBY people id -1
1
小数点
HINCRBYFLOAT
127.0.0.1:6379> HGET people id
1
127.0.0.1:6379> HINCRBYFLOAT people id 0.7
1.7
127.0.0.1:6379> HINCRBYFLOAT people id 0.7
2.4
127.0.0.1:6379> HINCRBYFLOAT people id 0.7
3.1
... ...
127.0.0.1:6379> HINCRBYFLOAT people id 10.9
128.57779999999999999 #处理精度不足
获取长度
HLEN
127.0.0.1:6379> HLEN people
4
#此时people有4对key value
获取某个字段的长度
HSTRLEN
127.0.0.1:6379> HGET people name
df
127.0.0.1:6379> HSTRLEN people name
2
设置过期时间
EXPIRE
#设置为10s后过期
127.0.0.1:6379> EXPIRE people 10
1
#此时查看发现已经过期
127.0.0.1:6379> TTL people
-2
#过期后再查看,无内容
127.0.0.1:6379> HGETALL people