Redis hashes存储的操作方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis hashes存储的操作方法相关的知识,希望对你有一定的参考价值。
上篇我们讲到string的操作方法,其实hashes与其类似,只不过命令格式不同而已。
使用redis-cli进入redis:
[[email protected] ~]# redis-cli 127.0.0.1:6379>
1、添加
a>hset
方法:hset key filed value
描述:与set一样,不存在增加,否则更新返回1表示新的filed,0表示filed已存在并用new value覆盖old value
127.0.0.1:6379> hset user:0001 name Sunshine (integer) 1 127.0.0.1:6379> hget user:0001 name "Sunshine" 127.0.0.1:6379> hset user:0001 name SunshineBoy (integer) 0 127.0.0.1:6379> hget user:0001 name "SunshineBoy"
b>hsetnx
方法:hsetnx key filed value
描述:与setnx一样,filed为指定value,如果key不存在先创建,否则返回0
127.0.0.1:6379> hsetnx user:0002 name Sunshineone (integer) 1 127.0.0.1:6379> hget user:0002 name "Sunshineone" 127.0.0.1:6379> hsetnx user:0002 name Sunshinetwo (integer) 0 127.0.0.1:6379> hget user:0002 name "Sunshineone"
c>hmset
方法:hmset key filed value [key filed value...]
描述:与mset一样,成功返回OK,如果filed存在就更新
127.0.0.1:6379> hmset user:003 name Sunshine age 25 OK 127.0.0.1:6379> hmset user:003 name Sunshine age 23 OK 127.0.0.1:6379> hmget user:003 name age 1) "Sunshine" 2) "23"
2、查看
a>hget
方法:hget key filed
描述:与get一样,查看key的filed的value
127.0.0.1:6379> hget user:0002 name "Sunshineone"
b>hmget
方法:hmget key filed [filed2...]
127.0.0.1:6379> hmget user:003 name 1) "Sunshine" 127.0.0.1:6379> hmget user:003 name age 1) "Sunshine" 2) "23"
d>hlen
方法: hlen key
描述:查看key有多个filed
127.0.0.1:6379> hkeys user:003 1) "name" 2) "age" 127.0.0.1:6379> hlen user:003 (integer) 2 127.0.0.1:6379> hkeys user:0001 1) "name" 127.0.0.1:6379> hlen user:0001 (integer) 1
e>hkeys
方法:hkey key
描述:查看并显示key的filedname
127.0.0.1:6379> hkeys user:0001 1) "name" 127.0.0.1:6379> hkeys user:0002 1) "name" 127.0.0.1:6379> hkeys user:003 1) "name" 2) "age" 127.0.0.1:6379> hkeys key:1 1) "value"
f>hvals
方法:hvals key
描述:插卡并电视key的filed值
127.0.0.1:6379> hvals user:0001 1) "SunshineBoy" 127.0.0.1:6379> hvals user:0002 1) "Sunshineone" 127.0.0.1:6379> hvals user:003 1) "Sunshine" 2) "23" 127.0.0.1:6379> hvals key:1 1) "20"
g>hgetall
方法:hgetall key
描述:查看并显示key的filedname及filedvalue
127.0.0.1:6379> hgetall user:0001 1) "name" 2) "SunshineBoy" 127.0.0.1:6379> hgetall user:0002 1) "name" 2) "Sunshineone" 127.0.0.1:6379> hgetall user:003 1) "name" 2) "Sunshine" 3) "age" 4) "23" 127.0.0.1:6379> hgetall key:1 1) "value" 2) "20"
3、修改
a>hincry
方法:方法与incrby一样,hashes没有递增递减
127.0.0.1:6379> hset key:1 value 20 (integer) 1 127.0.0.1:6379> hget key:1 value "20" 127.0.0.1:6379> hincrby key:1 value 10 (integer) 30 127.0.0.1:6379> hget key:1 value "30" 127.0.0.1:6379> hincrby key:1 value -10 (integer) 20 127.0.0.1:6379> hget key:1 value "20"
b>hincrbyfloat
方法:hincrbyfloat key filed incr_by_number
描述:用于浮点数,如果存在则递增递减,否则初始化值为0并设定当前值,下面为官方解释
Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。
如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。
127.0.0.1:6379> hincrbyfloat key:2 value 2.6 "2.6" 127.0.0.1:6379> hget key:2 value "2.6" 127.0.0.1:6379> hincrbyfloat key:2 value 1.3 "3.9" 127.0.0.1:6379> hget key:2 value "3.9" 127.0.0.1:6379> hincrbyfloat key:2 value -1.3 "2.6" 127.0.0.1:6379> hget key:2 value "2.6"
4、测试
a>hexists
方法:hexists key filed
描述:如果存在返回1,否则返回0
127.0.0.1:6379> hexists key:1 value1 (integer) 0 127.0.0.1:6379> hexists key:2 value (integer) 0 127.0.0.1:6379> hexists key:1 value (integer) 1
至此Redis Heashes就介绍完毕啦
欢迎大家关注 459479177QQ群
本文出自 “12087328” 博客,请务必保留此出处http://12097328.blog.51cto.com/12087328/1856356
以上是关于Redis hashes存储的操作方法的主要内容,如果未能解决你的问题,请参考以下文章