Hash is a ing table of key value (fieldvalue) columns. It is often used to store some object instances. Compared to storing each field of an object as a string, storing it as a hash will consume less memory. Why does it save more memory? Two configurations (hash-max-zipmap-entries and hash-max-zipmap-v) need to be clarified.
Hash is a ing table of key value (field value) columns. It is often used to store some object instances. Compared to storing each field of an object as a string, storing it as a hash will consume less memory. Why does it save more memory? Two configurations (hash-max-zipmap-entries and hash-max-zipmap-v) need to be clarified.
Main hash operation functions
Hash is a ing table of key value (field value) columns. It is often used to store some object instances. Compared to storing each field of an object as a string, storing it as a hash will consume less memory. Why does it save more memory? You need to understand the meaning of the two configurations (hash-max-zipmap-entries and hash-max-zipmap-value). The configuration details will be discussed in the final configuration optimization section.
1) added www.2cto.com.
A) hset
Syntax: hset key field value
Description: sets the field value in the key of the hash table. If the hash table does not exist, create and set the field value. If the hash table exists, overwrite or add the value field.
[Plain]
[Root @ xsf001 ~] # Redis-cli
Redis 127.0.0.1: 6379> hset user.1 name zhangsan # set the value of key user.1 name field
(Integer) 1
Redis 127.0.0.1: 6379> hset user.1 age 45 # set the age domain
(Integer) 1
Redis 127.0.0.1: 6379> hset user.1 tech lisi
(Integer) 1
B) hmset
Syntax: hash key field value [key value]
Explanation: Batch set hash table key fields
Www.2cto.com
[Plain]
Redis 127.0.0.1: 6379> hmset user.2 name niuer age 34 # set both the name and age Fields
OK
C) hsetnx
Syntax: hsetnx key field value
Explanation: only when the field does not exist, set the field value of the hash table.
[Plain]
Redis 127.0.0.1: 6379> hsetnx user.1 name lisi #0 is returned because the name field has been set
(Integer) 0
Redis 127.0.0.1: 6379> hsetnx user.1 fri 5
(Integer) 1 # fri domain is not set, so hset returns 1
2) Query
A) hget
Syntax: hget key field
Explanation: obtains the field value of the hash table key.
[Plain]
Redis 127.0.0.1: 6379> hget user.1 name # existing hash table and domain
"Zhangsan"
Redis 127.0.0.1: 6379> hget user.3 name # the hash table does not exist.
(Nil)
Redis 127.0.0.1: 6379> hget user.1 bb # nonexistent domain
(Nil)
B) hmet
Syntax: hmet key field [field]
Explanation: Batch retrieve the filed of the hash table
[Plain]
Redis 127.0.0.1: 6379> hmet user.1 name age fri tech
1) "zhangsan"
2) "45"
3) "5"
4) "lisi"
Redis 127.0.0.1: 6379> hmet user.1 name age fri tech nofield # the hash table contains a non-existing field nofield.
1) "zhangsan"
2) "45"
3) "5"
4) "lisi"
5) (nil)
Redis 127.0.0.1: 6379> hmet user.3 name age fri # nonexistent hash table
1) (nil)
2) (nil)
3) (nil)
C) hgetall
Syntax: hgetall key
Explanation: obtain all the domain values of the hash table.
[Plain]
Redis 127.0.0.1: 6379> hgetall user.2 # existing hash table
1) "name" # domain
2) "niuer" # value of domain name
3) "age" # domain
4) "34" # value of the age Field
Redis 127.0.0.1: 6379> hgetall user.3 # nonexistent hansh table
(Empty list or set)
D) hexists
Syntax: hexists key field
Explanation: Determine whether a field exists in the hash table.
[Plain]
Redis 127.0.0.1: 6379> hexists user.1 name # exists
(Integer) 1
Redis 127.0.0.1: 6379> hexists user.1 nofield # does not exist
(Integer) 0
Redis 127.0.0.1: 6379> hexists use1 nofield # the hash table does not exist.
(Integer) 0
E) hkeys
Syntax: hkeys key
Explanation: obtain all the domains of the hash table.
[Plain]
Redis 127.0.0.1: 6379> hkeys user.1 # existing hash table
1) "name"
2) "age"
3) "tech"
4) "fri"
Redis 127.0.0.1: 6379> hkeys user.4 # nonexistent hash
(Empty list or set)
F) hvals
Syntax: hvals key
Explanation: obtain all the domain values of the hash table.
[Plain]
Redis 127.0.0.1: 6379> hvals user.1 # hash exists
1) "zhangsan"
2) "45"
3) "lisi"
4) "5"
Redis 127.0.0.1: 6379> hvals user.4 # does not exist
(Empty list or set)
3) modify
Syntax: hincrby key field increment
Explanation: the value of the field in the hash table increases the step size (increment). If increment is a negative value, it decreases. If the domain does not exist, the initial value is 0.
[Plain]
Redis 127.0.0.1: 6379> hincrby user.1 age 2 # Add 2
(Integer) 47
Redis 127.0.0.1: 6379> hincrby user.1 age-3 #3 fewer
(Integer) 44
Redis 127.0.0.1: 6379> hincrby user.1 age2-3 # The domain cannot exist and the initial value is 0.
(Integer)-3
4) Delete
Syntax: hdel key field [field]
Explanation: deletes hash fields. If multiple fields are specified, multiple fields are deleted.
[Plain]
Redis 127.0.0.1: 6379> hkeys user.1
1) "name"
2) "age"
3) "tech"
4) "fri"
5) "age2"
Redis 127.0.0.1: 6379> hdel user.1 age2 # delete a domain
(Integer) 1
Redis 127.0.0.1: 6379> hkeys user.1
1) "name"
2) "age"
3) "tech"
4) "fri"
Redis 127.0.0.1: 6379> hdel user.1 fri tech # Delete two domains
(Integer) 2
Redis 127.0.0.1: 6379> hkeys user.1
1) "name"
2) "age"
Redis 127.0.0.1: 6379> hdel user.1 bb # delete a nonexistent domain
(Integer) 0 # Return 0
5) Others
Syntax: hlen key
Explanation: obtain the number of hash Fields
[Plain]
Redis 127.0.0.1: 6379> hkeys user.1
1) "name"
2) "age"
Redis 127.0.0.1: 6379> hlen user.1 # two domains exist
(Integer) 2
Redis 127.0.0.1: 6379> hlen user.4 # nonexistent hash
(Integer) 0