Redis Hash类型数据常用命令总结

来源:互联网 发布:腾讯视频链接不上网络 编辑:程序博客网 时间:2024/05/21 12:48

Redis Hash类型数据常用命令总结


Hash是一种数据结构,一般翻译做“散列”,也有直接音译为“哈希”。Redis  hash  是一个string类型的field和value的映射表。它特别适合用于存储对象。同将对象的每个字段存成单个string类型,存储为hash类型会占用更少的内存,并且方便的存取整个对象。

下面是关于hash类型存储的一些常用方法(命令):

1,hset 命令:

hset   <key>   <field>    <value>     将hash表中key  的 field域设置为值value。如果key值不存在,操作成功后返回1,如果key值已经存在,则覆盖原来的值,成功后返回0。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hset user name  'zhangsan'          # 设置值为zhangsan  
  2. (integer) 1  
  3. redis 127.0.0.1:6379> hset user name  'lisi'              # 将值覆盖为lisi  
  4. (integer) 0  
  5. redis 127.0.0.1:6379>  
2,hget  命令:

hget  <key>   <field>    返回hash表中指定key的field的值。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hget user name  
  2. "lisi"  
3,hsetnx 命令:

hsetnx    <key>  <field>   <value>    当且紧当filed域不存在时,设置值为value。如果该域不存在,返回1,如果该域已经存在,则不会执行操作,且返回0。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hget user name  
  2. "zhangsna"  
  3. redis 127.0.0.1:6379> hsetnx user name 'lisi'  
  4. (integer) 0                                          # 将name域的值设置为lisi时操作失败,因为name域已经存在  
  5. redis 127.0.0.1:6379> hsetnx user age  22<span style="white-space:pre">  </span>     # 将age域的值设置为22,操作成功,应为age域不存在  
  6. (integer) 1  
  7. redis 127.0.0.1:6379> hget user name                 # 取出name的值,并没有被修改  
  8. "zhangsna"  
  9. redis 127.0.0.1:6379> hget user age  
  10. "22"  
  11. redis 127.0.0.1:6379>  
4,hmset 命令:

hmset    <key>   <field>   <value>  [<field>   <value>...]  同时将多个“域-值”对存储在key键中,如果key不存在会自动创建,如果field已经存在,则会覆盖原来的值。操作成功后返回值OK。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hmset product name 'computer' price '3200' size '14inch'  
  2. OK  
5,hmget 命令:

hmget   <key>   <field>  [<field>....]  返回hash表中key的一个或者多个域值。如果给定的域名称在此hash表中不存在,则返回nil。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hmget product name price size contact  
  2. 1) "computer"           # name的值  
  3. 2) "3200"               # price的值  
  4. 3) "14inch"             # size的值  
  5. 4) (nil)                # contact的值在hash表中不存在,返回nil  
6,hgetall 命令:

hgetall   <key>    返回hash表中key的所有域的值。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hgetall product  
  2. 1) "name"  
  3. 2) "computer"  
  4. 3) "price"  
  5. 4) "3200"  
  6. 5) "size"  
  7. 6) "14inch"  
6,hlen 命令:

hlen   <key>  返回hash表中key中所有域的总数。当key值不存在时,返回0.

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hlen product  
  2. (integer) 3          # product中域的数量为3  
  3. redis 127.0.0.1:6379> hlen amimal  
  4. (integer) 0          # 未定义amimal,所以返回的是0  
7,hexists 命令:

hexists  <key>   <field>    查看hash表中,给定key的域field是否存在。如果存在,则返回1,如果field不存在或者是key也不存在,返回0。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hexists product name  
  2. (integer) 1                             # product中存在name域  
  3. redis 127.0.0.1:6379> hexists product contact  
  4. (integer) 0             # product中不存在contact域  
8,hkeys  命令:

hkeys  <key>    返回所有hash表中的key的所有域。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hkeys product  
  2. 1) "name"  
  3. 2) "price"  
  4. 3) "size"  
  5. redis 127.0.0.1:6379> hkeys amimal  
  6. (empty list or set)  
  7. redis 127.0.0.1:6379>  
9, hvals 命令:

hvals   <key>  返回所有hash表中的key的所有值。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hvals product  
  2. 1) "computer"  
  3. 2) "3200"  
  4. 3) "14inch"  
  5. redis 127.0.0.1:6379> hvals amimal  
  6. (empty list or set)  
10, hincrby 命令:

hincrby  <key>    <field>   <increment>   为哈希表 key 中的域 field 的值加上增量 increment 。增量也可以为负数,相当于对给定域进行减法操作。如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hincrby product price 200  
  2. (integer) 3400                              # price增加200  
  3. redis 127.0.0.1:6379> hincrby product final 3200  
  4. (integer) 3200                              # 域final本不存在,初始化为0,然后增加3200  
  5. redis 127.0.0.1:6379> hincrby product price -100  
  6. (integer) 3300                              # price减去100  
11,hdel  命令:

hdel   <key>  <field>  [<field>...]  删除hash表中key的一个或者多个域的值。如果指定的field不存在,则忽略操作。

[plain] view plain copy print?
  1. redis 127.0.0.1:6379> hgetall product  
  2. 1) "name"  
  3. 2) "computer"  
  4. 3) "price"  
  5. 4) "3300"  
  6. 5) "size"  
  7. 6) "14inch"  
  8. 7) "final"  
  9. 8) "3200"  
  10. redis 127.0.0.1:6379> hdel product size  
  11. (integer) 1  
  12. redis 127.0.0.1:6379> hgetall product  
  13. 1) "name"  
  14. 2) "computer"  
  15. 3) "price"  
  16. 4) "3300"  
  17. 5) "final"  
  18. 6) "3200"  
  19. redis 127.0.0.1:6379> hdel product final price  
  20. (integer) 2  
  21. redis 127.0.0.1:6379> hgetall product  
  22. 1) "name"  
  23. 2) "computer" 
0 0
原创粉丝点击