Redis数据结构之hashes

存储hash,String Key 和 String Value的map容器

hset

127.0.0.1:6379> hset myhash name tom
(integer) 1
127.0.0.1:6379> keys *
1) "myhash"
127.0.0.1:6379> type myhash
hash

hget

127.0.0.1:6379> hget myhash name
"tom"
127.0.0.1:6379> hget myhash username
(nil)

hgetall

127.0.0.1:6379> hset myhash age 18
(integer) 1
127.0.0.1:6379> hget myhash age
"18"
127.0.0.1:6379> hgetall myhash
1) "name"
2) "tom"
3) "age"
4) "18"

hkeys hvals

127.0.0.1:6379> hkeys myhash
1) "name"
2) "age"
127.0.0.1:6379> hvals myhash
1) "tom"
2) "18"

hlen

获取hash里面所有字段的数量

127.0.0.1:6379> hlen myhash
(integer) 2

hmset

127.0.0.1:6379> hmset myhash username tom age 19
OK
127.0.0.1:6379> keys *
1) "myhash"
127.0.0.1:6379> type myhash
hash
127.0.0.1:6379> hkeys myhash
1) "username"
2) "age"
127.0.0.1:6379> hvals myhash
1) "tom"
2) "19"

hdel

127.0.0.1:6379> hdel myhash age
(integer) 1
127.0.0.1:6379> hkeys myhash
1) "username"

hsetnx

设置hash的一个字段,只有这个字段不存时有效

127.0.0.1:6379> hsetnx myhash username jerry
(integer) 0
127.0.0.1:6379> hget myhash username
"tom"
127.0.0.1:6379> hsetnx myhash age 20
(integer) 1
127.0.0.1:6379> hkeys myhash
1) "username"
2) "age"
127.0.0.1:6379> hvals myhash
1) "tom"
2) "20"

hincrby

127.0.0.1:6379> hget myhash age
"20"
127.0.0.1:6379> hincrby myhash age 5
(integer) 25
127.0.0.1:6379> hget myhash age
"25"

hexists

判断field是否存在于hash中,存在返回1,不存在返回0

127.0.0.1:6379> hexists myhash username
(integer) 1
127.0.0.1:6379> hexists myhash password
(integer) 0

你可能感兴趣的:(redis)