Introduction to the
  • A hash consists of field-value pairs whose fields and values can be literal, integer, floating point, or binary data
  • Each field in the same hash must be unique and distinct, and there is no requirement for the value of a field. The values of different fields can be repeated
  • Equivalent to Hash in Java
Basic operation
  • HSET key field value Associates the given field value pair with value in the hash key. If the field has no previous associated value, the command returns 1. If the field already has an associated value, the command overwrites the old value with the new value and returns 0

    127.0.0.1:6379> hset hash msg hello
    (integer) 1
    127.0.0.1:6379> hset hash msg hi
    (integer) 0
    Copy the code
  • HSET Key field returns the value associated with the pre-field in the hash key key, or nil if the field has no associated value

    127.0.0.1:6379> hget hash msg
    "hi"
    127.0.0.1:6379> hget hash msg1
    (nil)
    Copy the code
  • HSETNX key Field Value If field does not exist in the hash key, the command associates the given field value pair with value if field already has an associated value

    127.0.0.1:6379> hsetnx hash msg hi
    (integer) 0
    127.0.0.1:6379> hsetnx hash msg2 hi
    (integer) 1
    127.0.0.1:6379> hget hash msg 
    "hi"
    127.0.0.1:6379> hget hash msg2
    "hi"
    Copy the code
  • HEXISTS key Field Checks whether the specified field exists in the hash key. 1 is returned if the field exists; 0 is returned if the field does not exist

     127.0.0.1:6379> hexists hash msg
    (integer) 1
    127.0.0.1:6379> hexists hash test
    (integer) 0
    Copy the code
  • HDEL key field [field …] Delete one or more of the specified fields in the hash key, the corresponding values of the fields already exist will be manipulated, the command returns the number of field value pairs successfully deleted

    127.0.0.1:6379> hdel hash msg msg2 test
    (integer) 2
    Copy the code
  • HLEN key Gets the number of field value pairs that the hash key contains

    127.0.0.1:6379> hlen hash
    (integer) 2
    Copy the code
The batch operation
  • HMSET key field value [ field value … ] Associating multiple field value pairs in a hash key is equivalent to executing multiple Hsets simultaneously

    127.0.0.1:6379> hmset hash msg3 hi3 msg4 hi4
    OK
    Copy the code
  • HMGET key field [ field …] Returns the value of one or more fields in the hash key, equivalent to performing multiple HGETS simultaneously

    127.0.0.1:6379> hmget hash msg1 msg2 msg3 msg4
    1) "world"
    2) (nil)
    3) "hi3"
    4) "hi4"
    Copy the code
  • HKEYS Key returns all fields contained in the hash key

    127.0.0.1:6379> hkeys hash
    1) "msg"
    2) "msg1"
    3) "msg3"
    4) "msg4"
    Copy the code
  • HVALS key Returns the values of all fields in the hash key

    127.0.0.1:6379> hash 1) "hello" 2) "world" 3) "hi3" 4) "hi4"Copy the code
  • GHETALL Key returns all field value pairs contained in the hash key

    127.0.0.1:6379> hgetall hash
    1) "msg"
    2) "hello"
    3) "msg1"
    4) "world"
    5) "msg3"
    6) "hi3"
    7) "msg4"
    8) "hi4"
    Copy the code
Digital operation

There is no field that initializes the value to 0 and then performs the corresponding operation

  • HINCRBY key field incrementIncrement increment is the value of field increment in the hash key
    127.0.0.1:6379> hincrby hash number 2
    (integer) 2
    127.0.0.1:6379> hget hash number
    "2"
    127.0.0.1:6379> hincrby hash number -4
    (integer) -2
    127.0.0.1:6379> hget hash number
    "-2"
    Copy the code
  • HINCRBYFLOAT key field incrementIncrement increment; return the value of field increment increment
    127.0.0.1:6379> hincrbyfloat hash float 0.9
    "0.9"
    127.0.0.1:6379> hincrbyfloat hash float -3.14
    "-2.24"
    Copy the code
Hash keys and string keys
  • Command is

    Hash commands String command
    HSET SET
    HGET GET
    HSETINX SETINX
    HDEL DEL (can delete any key)
    HMSET MSET
    HMGET MGET
    HINCRBY INCRBY
    HINCRBYFLOAT INCRBYFLOAT
    HEXISTS EXISTS (can check the existence of any key)
  • Benefits of hashing 1. Related information can be stored in the same place, rather than directly scattered in the entire database, which facilitates data management and avoids misoperations. 2. Avoid conflict key name 3. Reduce the memory footprint Redis in creating each key with additional database management information (such as the type of this key, a best access time, etc.), so the key inside the database, the more servers in storage management information, the more memory, additional spent on key management database is the CPU. When the hash contains a small number of field pairs, Redis automatically implements the underlying hash using a data structure that takes up very little memory. This helps greatly when the hash number is high