Redis的Hash数据类型及常用命令

Redis的Hash数据类型及常用命令

hset存储数据

127.0.0.1:6379> hset myhash filed zhang
(integer) 1

HGET获取数据

127.0.0.1:6379> HGET myhash filed
"zhang"

HSET,hmset存储多个数据

HMGET获取多个数据

127.0.0.1:6379> HSET myhash h1 vi h2 v2 h3 v3
(integer) 3
127.0.0.1:6379> hmset myhash2 h1 v1 h2 v2 h3 v3
OK

127.0.0.1:6379> HMGET myhash h1 h2 h3
1) "vi"
2) "v2"
3) "v3"
127.0.0.1:6379> HMGET myhash2 h1 h2 h3
1) "v1"
2) "v2"
3) "v3"

HGETALL获取所有的key,vlaue

127.0.0.1:6379> HGETALL myhash2
1) "h1"	#key
2) "v1" #vlaue
3) "h2"
4) "v2"
5) "h3"
6) "v3"

HDEL删除指定的值

127.0.0.1:6379> HDEL myhash h1
(integer) 1
127.0.0.1:6379> HGETALL myhash
1) "h2"
2) "v2"
3) "h3"
4) "v3"

HLEN获取hash的长度

127.0.0.1:6379> HLEN myhash
(integer) 2

HEXISTS判断指定的key是否存在

127.0.0.1:6379> HEXISTS myhash h2
(integer) 1
127.0.0.1:6379> HEXISTS myhash h1
(integer) 0

只获得key或只获得vlaue

127.0.0.1:6379> HKEYS myhash
1) "h2"
2) "h3"
127.0.0.1:6379> HVALS myhash
1) "v2"
2) "v3"

HINCRBY加上指定的值,减上指定的值

127.0.0.1:6379> hset myhash number 5
(integer) 1
127.0.0.1:6379> HINCRBY number 5
(error) ERR wrong number of arguments for 'hincrby' command
127.0.0.1:6379> HINCRBY myhash number 2
(integer) 7
127.0.0.1:6379> HINCRBY myhash number -4
(integer) 3

HSETNX如果没有则创建数据,有则不创建

127.0.0.1:6379> HSETNX myhash number 7
(integer) 0
127.0.0.1:6379> HSETNX myhash number1 7
(integer) 1

学习参考狂神说java

上一篇:乙烯和苯:知识预习


下一篇:博客园添加多级目录