“This is the seventh day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

Hash Indicates the Hash type

A hash type is a Redis key-value pair whose value is itself a key-value pair structure of the form value=[{field1, value1}…{fieldN, valueN}],

The Redis hash is a string mapping table of fields and values. Hashes are particularly suitable for storing objects.

Each hash in Redis can store 2^32-1 key-value pairs (over 4 billion).

Common commands

Assign a value to a field in the hash table: hset

If the hash table does not exist, a new hash table is created and HSET.

If the field already exists in the hash table, the old value is overwritten.

Gets the value of a field in a hash table: hGET

127.0.0.1:6379> hSet MyHash Field1 Hello1 (INTEGER) 1 127.0.0.1:6379> hGET MyHash Field1 "hello1" 127.0.0.1:6379> hSET Myhash Field1 hello2 (INTEGER) 0 127.0.0.1:6379> hGET MyHash Field1 #hello1Copy the code

Set multiple field-value pairs to hash table at the same time: mhset

This command overwrites the existing fields in the hash table.

If the hash table does not exist, an empty hash table is created and the HMSET operation is performed.

Get the values of fields in multiple hash tables simultaneously: MHGET

127.0.0.1:6379> hmset myhash field1 hello1 field2 hello2 OK 127.0.0.1:6379> hmget myHash field1 1) "hello1" 2) "hello1"Copy the code

Gets all the fields and values of the specified key in the hash table: hGEtall

 127.0.0.1:6379> hgetall myhash
 1) "field1"
 2) "hello1"
 3) "field2"
 4) "hello2"
Copy the code

Deletes one or more specified fields in the hash table key. Non-existent fields are ignored: hdel

127.0.0.1:6379> hGEtall MyHash # 1) "fielD2" 2) "Hello2" 127.0.0.1:6379> hmset myHash field3 hello3 field4 Hello4 field5 hello5 OK 127.0.0.1:6379> hgetall MyHash 1) "field2" 2) "hello2" 3) "field3" 4) "hello3" 5) "field4" 6) "hello4" 7) "field5" 8) "hello5" #============= Delete multiple fields from the specified hash myhash ============= 127.0.0.1:6379> hdel myHash field2 field3 field3 field4 field5 (INTEGER) 4 127.0.0.1:6379> hGetall MyHash (empty Array)Copy the code

Gets the number of fields in the hash table: hlen

The number of fields in the hash table. Return 0 if the key does not exist

 127.0.0.1:6379> hmset myhash field3 hello3 field4 hello4 field5 hello5
 OK
 127.0.0.1:6379> hlen myhash
 (integer) 3
 127.0.0.1:6379> hlen youhash
 (integer) 0
Copy the code

Check whether the specified field of the hash table exists: hEXISTS

If the hash table contains the given field, return 1. If the hash table does not contain the given field, or if the key does not exist, return 0

 127.0.0.1:6379> hgetall myhash
 1) "field3"
 2) "hello3"
 3) "field4"
 4) "hello4"
 5) "field5"
 6) "hello5"
 127.0.0.1:6379> hexists myhash field4
 (integer) 1
 127.0.0.1:6379> hexists myhash field6
 (integer) 0
Copy the code

Gets all fields in the hash table: hkeys

Gets the attributes for all fields in the hash table: hVALS

127.0.0.1:6379> hGeTall MyHash 1) "field3" 2) "hello3" 3) "field4" 4) "hello4" 5) "field5" 6) "hello5" 127.0.0.1:6379> 1) "field3" 2) "field4" 3) "field5" 127.0.0.1:6379> hVALS  "hello4" 3) "hello5"Copy the code

Adds the specified increment to the field values in the hash table: hincrby

The increment can also be negative, which is equivalent to subtracting a specified field.

If the key of the hash table does not exist, a new hash table is created and the HINCRBY command is executed.

If the specified field does not exist, the value of the field is initialized to 0 before the command is executed.

Executing the HINCRBY command on a field that stores a string value will result in an error.

The value of this operation is limited to a 64-bit signed numeric representation.

 127.0.0.1:6379> hset myhash field1 10
 (integer) 1
 127.0.0.1:6379> hincrby myhash field1 5
 (integer) 15
 127.0.0.1:6379> hget myhash field1
 "15"
Copy the code

Assign hsetnx to a field that does not exist in the hash table

If the hash table does not exist, a new hash table is created and HSET.

If the field already exists in the hash table, the operation is invalid.

If the key does not exist, a new hash table is created and the HSETNX command is executed.

If the setting succeeds, 1 is returned. Return 0 if the given field already exists and no operation has been performed.

127.0.0.1:6379> hsetnx myhash field2 hello2
(integer) 1
127.0.0.1:6379> hsetnx myhash field2 hi2
(integer) 0
Copy the code

The Redis hash object is often used to cache some object information, such as user information, product information, configuration information, etc.

127.0.0.1:6379> hset user:1 name wanli age 3 
(integer) 2
127.0.0.1:6379> hmget user:1 name age
1) "wanli"
2) "3"
Copy the code

\