Redis command: hash


Redis Hash

Redis Hash is one of the Redis data structures, which is a collection of key-value pairs. It works very much like a table structure, with a key and multiple values ​​associated with it. In Redis Hash, each field (Field) and value (Value) are string types.

The Hash data structure is particularly suitable for storing objects. For example, a user object contains user name, age, birthday and other information. Compared with storing multiple key-value pairs in multiple keys, the Hash structure can store richer data information through one key and internal field-value, and does not require multiple queries when obtaining data, which can achieve more efficient data access.

For example, if we have a user ID as the lookup key, we can store multiple attributes of the user (such as name, age, birthday, etc.) as values ​​in the hash table. Through key and field, we can find the corresponding value in O(1) time, avoiding the overhead of searching in multiple keys and the problem of concurrent modification control.

The data structure of Redis Hash corresponds to two implementation methods: ziplist and hashtable. When the field-value length is short and the number is small, use ziplist; otherwise, use hashtable. This data structure can ensure the speed and effectiveness of obtaining data, while also reducing memory usage.

Example

Here are some examples of Redis hash commands:

  1. HSET command: Set a field in the hash table to the specified value.
HSET myhash field1 "Hello"
  1. HGET command: Get the value corresponding to a field in the hash table.
HGET myhash field1
  1. HMSET command: Set multiple field-value pairs in the hash table in batches.
HMSET myhash field1 "Hello" field2 "World"
  1. HEXISTS command: Check whether a given field exists in the hash table.
HEXISTS myhash field1
  1. HKEYS command: List all fields in the hash table.
HKEYS myhash
  1. HVALS command: List all values ​​in the hash table.
HVALS myhash
  1. HINCRBY command: increments the value of a field in the hash table.
HINCRBY myhash field1 1
  1. HSETNX command: Set the value of a field in the hash table to a given value. The setting is successful only when the field does not exist.
HSETNX myhash field3 "New Value"

in the above command myhashis the name of the hash table, which can be replaced according to the actual situation. At the same time, these commands can be executed through the Redis client, such as using redis-clicommand line tools or Redis client libraries in other programming languages.

Redis hash command

The following are some basic commands and descriptions of Redis Hash:

Order describe
HSET key field value Set the field in the hash table key to value. If key does not exist, create a new hash table.
HGET key field Get the value of the field in the hash table key. If the field does not exist, returns nil.
HMSET key field1 value1 field2 value2 … fieldN valueN Set multiple field-value pairs to the hash table key at the same time. If key does not exist, create a new hash table.
HGETALL key Get all fields and values ​​in the hash table key. Returns a list containing all field-value pairs.
HKEYS key Get all fields in the hash table key. Returns a list containing all fields.
HVALS key Get all values ​​in hash table key. Returns a list containing all values.
HLEN key Get the number of fields in the hash table key. Returns an integer value for the number of fields.
HEXISTS key field Check whether the specified field exists in the hash table key. If the field exists, return 1; otherwise return 0.
HINCRBY key field increment Add an increment to the value of the specified field in the hash table key. If the field does not exist, its value is set to increment.
HSETNX key field value Set the field in the hash table key to value. The setting is successful only when the field does not exist. If the field already exists, no action is performed.
HSCAN key cursor [MATCH pattern] [COUNT count] Iterate over the fields and values ​​in the hash table key. You can use the MATCH and COUNT options to filter and limit the returned results. Returns a list containing fields and values ​​and the next cursor.

These commands are basic operations on the Redis Hash data type and can be used to create, get, set, and iterate data in the hash table. Please note that only some commonly used commands are listed here, Redis also provides other more advanced commands and functions.

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/133140168