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

xiaoxiao2021-02-27  288

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 ? redis 127.0.0.1:6379> hset user name  'zhangsan'          # 设置值为zhangsan  (integer) 1  redis 127.0.0.1:6379> hset user name  'lisi'              # 将值覆盖为lisi  (integer) 0  redis 127.0.0.1:6379>   2,hget  命令:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[plain] view plain copy print ? redis 127.0.0.1:6379> hgetall product  1) "name"  2) "computer"  3) "price"  4) "3300"  5) "size"  6) "14inch"  7) "final"  8) "3200"  redis 127.0.0.1:6379> hdel product size  (integer) 1  redis 127.0.0.1:6379> hgetall product  1) "name"  2) "computer"  3) "price"  4) "3300"  5) "final"  6) "3200"  redis 127.0.0.1:6379> hdel product final price  (integer) 2  redis 127.0.0.1:6379> hgetall product  1) "name"  2) "computer" 
转载请注明原文地址: https://www.6miu.com/read-10189.html

最新回复(0)