Redis basic data type (hash hash)
- hash
-
- Storage type
- Advantages and disadvantages with String
- Common operation commands
-
- hset: set value
- hget: Get the value
- hmset: set multiple values
- hmget: Get multiple values
- hkeys: Get all the key values of the hash
- hvals: get all value values of key
- hgetall: Get all the field and value values of the key
- hdel: delete the field specified in the key
- hlen: Get the number of fields contained in the hash
- hexists: Determine whether the hash exists
- hincrby: the integer value in the hash increases
- hincrbyfloat: Increase in floating point number in hash
- hsetnx
- hstrlen: Get the string length of the value of the hash specified field
hash
Storage type
Store multiple unordered key-value pairs, the maximum storage amount is 2^32-1 (about 4 billion), value can only be a string, and other types cannot be nested
Advantages and disadvantages with String
- advantage
- Gather all relevant values into one key to save memory space
- Use only one key to reduce key conflicts
- When you need to get values in batches, you only need to use one command to reduce memory/IO/CPU consumption
- Disadvantage
- Field cannot set expiration time separately
- Need to consider the distribution of data volume (when there are a lot of fields, it cannot be distributed to multiple nodes)
Common operation commands
hset: set value
-
command
HSET key field value
-
Description
Set the value of the specified field in the hash set specified by key.
If the hash set specified by the key does not exist, a new hash set will be created and associated with the key.
If the field exists in the hash set, it will be overwritten.
-
return value
- 1-if field is a new field
- 0-If the field originally existed in the map
-
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HGET myhash field1 "Hello" redis>
hget: Get the value
-
command
HGET key field
-
Description
Returns the value associated with the field in the hash set specified by key
-
return value
The value associated with this field. When the field does not exist or the key does not exist, nil is returned.
-
example
redis> HSET myhash field1 "foo" (integer) 1 redis> HGET myhash field1 "foo" redis> HGET myhash field2 (nil) redis>
hmset: set multiple values
-
command
HMSET key field value [field value ...]
-
Description
Set the
key
value of the specified concentration of the specified field hashes. This command will rewrite all the fields that exist in the hash set. If thekey
specified hash set does not exist, create a new hash set and thekey
associated -
example
redis> HMSET myhash field1 "Hello" field2 "World" OK redis> HGET myhash field1 "Hello" redis> HGET myhash field2 "World" redis>
hmget: Get multiple values
-
command
HMGET key field [field ...]
-
Description
Returns the
key
value of the specified concentration of the specified field hashes.For each hash focused field does not exist, the return
nil
value. Because they do not exist keys is considered to be an empty hash set to a non-existentkey
executionHMGET
will return only contain anil
list of values -
return value
Contains a list of the given fields and their values, and keeps the same order as the request.
-
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HMGET myhash field1 field2 nofield 1) "Hello" 2) "World" 3) (nil) redis>
hkeys: Get all the key values of the hash
-
command
HKEYS key
-
Description
Returns the names of all fields in the hash set specified by key.
-
return value
The list of fields in the hash set, an empty list is returned when the hash set specified by key does not exist.
-
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HKEYS myhash 1) "field1" 2) "field2" redis>
hvals: get all value values of key
-
command
HVALS key
-
Description
Returns the values of all fields in the hash set specified by key.
-
return value
A list of values in the hash set. When the hash set specified by key does not exist, an empty list is returned.
-
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HVALS myhash 1) "Hello" 2) "World" redis>
hgetall: Get all the field and value values of the key
-
command
HGETALL key
-
Description
Returns all the fields and values in the hash set specified by key. In the return value, the next of each field name is its value, so the length of the return value is twice the size of the hash set
-
return value
A list of fields and values in the hash set. When the hash set specified by key does not exist, an empty list is returned.
-
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HGETALL myhash 1) "field1" 2) "Hello" 3) "field2" 4) "World" redis>
hdel: delete the field specified in the key
-
command
HDEL key field [field ...]
-
Description
Remove the specified domain from the hash set specified by key. Domains that do not exist in the hash set will be ignored.
If the hash set specified by key does not exist, it will be regarded as an empty hash set, and the command will return 0.
-
return value
Returns the number of domains successfully removed from the hash set, excluding those domains that are indicated but do not exist
-
example
redis> HSET myhash field1 "foo" (integer) 1 redis> HDEL myhash field1 (integer) 1 redis> HDEL myhash field2 (integer) 0 redis>
hlen: Get the number of fields contained in the hash
-
command
HLEN key
-
Description
Returns
key
the number of fields included in the hash set specified. -
return value
The number of fields in the hash, as
key
0 if the specified hash set does not exist -
example
redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HLEN myhash (integer) 2 redis>
hexists: Determine whether the hash exists
-
command
HEXISTS key field
-
Description
Returns whether the field exists in the hash
-
return value
- 1 --hash contains the field.
- 0 --hash does not contain the field or the key does not exist.
-
example
redis> HSET myhash field1 "foo" (integer) 1 redis> HEXISTS myhash field1 (integer) 1 redis> HEXISTS myhash field2 (integer) 0 redis>
hincrby: the integer value in the hash increases
-
command
HINCRBY key field increment
-
Description
Increase
key
specified hash centralized specified numeric fields. Ifkey
does not exist, create a new hash set and with thekey
association. If the field does not exist, the value of the field is set to 0 before the operation is performedHINCRBY
The range of supported values is limited to 64-bit signed integers -
return value
The value of this field after the value-added operation is executed.
-
example
redis> HSET myhash field 5 (integer) 1 redis> HINCRBY myhash field 1 (integer) 6 redis> HINCRBY myhash field -1 (integer) 5 redis> HINCRBY myhash field -10 (integer) -5 redis>
hincrbyfloat: Increase in floating point number in hash
-
command
HINCRBYFLOAT key field increment
-
Description
Perform a float type addition for the specified
key
hashfield
field valueincrement
. If itfield
does not exist, set it to 0 before performing the operation. If one of the following situations occurs, an error is returned:field
The value contains the wrong type (not a string).- Currently
field
orincrement
cannot be parsed as a float type.
-
return value
field
Implementation ofincrement
value added -
example
redis> HSET mykey field 10.50 (integer) 1 redis> HINCRBYFLOAT mykey field 0.1 "10.6" redis> HSET mykey field 5.0e3 (integer) 0 redis> HINCRBYFLOAT mykey field 2.0e2 "5200" redis>
hsetnx
-
command
HSETNX key field value
-
Description
Only
key
when the specified hash concentration specified field does not exist, setting the value of the field. If thekey
specified hash set does not exist, create a new hash set and with thekey
association. If the field already exists, this operation has no effect. -
return value
- 1-If the field is a new field and successfully assigned
- 0-If the field already exists in the hash set, no operation is performed
-
example
redis> HSETNX myhash field "Hello" (integer) 1 redis> HSETNX myhash field "World" (integer) 0 redis> HGET myhash field "Hello" redis>
hstrlen: Get the string length of the value of the hash specified field
-
command
HSTRLEN key field
-
return value
Returns the string length of the value of the hash specified field. If the hash or field does not exist, return 0.
-
example
redis> HMSET myhash f1 HelloWorld f2 99 f3 -256 OK redis> HSTRLEN myhash f1 (integer) 10 redis> HSTRLEN myhash f2 (integer) 2 redis> HSTRLEN myhash f3 (integer) 4 redis>