Redis tutorial of iv: Hashes data type

  • 2020-05-06 11:59:33
  • OfStack

Overview:

We can think of Hashes types in Redis as map containers with String Key and String Value. So this type is ideal for storing information about value objects. Such as Username, Password and Age. If Hash contains very few fields, this type of data also takes up very little disk space. Each Hash can store 4294967295 key-value pairs.

ii. List of related commands:

命令原型 时间复杂度 命令描述 返回值
HSET key field value O(1) 为指定的Key设定Field/Value对,如果Key不存在,该命令将创建新Key以参数中的Field/Value对,如果参数中的Field在该Key中已经存在,则用新值覆盖其原有值。  1表示新的Field被设置了新值,0表示Field已经存在,用新值覆盖原有值。 
HGET key field  O(1)  返回指定Key中指定Field的关联值。 返回参数中Field的关联值,如果参数中的Key或Field不存,返回nil。
HEXISTSkey field  O(1)  判断指定Key中的指定Field是否存在。 1表示存在,0表示参数中的Field或Key不存在。
HLEN key  O(1) 获取该Key所包含的Field的数量。 返回Key包含的Field数量,如果Key不存在,返回0。
HDEL key field [field ...]  O(N) 时间复杂度中的N表示参数中待删除的字段数量。从指定Key的Hashes Value中删除参数中指定的多个字段,如果不存在的字段将被忽略。如果Key不存在,则将其视为空Hashes,并返回0. 实际删除的Field数量。
HSETNXkey field value O(1) 只有当参数中的Key或Field不存在的情况下,为指定的Key设定Field/Value对,否则该命令不会进行任何操作。  1表示新的Field被设置了新值,0表示Key或Field已经存在,该命令没有进行任何操作。
HINCRBYkey field increment  O(1) 增加指定Key中指定Field关联的Value的值。如果Key或Field不存在,该命令将会创建一个新Key或新Field,并将其关联的Value初始化为0,之后再指定数字增加的操作。该命令支持的数字是64位有符号整型,即increment可以负数。  返回运算后的值。
HGETALLkey O(N)  时间复杂度中的N表示Key包含的Field数量。获取该键包含的所有Field/Value。其返回格式为一个Field、一个Value,并以此类推。 Field/Value的列表。
HKEYSkey  O(N) 时间复杂度中的N表示Key包含的Field数量。返回指定Key的所有Fields名。 Field的列表。
HVALSkey  O(N) 时间复杂度中的N表示Key包含的Field数量。返回指定Key的所有Values名。  Value的列表。 
HMGETkey field [field ...]  O(N)  时间复杂度中的N表示请求的Field数量。获取和参数中指定Fields关联的一组Values。如果请求的Field不存在,其值返回nil。如果Key不存在,该命令将其视为空Hash,因此返回一组nil。 返回和请求Fields关联的一组Values,其返回顺序等同于Fields的请求顺序。
HMSET key field value [field value ...] O(N) 时间复杂度中的N表示被设置的Field数量。逐对依次设置参数中给出的Field/Value对。如果其中某个Field已经存在,则用新值覆盖原有值。如果Key不存在,则创建新Key,同时设定参数中的Field/Value。    

Example of command:

      1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:
 


    # in Shell Command line startup Redis Client program
    /> redis-cli
    # To key values myhash The key setting field is field1 And has a value of stephen .
    redis 127.0.0.1:6379> hset myhash field1 "stephen"
    (integer) 1
    # Gets the key value of myhash , the field of field1 The value of the.
    redis 127.0.0.1:6379> hget myhash field1
    "stephen"
    #myhash It doesn't exist in the bond field2 Field, so returns nil .
    redis 127.0.0.1:6379> hget myhash field2
    (nil)
    # to myhash The associated Hashes Value adds a new field field2 , its value is liu .
    redis 127.0.0.1:6379> hset myhash field2 "liu"
    (integer) 1
    # To obtain myhash Number of key fields.
    redis 127.0.0.1:6379> hlen myhash
    (integer) 2
    # judge myhash Whether there is a field name in the key field1 , returns a value of 1 .
    redis 127.0.0.1:6379> hexists myhash field1
    (integer) 1
    # delete myhash Key field name field1 The field of delete returns successfully 1 .
    redis 127.0.0.1:6379> hdel myhash field1
    (integer) 1
    # Delete again myhash Key field name field1 Because the last command has deleted it, because it was not deleted, returns 0 .
    redis 127.0.0.1:6379> hdel myhash field1
    (integer) 0
    # judge myhash Whether it's in the bond field1 Field because the previous command has deleted it because it returns 0 .
    redis 127.0.0.1:6379> hexists myhash field1
    (integer) 0
    # through hsetnx The command to myhash Add a new field field1 , its value is stephen Because the field has been deleted, the command adds successfully and returns 1 .
    redis 127.0.0.1:6379> hsetnx myhash field1 stephen
    (integer) 1
    # Due to the myhash the field1 The field was successfully added from the previous command because this command returns without doing anything 0 .
    redis 127.0.0.1:6379> hsetnx myhash field1 stephen
    (integer) 0

    2. HINCRBY:
   

    # Remove the key to facilitate testing of the following examples.
    redis 127.0.0.1:6379> del myhash
    (integer) 1
    # Prepare the test data, that myhash the field Field Settings 1 .
    redis 127.0.0.1:6379> hset myhash field 5
    (integer) 1
    # to myhash the field The value of the field plus 1 , returns the plus result.
    redis 127.0.0.1:6379> hincrby myhash field 1
    (integer) 6
    # to myhash the field The value of the field plus -1 , returns the plus result.
    redis 127.0.0.1:6379> hincrby myhash field -1
    (integer) 5
    # to myhash the field The value of the field plus -10 , returns the plus result.
    redis 127.0.0.1:6379> hincrby myhash field -10
    (integer) -5  

      3. HGETALL/HKEYS/HVALS/HMGET/HMSET:
 

    # Remove the key for later sample testing.
    redis 127.0.0.1:6379> del myhash
    (integer) 1
    # For the key myhash , set multiple fields at once, respectively field1 = "hello", field2 = "world" .
    redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
    OK
    # To obtain myhash Multiple fields of the key, among them field3 Does not exist because the value corresponding to this field in the return result is nil .
    redis 127.0.0.1:6379> hmget myhash field1 field2 field3
    1) "hello"
    2) "world"
    3) (nil)
    # return myhash All the fields of the key and their values, as you can see from the results, are listed in pairs.
    redis 127.0.0.1:6379> hgetall myhash
    1) "field1"
    2) "hello"
    3) "field2"
    4) "world"
    # Only get myhash The name of all the fields in the key.
    redis 127.0.0.1:6379> hkeys myhash
    1) "field1"
    2) "field2"
    # Only get myhash Values for all fields in the key.
    redis 127.0.0.1:6379> hvals myhash
    1) "hello"
    2) "world"


Related articles: