Redis Hash is a string-type field and value mapping table. Its addition, delete operations are O (1) (average). Hash is particularly useful for storing objects. there is a single string type compared to each field of an object. Storing an object in a hash type takes up less memory and makes it easier to access the entire object.
Hset: Sets the hash field to the specified value, and if key does not exist, it is created first.
Hget: Gets the specified hash field.
127.0.0.1:6379>hset user:001 name Tom
(integer) 1
127.0.0.1:6379>hset user:001 Age 28
(integer) 1
127.0.0.1:6379>hget user:001 Name
"Tom"
HSETNX: Sets the hash field to the specified value, and if key does not exist, it is created first. Returns 0 if it exists.
127.0.0.1:6379>hset user:001 name Jack
(integer) 0
Hmset: Set multiple fields of hash at the same time.
Hmget: Gets all the specified hash field.
127.0.0.1:6379> hmset user:002 ID 1 name Mark sex male Age 29
Ok
127.0.0.1:6379> Hget user:002
(Error) ERR wrong number of arguments for ' hget ' command
127.0.0.1:6379> hmget user:002 ID name male age
1) "1"
2) "Mark"
3) (nil)
4) "29"
Hincrby: Specifies the hash field plus the given value.
127.0.0.1:6379> Hincrby user:002 age 6
(integer) 35
127.0.0.1:6379> Hincrby user:002 age-2
(integer) 33
Hexists: test Specifies whether the field exists in the hash.
127.0.0.1:6379> hexists user:002 Sex
(integer) 1
127.0.0.1:6379> hexists user:001 Sex
(integer) 0
Hlen: Returns the number of field for the specified hash.
127.0.0.1:6379> Hlen user:001
(integer) 2
127.0.0.1:6379> Hlen user:002
(integer) 4
Hdel: Deletes the field of the specified hash.
127.0.0.1:6379> Hdel user:002 Sex
(integer) 1
127.0.0.1:6379> hget user:002 Sex
(nil)
Hkeys: Returns all field of the hash.
127.0.0.1:6379> Hkeys user:002
1) "id"
2) "Name"
3) "Age"
127.0.0.1:6379> Hkeys user:001
1) "Name"
2) "Age"
Hvals: Returns all value of the hash.
127.0.0.1:6379> hvals user:002
1) "1"
2) "Mark"
3) "33"
Hgetall: Gets all the field and value in a hash.
127.0.0.1:6379> Hgetall user:002
1) "id"
2) "1"
3) "Name"
4) "Mark"
5) "Age"
6) "33"
Redis Learning Lesson Two: Redis hash types and operations