Introduction and use of hash data type related commands of redis

Posted by zipp on Wed, 19 Jan 2022 23:16:53 +0100

Introduction to hash type

The hash (hash or hash) in redis stores many key value pairs internally in the form of key - [field value]. It is also a two-dimensional structure of array + linked list (itself is also a key value pair structure). Because of this, we can usually use hash to store an object information. Each hash in redis can store 232 - 1 key value pairs (more than 4 billion)

Add command

  1. hset key field value
    Set the value of the field in the hash table key to value. If it does not exist, create the setting, otherwise the old value will be overwritten; If the field in the hash table already exists and the old value has been overwritten by the new value, 0 is returned instead of 1.
    127.0.0.1:6379> hset student name zhangsan
    (integer) 1
    127.0.0.1:6379> hgetall student
    1) "name"
    2) "zhangsan"
    
  2. hmset key field value [field value ...]
    Set multiple field value data into the hash table at one time, and the existing fields in the table will be directly overwritten. If a field value already exists in the hash table, the duplicate setting will return OK instead of 0.
    127.0.0.1:6379> hmset student age 18 hobby playgame
    OK
    127.0.0.1:6379> hgetall student
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "18"
    5) "hobby"
    6) "playgame"
    
  3. hsetnx key field value
    It can be set only when the field in the hash table does not exist, otherwise it is invalid. If the set field already has a value, the current operation will return a result set of 0 instead of OK
    //The field name already exists, so 0 is returned
    127.0.0.1:6379> hsetnx student name lisi
    (integer) 0
    127.0.0.1:6379> hsetnx student sex man
    (integer) 1
    127.0.0.1:6379> hgetall student
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "18"
    5) "hobby"
    6) "playgame"
    7) "sex"
    8) "man"
    
  4. hincrby key field increment
    Add a value to the specified field in the hash table. After executing the hincrby command, the latest value of the field is returned, not ok or 1.
    //Age + 1
    127.0.0.1:6379> hincrby student age 1
    (integer) 19
    127.0.0.1:6379> hgetall student
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "19"
    5) "hobby"
    6) "playgame"
    7) "sex"
    8) "man"
    

Query command

  1. hget key field
    Get the value of the given field in the hash table key. If it does not exist, return nil

    127.0.0.1:6379> hget student name
    "zhangsan"
    
  2. hgetall key
    Get all fields and values in the hash table key. If they do not exist, return an empty list

    127.0.0.1:6379> hgetall student
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "19"
    5) "hobby"
    6) "playgame"
    7) "sex"
    8) "man"
    
  3. hlen key
    Get the number of field s in the hash table key. If it does not exist, return 0

    127.0.0.1:6379> hlen student
    (integer) 4
    
  4. hmget key field [field ...]
    Get the value of one or more given fields in the hash table key. If there is no value, return nil

    127.0.0.1:6379> hmget student name age
    1) "zhangsan"
    2) "19"
    
  5. hkeys key
    Get the keys of all fields in the hash table key. If it does not exist, an empty table will be returned

    127.0.0.1:6379> hkeys student
    1) "name"
    2) "age"
    3) "hobby"
    4) "sex"
    
  6. hscan key cursor [MATCH pattern] [COUNT count]
    Iterates over key value pairs in the hash table.
    Cursor: cursor. Each time the HSCAN command is called, a new cursor will be returned to the user. The user needs to use this new cursor as the cursor parameter of the HSCAN command in the next iteration to continue the previous iteration process. When the cursor parameter of the SCAN command is set to 0, the server will start a new iteration. When the server returns a cursor with a value of 0 to the user, it indicates that the iteration has ended

    MATCH pattern: matching pattern. Like the KEYS command, the incremental iteration command can also provide a glob style pattern parameter to make the command return only elements matching the given pattern. This can be achieved by giving the MATCH parameter when executing the incremental iteration command

    COUNT: specifies how many elements are returned from the dataset. The default value is 10. Although the incremental iteration command does not guarantee the number of elements returned in each iteration, we can use the COUNT option to adjust the behavior of the command to some extent. Basically, the function of the COUNT option is to let the user tell the iteration command how many elements should be returned from the dataset in each iteration

    127.0.0.1:6379> hscan student 0 match *a* count 2
    1) "0"
    2) 1) "name"
       2) "zhangsan"
       3) "age"
       4) "19"
    
  7. hvals key
    Get the values of all fields in the hash table key. If there is no field, return an empty table

    127.0.0.1:6379> hvals student
    1) "zhangsan"
    2) "19"
    3) "playgame"
    4) "man"
    
  8. hexists key field
    Gets whether the field in the hash table key exists. If it exists, it returns 1. If it does not exist, it returns 0

    127.0.0.1:6379> hexists student name
    (integer) 1
    127.0.0.1:6379> hexists student class
    (integer) 0
    
  9. hstrlen key field
    Gets the length of the field in the hash table key. If it does not exist, it returns 0. Otherwise, it returns the length integer

    127.0.0.1:6379> hstrlen student name
    (integer) 8
    

Delete command

  1. hdel key field [field ...]
    Delete one or more fields in the hash table. If they do not exist, they will be ignored. The return value of the deletion operation is the number of successful deletion, and the non-existent fields will be ignored
    //The class key does not exist, so 2 is returned
    127.0.0.1:6379> hdel student hobby sex class
    (integer) 2
    127.0.0.1:6379> hkeys student
    1) "name"
    2) "age"
    

Application scenario

  1. Shopping cart: the hset key field value command can be implemented with user Id, commodity Id as field and commodity quantity as value. It just constitutes three elements of shopping cart
  2. Storage object: the structure of hash type (key, field, value) is similar to that of object (object id, attribute, value). It can also be used to store objects.

Topics: Redis hash Reids