“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

Redis Hash is a key-value type. The value type is similar to the map structure: key-{{field1,value1},… ,{fieldN, valueN}}, is more suitable for holding objects.

For example, if we want to save the user’s personal information, in the String type, we will save the object’s serial number as a JSON String, which is easy to access but not easy to update. If we want to add a property, we need to update the entire value. The Hash type can be saved to the property granularity, making it easier to add and delete properties.

All Hash commands start with H. Let’s learn how to do this systematically.

HSET

Available versions: >= 2.0.0

Time complexity: O(N), where N is the number of fields

The command format

HSET key field value [field value ...]
Copy the code

Command description

  • Save multiple field-values to the hash table;
  • If the key does not exist, a new hash table is created.
  • If the hash table corresponding to the key already exists and the filed already exists, the set is overwritten.

The return value

Number of successful fields added

The sample

127.0.0.1:6379> hset user:1 name lifelmy age 10
(integer) 2
127.0.0.1:6379> hget user:1 name
"lifelmy"
127.0.0.1:6379> hget user:1 age
"10"
Copy the code

HSETNX

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HSETNX key field value
Copy the code

Command description

  • Value is set only if the field corresponding to the key does not exist.
  • No operation will be performed if the field corresponding to the key exists.
  • If the key does not exist, create a hash table.

The return value

  • 1: Field does not exist. The setting is successful
  • 0: Field already exists but is not set

The sample

127.0.0.1:6379> HSETNX user:01 name lifelmy
(integer) 1
127.0.0.1:6379> HSETNX user:01 age 25
(integer) 1

#Age field already exists and will not be set
127.0.0.1:6379> HSETNX user:01 age 18
(integer) 0

127.0.0.1:6379> hget user:01 name
"lifelmy"
127.0.0.1:6379> hget user:01 age
"25"
Copy the code

HGET

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HGET key field
Copy the code

Command description

Key corresponds to the hash table, and the corresponding value filed is returned.

The return value

  • String: the value of the field when it exists
  • Nil: Field does not exist or key does not exist

The sample

127.0.0.1:6379> hset User :01 Name lifelmy (INTEGER) 0
#The field is
127.0.0.1:6379> hget user:01 name
"lifelmy"

#The field does not exist
127.0.0.1:6379> hget user:01 gender
(nil)

#The key doesn't exist
127.0.0.1:6379> hget user:02 name
(nil)

Copy the code

HEXISTS

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HEXISTS key field
Copy the code

Command description

Returns whether filed exists in the hash table corresponding to key.

The return value

  • 1: Field exists
  • 0: Field or key does not exist

The sample

127.0.0.1:6379> hset stu:01 name lifelmy (INTEGER) 1 127.0.0.1:6379> hEXISTS STu :01 name (INTEGER) 1 127.0.0.1:6379> hexists stu:01 age (integer) 0Copy the code

HDEL

Available versions: >= 2.0.0

Time complexity: O(N), where N is the number of fields given

The command format

HDEL key field [field ...]
Copy the code

Command description

  • Delete the hash table corresponding to the key.
  • If the field itself does not exist, the deletion ignores the field.
  • If the key does not exist, return 0.

The return value

  • Integer value: number of fields to delete (excluding non-existent fields)

The sample

127.0.0.1:6379> hset mykey field1 value1
(integer) 1

#Field2 does not exist, return 0
127.0.0.1:6379> hdel mykey field2
(integer) 0

#Field1 exists, return 1
127.0.0.1:6379> hdel mykey field1 field2
(integer) 1

#The key doesn't exist
127.0.0.1:6379> hdel mykey1 field1
(integer) 0
Copy the code

HSTRLEN

Available version: >= 3.2.0

Time complexity: O(1)

The command format

HSTRLEN key field
Copy the code

Command description

Return the length of value filed in the hash table corresponding to the key.

The return value

  • Integer value: the length of the value corresponding to the field. 0 is returned if the key or field does not exist

The sample

127.0.0.1:6379> hSET User :001 name lifelmy (INTEGER) 1
#The name field is
127.0.0.1:6379> hstrlen user:001 name
(integer) 7

#The field does not exist127.0.0.1:6379> hSTRlen User :001 age (INTEGER) 0
#The key doesn't exist
127.0.0.1:6379> hstrlen user:111 name
(integer) 0
Copy the code

HLEN

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HLEN key
Copy the code

Command description

Return the number of filed fields in the hash table corresponding to the key.

The return value

  • Integer value: number of fields in the hash table. If the key does not exist, 0 is returned

The sample

127.0.0.1:6379> hset user:001 name lifelmy age 25
(integer) 2

127.0.0.1:6379> hlen user:001
(integer) 2

#The key doesn't exist
127.0.0.1:6379> hlen user:111
(integer) 0
Copy the code

HINCRBY

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HINCRBY key field increment
Copy the code

Command description

  • In the hash table corresponding to key, add an integer to the value corresponding to the filed;
  • If the key does not exist, create a hash table.
  • If the field does not exist, create a field with a value of 0.

The return value

  • Integer value: The value of value after the operation is performed

The sample

127.0.0.1:6379> hset user:01 cost 10
(integer) 1
127.0.0.1:6379> hincrby user:01 cost 2
(integer) 12
127.0.0.1:6379> hincrby user:01 cost -5
(integer) 7

#The key doesn't exist
127.0.0.1:6379> hincrby user:02 cost 2
(integer) 2
127.0.0.1:6379> hget user:02 cost
"2"
Copy the code

HINCRBYFLOAT

Available versions: >= 2.0.0

Time complexity: O(1)

The command format

HINCRBYFLOAT key field increment
Copy the code

Command description

  • In the hash table corresponding to key, add a floating point number to the value of the field.
  • If the key does not exist, create a hash table.
  • If the field does not exist, create a field with value 0;
  • Refer to the INCRBYFLOAT operation.

The return value

  • String: string representation of the value after the operation is performed
  • Error: The old value is not String or cannot be converted to floating point

The sample

127.0.0.1:6379> HSET mykey Field 10.50 (INTEGER) 1 127.0.0.1:6379> HINCRBYFLOAT mykey Field 0.1 "10.6" 127.0.0.1:6379> float mykey field 0.1 "10.6" HINCRBYFLOAT myKey field -5 "5.6" 127.0.0.1:6379> HSET myKey Field 5.0e3 (INTEGER) 0 127.0.0.1:6379> HINCRBYFLOAT myKey field -5 "5.6" 127.0.0.1:6379> HSET MyKey Field 5.0e3 (INTEGER) 0 127.0.0.1:6379> HINCRBYFLOAT myKey field The field 2.0 e2 "5200"Copy the code

HKEYS

Available versions: >= 2.0.0

Time complexity: O(N), where N is the length of the hash table

The command format

HKEYS key
Copy the code

Command description

Returns all fields in the hash table

The return value

  • The field list

The sample

127.0.0.1:6379> flushdb OK 127.0.0.1:6379> hset user:01 name zhangsan (INTEGER) 1 127.0.0.1:6379> hset user:01 age 18 (INTEGER) 1 127.0.0.1:6379> hkeys user:01 1) name 2) ageCopy the code

HVALS

Available versions: >= 2.0.0

Time complexity: O(N), where N is the length of the hash table

The command format

HVALS key
Copy the code

Command description

Returns vLAUe values for all fields in the hash table

The return value

  • The value list of values

The sample

127.0.0.1:6379> hset user:01 name zhangsan
(integer) 1
127.0.0.1:6379> hset user:01 age 18
(integer) 1
127.0.0.1:6379> hvals user:01
1) "zhangsan"
2) "18"
Copy the code

HGETALL

Available versions: >= 2.0.0

Time complexity: O(N), where N is the length of the hash table

The command format

HGETALL key
Copy the code

Command description

Returns all the field-vlaue values in the hash table, in a list form, with value following the corresponding field.

The return value

  • Field, value List of values

The sample

127.0.0.1:6379> hset user:01 name zhangsan
(integer) 0
127.0.0.1:6379> hset user:01 age 18
(integer) 0
127.0.0.1:6379> hgetall user:01
1) "name"
2) "zhangsan"
3) "age"
4) "18"
Copy the code

HRANDFIELD

Available version: >= 6.2.0

Time complexity: O(N), where N is the number of returns

The command format

HRANDFIELD key [count [WITHVALUES]]
Copy the code

Command description

  • Given only the key, a random field is returned by default
  • If count is specified and is a positive number, return a list of fields with distinct elements and the smaller of count and hash length.
  • If count is negative, the field is repeated and the length of the field list is the absolute value of count
  • WITHVALUESIndicates that the returned result contains the corresponding value

The return value

  • String: specifies only key, return random filed or nil (key does not exist)
  • List: When specifying additional parameters

The sample

127.0.0.1:6379> hSET User :01 Name Zhangsan age 18 (INTEGER) 2
#Random return
127.0.0.1:6379> hrandfield user:01
"age"
127.0.0.1:6379> hrandfield user:01
"name"

#The count is greater than thehashThe length of the127.0.0.1:6379> hrandfield user:01 1) "name" 2) "age"
#The count negative
127.0.0.1:6379> hrandfield user:01 -4
1) "name"
2) "age"
3) "age"
4) "name"

#Specify the withvalues argument
127.0.0.1:6379> hrandfield user:01 4 withvalues
1) "name"
2) "zhangsan"
3) "age"
4) "18"

127.0.0.1:6379> hrandfield user:01 -3  withvalues
1) "name"
2) "zhangsan"
3) "age"
4) "18"
5) "name"
6) "zhangsan"
Copy the code

HMSET

Available versions: >= 2.0.0

Time complexity: O(N), N is the number of fields

Since Redis 4.0.0, this command is deprecated, HSET is preferred

The command format

HMSET key field value [field value ...]
Copy the code

Command description

  • Set multiple field-vlaue values simultaneously

The return value

  • The execution result

The sample

127.0.0.1:6379> hmset user:01 name zhangsan age 18 OK 127.0.0.1:6379> hget user:01 name "zhangsan" 127.0.0.1:6379> hmset  user:02 name lisi OKCopy the code

HMGET

Available versions: >= 2.0.0

Time complexity: O(N), N is the number of fields

The command format

HMGET key field [field ...]
Copy the code

Command description

  • Returns value values corresponding to multiple fields

The return value

  • Value List of values, return nil if field does not exist

The sample

127.0.0.1:6379> hset user:01 name zhangsan age 18 (INTEGER) 2 127.0.0.1:6379> hmget User :01 name age gender 1) "zhangsan" 2) "18" 3) (nil)Copy the code

HSCAN

Available versions: >= 2.8.0

Time complexity: O(1)

The command format

HSCAN key cursor [MATCH pattern] [COUNT count]
Copy the code

Command description

  • Used for incremental traversal of the entire hash table
  • Cursor is the cursor that indicates where to start the walk, with the first time specified as 0.
  • MATCH allows you to specify regular expressions
  • COUNT Specifies the number of returns. The default is 10 (the system uses this as a reference)
  • For details, see SCAN

The return value

The return value has two parts:

  1. The start position of the cursor for the next pass; If the traversal is complete, return 0
  2. The list of results of this traversal

The sample

127.0.0.1:6379> hSET User :01 Name Zhangsan age 18 Gender Male school SEu TALL 180 (INTEGER) 4
#The first returns a value of 0, indicating that the traversal is complete127.0.0.1:6379> hsCAN user:01 01) "0" 2) 1) "name" 2) "zhangsan" 3) "age" 4) "18" 5) "gender" 6) "male" 7) "school" 8) "seu" 9) "tall" 10) "180"#Specifying a regular expression
127.0.0.1:6379> hscan user:01 0 match nam*
1) "0"
2) 1) "name"
   2) "zhangsan"
Copy the code

More and more

Personal blog: lifelmy.github. IO /

Wechat official Account: Long Coding road