首页 > 代码库 > Redis哈希-hash

Redis哈希-hash

Redis的hash类型数据存储极为重要

hset K V  赋值一个hash 其中V为 (key, value)

127.0.0.1:6379> hset user id 1
(integer) 1
127.0.0.1:6379> hset user name z3
(integer) 1
127.0.0.1:6379>

 

hget 获取hash值

127.0.0.1:6379> hget user id
"1"
127.0.0.1:6379> hget user name
"z3"
127.0.0.1:6379>

 

hmset/hmget 一次可以批量存取

127.0.0.1:6379> hmset cumtomer name z3 age 11 id 1
OK
127.0.0.1:6379> hmget cumtomer name age id
1) "z3"
2) "11"
3) "1"
127.0.0.1:6379>

 

 

hgetall  可以取出所有的key-value

127.0.0.1:6379> hgetall cumtomer
1) "name"
2) "z3"
3) "age"
4) "11"
5) "id"
6) "1"
127.0.0.1:6379>

 

 

hdel 删除一个值

127.0.0.1:6379> hdel user name
(integer) 1
127.0.0.1:6379>

 

hlen 可以获取长度

127.0.0.1:6379> hlen user
(integer) 1
127.0.0.1:6379> hlen cumtomer
(integer) 3
127.0.0.1:6379>

 

 

hexists key 在key里面的某个值的key (判断某个key是否存在)

127.0.0.1:6379> HEXISTS cumtomer id
(integer) 1
127.0.0.1:6379> hexists cumtomer name
(integer) 1
127.0.0.1:6379> hexists cumtomer email
(integer) 0
127.0.0.1:6379>

 

hkeys/hvals 取出所有的key/value

127.0.0.1:6379> hkeys cumtomer
1) "name"
2) "age"
3) "id"
127.0.0.1:6379> hvals cumtomer
1) "z3"
2) "11"
3) "1"
127.0.0.1:6379>

 

hincrby/hincrbyfloat

127.0.0.1:6379> HINCRBY cumtomer age 2
(integer) 13
127.0.0.1:6379> HINCRBY cumtomer age 2
(integer) 15
127.0.0.1:6379> HINCRBY cumtomer age 2
(integer) 17
127.0.0.1:6379> hset cumtomer score 91.5
(integer) 1
127.0.0.1:6379> HINCRBYFLOAT cumtomer score 0.5
"92"
127.0.0.1:6379> HINCRBYFLOAT cumtomer score 0.5
"92.5"
127.0.0.1:6379>

 

hsetnx 如果不存在则添加,如果存在不改变

127.0.0.1:6379> hsetnx cumtomer age 26
(integer) 0
127.0.0.1:6379> hsetnx cumtomer email abc@email.com
(integer) 1
127.0.0.1:6379>

 

Redis哈希-hash