In Redis, the hash table is a data structure that maps string type fields and corresponding values. Particularly suitable for storing objects, each hash can store more than 4 billion key-value pairs.
Children's shoes who are familiar with python can think of it as a dictionary dict. The previous data type storage was k-v, and the hash storage is k-dict
, and dict
will have its own k-v
.
Assign values to the fields in the hash table. If the hash table does not exist, create a new hash table and perform the hset operation.
If the field already exists in the hash table, the old value will be overwritten.
hset myhash k1 v1
Returns the value of the specified field in the hash table. If the given field or key does not exist, nil is returned.
hget myhash k1
Set multiple field-value pairs into the hash table at the same time.
hmset myhash k2 v2 k3 v3
If the field already exists in the hash table, it will be overwritten.
If the hash table does not exist, an empty hash table will be created and the hset operation will be performed.
Returns the value of one or more given fields in the hash table.
hmget myhash k1 k2 k3 k4
If the specified field does not exist in the hash table, then a nil value is returned.
Returns all fields and values in the hash table.
hgetall myhash
Note that in the return value, immediately following each field name (field name) is the value of the field (value), so the length of the return value is twice the size of the hash table.
Delete one or more specified fields in the hash table key. Non-existing fields will be ignored.
hdel myhash k2 k3 k5
Returns the number of successfully deleted fields, excluding ignored fields.
Get the number of fields in the hash table.
hlen myhash
Check whether the specified field of the hash table exists.
hexists myhash k1
If the hash table contains the given field, return 1.
If the hash table does not contain the given field, or key does not exist, return 0.
Get all fields in the hash table.
hkeys myhash
Contains a list of all fields in the hash table. When key does not exist, an empty list is returned.
Returns the values of all fields in the hash table.
hvals myhash
Returns a list containing all field values in the hash table. When key does not exist, an empty table is returned.
Adds the specified increment value to the field value in the hash table. This increment can also be a negative number, which is equivalent to Subtraction.
If the key of the hash table does not exist, create a new hash table and execute the hincrby
command.
If the specified field does not exist, the value of the field will be initialized to 0 before executing the command.
If executed on a field that stores a string value, an error will be reported.
Assign values to fields that do not exist in the hash table.
If the hash table does not exist, create a new hash table and perform hset operation.
If the field already exists in the hash table, the operation is invalid.
If the key does not exist, create a new hash table and execute the hsetnx
command.
Regarding the application of hash in redis, such as saving user information data and frequently changing information, if you do not want to use traditional k-v objects to store, you can use redis hash.
The above is the detailed content of Analysis of common operation examples of Hash, the basic data type of Redis. For more information, please follow other related articles on the PHP Chinese website!