Home >Backend Development >PHP Tutorial >Methods and code examples for php to operate hash and zset type data in redis_PHP tutorial

Methods and code examples for php to operate hash and zset type data in redis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:43903browse

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:

Copy code The code is as follows:

$redis = new redis(); 
$redis->connect('192.168.1.108', 6379); 
$redis->delete('test'); 
$redis->hset('test', 'key1', 'hello'); 
echo $redis->hget('test', 'key1');     //结果:hello 
 
echo "
"; 
$redis->hSetNx('test', 'key1', 'world'); 
echo $redis->hget('test', 'key1');   //结果:hello 
 
$redis->delete('test'); 
$redis->hSetNx('test', 'key1', 'world'); 
echo "
"; 
echo $redis->hget('test', 'key1');   //结果:world 
 
echo $redis->hlen('test');   //结果:1 
var_dump($redis->hdel('test','key1'));  //结果: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'));  //结果:Array ( [0] => a [1] => b [2] => c )  
 
print_r($redis->hvals('test'));  //结果:Array ( [0] => x [1] => y [2] => z )  
 
print_r($redis->hgetall('test'));  //结果:Array ( [a] => x [b] => y [c] => z )  
 
var_dump($redis->hExists('test', 'a'));  //结果:bool(true)  
 
$redis->delete('test'); 
echo $redis->hIncrBy('test', 'a', 3);    //结果:3 
echo $redis->hIncrBy('test', 'a', 1);    //结果:4 
 
$redis->delete('test'); 
var_dump($redis->hmset('test', array('name' =>'tank', 'sex'=>"man"))); //结果:bool(true) 
print_r($redis->hmget('test', array('name', 'sex')));  //结果: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 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:

Copy code The code is as follows:

$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 "
";
$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); //Result: 2

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

echo $redis->zsize('key'); //Result: 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'); //Result: 1

$redis->delete('key');

echo $redis->zincrby('key', 2, 'aaaa'); //Result: 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 )
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824605.htmlTechArticleThe previous blog is mainly about string type, list type and set type. 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,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn