Home > Article > Backend Development > Methods and code examples for php to operate hash and zset type data in redis_PHP tutorial
The previous blog mainly focuses on string type, list type and set type, and the following hash type and zset type
1,hset
Description: Set the value of the field field in the hash table key to value. If the key does not exist, a new hash table is created and the HSET operation is performed. If the field already exists in the hash table, the old value will be overwritten.
Parameter: key field value
Return value: If field is a new field in the hash table and the value is set successfully, 1 is returned. If the field in the hash table already exists and the old value has been overwritten by the new value, 0 is returned.
2,hsetnx
Description: Set the value of the field field in the hash table key to value if and only if the field field does not exist. If the domain field already exists, this operation is invalid. If the key does not exist, a new hash table is created and the HSETNX command is executed.
Parameter: key field value
Return value: If the setting is successful, 1 is returned. If the given domain already exists and no operations were performed, 0 is returned.
3, hget
Description: Returns the value of the given field in the hash table key.
Parameter: key field
Return value: The value of the given field. When the given domain does not exist or the given key does not exist, nil is returned.
4,hmset
Description: Set multiple field-value (field-value) pairs to the hash table key at the same time. This command overwrites fields that already exist in the hash table. If the key does not exist, an empty hash table is created and the HMSET operation is performed.
Parameters: key field value [field value ...]
Return value: If the command is executed successfully, OK is returned. When the key is not a hash table (hash) type, an error is returned.
5,hmget
Description: Returns the value of one or more given fields in the hash table key. If the given field does not exist in the hash table, a nil value is returned. Because a non-existent key is treated as an empty hash table, a HMGET operation on a non-existent key will return a table with only nil values.
Parameters: key field [field ...]
Return value: A table containing multiple associated values for a given domain, arranged in the same order as the requested domain parameters.
6,hgetall
Description: Returns all fields and values in the hash table key. In the return value, immediately following each field name is the value of the field, so the length of the return value is twice the size of the hash table.
Parameter: key
Return value: Returns the domain and domain value of the hash table in list form. If key does not exist, an empty list is returned.
7,hdel
Description: Delete one or more specified fields in the hash table key. Non-existing fields will be ignored.
Parameters: key field [field ...]
Return value: The number of successfully removed domains, excluding ignored domains.
8,hlen
Description: Returns the number of fields in the hash table key.
Parameter: key
Return value: The number of fields in the hash table. When the key does not exist, 0 is returned.
9,hexists
Description: Check whether the given domain field exists in the hash table key.
Parameter: key field
Return value: If the hash table contains the given field, return 1. If the hash table does not contain the given field, or the key does not exist, 0 is returned.
10,hincrby
Description: Add increment to the value of the field in the hash table key. Increment can also be negative, which is equivalent to subtracting a given domain.
Parameters: key field increment
Return value: After executing the HINCRBY command, the value of the field in the hash table key.
11,hkeys
Description: Returns all fields in the hash table key.
Parameter: key
Return value: A table containing all fields in the hash table. When the key does not exist, an empty table is returned.
12,hvals
Description: Returns all values in the hash table key.
Parameter: key
Return value: A table containing all the values in the hash table. When the key does not exist, an empty table is returned.
Code examples for the above 12 methods:
13,zadd
Description:
Add one or more elements. If the element already exists, update its socre value
Although an ordered set is ordered, it is also a set and cannot duplicate elements. Adding duplicate elements will only
Update the score value of the original element
Parameters:
key
score: double
value: string
Return value: 1 or 0
14, zrange
Description: Get the sorted elements within a specific range, 0 represents the first element, 1 represents the second element, and so on. -1 represents the last one, -2 represents the penultimate one...
Parameters:
key
start: long
end: long
withscores: bool = false
Return value: array
15, zdelete, zrem
Description: Remove the specified member from the ordered set.
Parameters:
key
member
Return value: 1 or 0
16,zrevrange
Description: Returns all elements in the specified range in the ordered set corresponding to key. These elements are arranged in order from high to low score. Elements with the same score will be sorted in descending lexicographic order. This command is similar to ZRANGE, except that the order of elements in this command is different from the former.
Parameters:
key
start: long
end: long
withscores: bool = false
Return value: array
17, zrangebyscore, zrevrangebyscore
Description: Returns all elements in the ordered set corresponding to key whose score is between min and max (including elements whose score is equal to min or max). The elements are arranged in order from low to high score. If elements have the same score, they are sorted in lexicographic order.
The optional LIMIT option can be used to obtain matching elements within a certain range. If the offset value is large, the sorted collection needs to be traversed before obtaining the element to be returned, thus increasing the time complexity of O(N). The optional option WITHSCORES allows the element's score to be returned along with the element. This option is available since Redis 2.0.
Parameters:
key
start: string
end: string
options: array
Return value: array
18, zcount
Description: Returns the number of elements between min and max in the ordered set corresponding to key.
Parameters:
key
start: string
end: string
Return value: array length
19, zremrangebyscore, zreleterangebyscore
Description: Remove all elements whose scree is between min and max (including endpoints) in the sorted set corresponding to key. Starting from version 2.1.6, the interval endpoints min and max can be excluded, which is the same syntax as ZRANGEBYSCORE.
Parameters:
key
start: double or "+inf" or "-inf" string
end: double or "+inf" or "-inf" string
Return value: Number of deleted elements
20, zremrangebyrank, zdeleterangebyrank
Description: Remove all elements whose rank value is between start and stop in the sorted set corresponding to key. Both start and stop start from 0, and both can be negative values. When the index value is negative, it indicates that the offset value starts from the element with the highest score value in the sorted set. For example: -1 represents the element with the highest score, and -2 represents the element with the second-highest score, and so on.
Parameters:
key
start: LONG
end: LONG
Return value: Number of deleted elements
21, zsize, zcard
Description: Returns the number of elements stored in the ordered set corresponding to key.
Parameter: key
Return value: Number of elements
22, zscore
Description: Returns the score value of the member in the ordered set corresponding to the key. If member does not exist in the sorted set, null will be returned.
Parameter: key member
23,zrank, zrevrank
Description: Returns the index value of the member element in the ordered set corresponding to the key. The elements are arranged from low to high according to the score. The rank value (or index) is 0-based, which means that the element with the lowest score value has a rank value of 0. Use ZREVRANK to get the rank (or index) of elements arranged from high to low.
Parameter: key member
Return value: Number
24, zinccrby
Add increment to the scroe of the member element in the ordered set corresponding to the key. If the specified member does not exist, the element will be added and its initial score will be increment. If key does not exist, a new ordered list will be created containing the only element member. If the value corresponding to the key is not an ordered list, an error will occur. The value of the specified score should be a string that can be converted to a numeric value, and accepts double-precision floating point numbers. Also, you can provide a negative value, which will reduce the score value.
Parameter: key value member
Return value: character data
25, zunion
Description: Calculate the collection of numkeys ordered sets corresponding to keys, and store the results in destination
Parameters: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return value: Union array
26, zinter
Description: Calculate the intersection of numkeys ordered sets corresponding to keys, and store the results in destination
Parameters: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return value: intersection array
Code example for 13-26: