PHP method and code example for operating hash and zset type data in Redis

1,hset

Description: set the value of the field in the hash table key to value. If the key does not exist, a new hash table is created and 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 the 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 in the hash table key to value if and only if the field does not exist. If the field already exists, the 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: set successfully, return 1. If the given domain already exists and no operation is 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 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.
Parameter: key field value [field value...]
Return value: if the command is executed successfully, it returns OK. When the key is not of 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 domain does not exist in the hash table, a nil value is returned. Because nonexistent keys are treated as an empty hash table, HMGET on a nonexistent key will return a table with only nil values.
Parameter: key field [field...]
Return value: a table containing the associated values of multiple given fields. The table values are arranged in the same order as the request order of the given field parameters.

6,hgetall

Description: returns all fields and values in the hash table key. In the return value, the value of the domain is immediately followed by each field name, so the length of the return value is twice the size of the hash table.
Parameter: key
Return value: returns the field and the value of the field of the hash table in the form of a list. If the key does not exist, an empty list is returned.

7,hdel

Description: delete one or more specified fields in the hash table key. Nonexistent fields will be ignored.
Parameter: key field [field...]
Return value: the number of domains successfully removed, excluding the 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 field exists in the hash table key.
Parameter: key field
Return value: returns 1 if the hash table contains the given field. 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. The increment can also be negative, which is equivalent to subtracting a given field.
Parameter: key field increment
Return value: the value of the field in the hash table key after the HINCRBY command is executed.

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 of the above 12 methods:


<?php  

$redis = new redis();  

$redis->connect('192.168.1.108', 6379);  

$redis->delete('test');  

$redis->hset('test', 'key1', 'hello');  

echo $redis->hget('test', 'key1');     //Result: hello   

  

echo "<br>";  

$redis->hSetNx('test', 'key1', 'world');  

echo $redis->hget('test', 'key1');   //Result: hello   

  

$redis->delete('test');  

$redis->hSetNx('test', 'key1', 'world');  

echo "<br>";  

echo $redis->hget('test', 'key1');   //Result: world   

  

echo $redis->hlen('test');   //Results: 1   

var_dump($redis->hdel('test','key1'));  //Result: bool(true)    

  

$redis->delete('test');  

$redis->hSet('test', 'a', 'x');  

$redis->hSet('test', 'b', 'y');  

$redis->hSet('test', 'c', 'z');  

print_r($redis->hkeys('test'));  //Result: array ([0] = > a [1] = > b [2] = > C)    

  

print_r($redis->hvals('test'));  //Result: array ([0] = > x [1] = > y [2] = > z)    

  

print_r($redis->hgetall('test'));  //Result: array ([a] = > x [b] = > y [C] = > z)    

  

var_dump($redis->hExists('test', 'a'));  //Result: bool(true)    

  

$redis->delete('test');  

echo $redis->hIncrBy('test', 'a', 3);    //Result: 3   

echo $redis->hIncrBy('test', 'a', 1);    //Result: 4   

  

$redis->delete('test');  

var_dump($redis->hmset('test', array('name' =>'tank', 'sex'=>"man"))); //Result: bool(true)   

print_r($redis->hmget('test', array('name', 'sex')));  //Result: array ([name] = > tank [sex] = > man)   

?>  

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 sorting elements in a specific range. 0 represents the first element, 1 represents the second element, and so on- 1 stands for the last, and - 2 stands for the penultimate
Parameters:
key
start: long
end: long
withscores: bool = false
Return value: array

15,zdelete, zrem

Description: deletes a specified member from an ordered collection.
Parameters:
key
member
Return value: 1 or 0

16,zrevrange

Description: returns all elements of the specified interval in the ordered set corresponding to the key. These elements are arranged in the order of score from high to low. Elements with the same score are arranged in descending dictionary 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 with a score between min and Max in the ordered set corresponding to the key (including elements with a score equal to min or max). The elements are arranged in the order of score from low to high. If the elements have the same score, they are arranged in dictionary order.
The optional option LIMIT can be used to obtain a range of matching elements. If the offset value is large, the ordered set needs to be traversed before obtaining the elements to be returned, which will increase the time complexity of O(N). The optional option with scores enables you to return the score of the element while returning the element. This option has been available since redis version 2.0.
Parameters:
key
start: string
end: string
options: array
Return value: array

18,zcount

Description: returns the number of elements in the ordered set corresponding to the key between min and max.
Parameters:
key
start: string
end: string
Return value: array length

19,zremrangebyscore, zreleterangebyscore

Description: removes all elements in the ordered set corresponding to the key where scroe is between min and max (including endpoints). Since version 2.1.6, interval endpoints min and Max can be excluded, which is the same as the syntax of 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: removes all elements with rank values between start and stop in the ordered set corresponding to key. Both start and stop start from 0, and both can be negative. When the index value is negative, it indicates that the offset value starts from the element with the highest score value in the ordered set. For example: - 1 represents the element with the highest score, while - 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 the 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 the member does not exist in the ordered collection, 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) starts at 0, which means that the rank value of the element with the lowest score value is 0. Using ZREVRANK, you can obtain the rank (or index) of elements arranged from high to low.
Parameter: key member
Return value: number

24,zincrby

Add increment to the scroe of the member element in the ordered set corresponding to key. If the specified member does not exist, the element will be added, and the initial value of its score is increment. If the key does not exist, a new ordered list will be created, which contains the unique 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 receive a double precision floating-point number. At the same time, you can also provide a negative value, which will reduce the value of score.
Parameter: key value member
Return value: character data

25,zunion

Description: numkeys are ordered sets corresponding to keys, the set is calculated, and the results are stored in destination
Parameters: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return value: Union array

26,zinter

Description: the numkeys ordered sets corresponding to keys calculate the intersection and store the results in destination
Parameters: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return value: intersection array


Code example of 13-26:


$redis = new redis();  

$redis->connect('192.168.1.108', 6379);  

$redis->delete('test');  

$redis->zadd('test', 1, 'val1');  

$redis->zadd('test', 0, 'val2');  

$redis->zadd('test', 3, 'val3');  

  

print_r($redis->zrange('test', 0, -1)); //Result: array ([0] = > val2 [1] = > val1 [2] = > val3)   

  

$redis->zdelete('test', 'val2');  

print_r($redis->zrange('test', 0, -1)); //Result: array ([0] = > val1 [1] = > val3)    

  

$redis->zadd('test',4, 'val0');  

print_r($redis->zrevrange('test', 0, -1));  //Result: array ([0] = > val0 [1] = > val3 [2] = > val1)   

print_r($redis->zrevrange('test', 0, -1,true));  //Result: array ([val0] = > 4 [val3] = > 3 [val1] = > 1)    

  

echo "<br>";  

$redis->zadd('key', 0, 'val0');  

$redis->zadd('key', 2, 'val2');  

$redis->zadd('key', 10, 'val10');  

  

print_r($redis->zrangebyscore('key', 0, 3, array('limit' => array(1, 1),'withscores' => TRUE))); //Result: array ([val2] = > 2)   

print_r($redis->zrangebyscore('key', 0, 3, array('limit' => array(1, 1)))); //Result: array ([0] = > val2)    

  

echo $redis->zcount('key', 0, 3); //Results: 2   

  

$redis->zremrangebyscore('key', 0, 3);  

print_r($redis->zrange('key', 0, -1));  //Result: array ([0] = > val10)    

  

echo $redis->zsize('key');   //Results: 1   

  

$redis->zadd('key', 2.5, 'aaaa');  

echo $redis->zscore('key', 'aaaa');   //Result: 2.5   

  

echo $redis->zrank('key', 'aaaa');   //Result: 0   

echo $redis->zrevrank('key', 'aaaa');    //Results: 1   

  

$redis->delete('key');  

  

echo $redis->zincrby('key', 2, 'aaaa');  //Results: 2   

echo $redis->zincrby('key', 1, 'aaaa');  //Result: 3   

  

$redis->delete('key');  

$redis->delete('test');  

  

$redis->zadd('key', 0, 'val0');  

$redis->zadd('key', 1, 'val1');  

$redis->zadd('key', 4, 'val2');  

$redis->zadd('test', 2, 'val2');  

$redis->zadd('test', 3, 'val3');  

$redis->zunion('k01', array('key', 'test'));  

print_r($redis->zrange('k01',0, -1)); //Result: array ([0] = > val0 [1] = > val1 [2] = > val3 [3] = > val2)   

  

$redis->zunion('k03', array('key', 'test'), array(5, 1));  

print_r($redis->zrange('k03',0, -1)); //Result: array ([0] = > val0 [1] = > val3 [2] = > val1 [3] = > val2)    

  

$redis->zinter('k02', array('key', 'test'));  

print_r($redis->zrange('k02',0, -1)); //Result: array ([0] = > val2)   

?>  

Keywords: PHP Redis

Added by wvwisokee on Fri, 19 Nov 2021 19:53:01 +0200