2.2. The Hash (Hash)

Introduction of 2.2.1.

  • Redis hash is a mapping table of fields and values of string type. Hash is especially suitable for storing objects.
  • Redis can store 232-1 key-value pairs per hash (over 4 billion)
  • It can be regarded as a MAP container with keys and values. This type is very suitable for storing information about VALUE objects, such as UName,upass, and Age. This type of data takes up very little disk space (compared to JSON)

This type is somewhat similar to Object (Map) in Java

2.2.2. Hash commands

Assignment syntax:

  • HSET KEY FIELD VALUE // Set FILD/VALUE for the specified KEY

  • HMSET KEY FIELD VALUE [FIELD1,VALUE1]…… Set multiple field-value pairs into the hash key at the same time.

  • Value syntax:

    HGET KEY FIELD // Retrieves the VALUE stored in the HASH based on the FIELD

    HMGET key field[field1] // Get the values of all given fields in the key

    HGETALL key // Returns all the fields and values in the HASH table



  • HKEYS key // Get all fields in the hash table
  • HLEN key // Get the number of fields in the hash table

  • Type Key // View the key type

  • Delete all: del key

  • Delete syntax:

    HDEL KEY field1[field2] // Delete one or more HASH table fields

  • Other syntax: HSETNX Key field Value Sets the value of a hash table field only if the field field does not exist
  • HINCRBY key field increment

    Add increment to the integer value of the specified field in the hash table key.



  • HINCRBYFLOAT Key field INCREMENT Specifies the floating point value of the specified field in the hash table key plus increment increment.
  • HEXISTS key field // Checks whether the specified field in the hash table key exists

2.2.3. Application Scenarios

Application scenarios of Hash :(store a user information object data) 1. It is usually used to store an object. 2.

  • Hash is the data type that most closely resembles the structure of a relational database. It can convert a database record or an object in a program into a HashMap and store it in Redis.
  • The user ID is the key to search for, and the value is stored. The user object contains information such as name, age, and date of birth. If the user object is stored using the common key/value structure, it can be stored in the following two ways:
  • The first way to user ID as a lookup key, encapsulate other information into an object is stored in the form of serialization, the disadvantage of this approach is that increased the serialization/deserialization overhead, and when one of the need to modify the information, need to retrieve the object, and concurrent modification operations need to be protected, the introduction of CAS and other complex problems.
  • The second method is to the user how many members of the deposit information object into many key – value pair, use the user ID + correspond to the name of the attribute as a unique identifier to obtain corresponding to the value of the attribute, while eliminating the serialization overhead and concurrency issues, but the user ID for repeated storage, if there is a large number of such data, memory waste is very considerable.

Conclusion:

The Hash provided by Redis solves this problem very well. The Hash provided by Redis is actually a HashMap that stores the Value internally and provides an interface to access the Map members directly