Methods for hashing and Zset type data in PHP operations Redis

Source: Internet
Author: User
Tags array length redis

Composer require Predis/predis

COMPOSER-VVV Update

-----------------------------------------------------above for Laravel Install the Redis----------------------------------------------------------------------------------------------------

Redis is an open source, with the use of ANSI C language, support network, can be based on memory can also be persistent log-type, Key-value database, and provide a variety of language APIs.

This article mainly explains the PHP operation Redis the hash and the Zset type data method and the example code, has introduced 26 groups of functions or the method, the interest schoolmate consults under.

1,hset

Description: Sets the value of the field field in the hash table key to values. If key does not exist, a new hash table is created and Hset. If the field field already exists in the hash table, the old value is overwritten.
Parameters: Key field value
Return value: If field is a new field in the hash table, and the value setting succeeds, returns 1. Returns 0 if the field field in the hash table already exists and the old value is overwritten by the new value.

2,hsetnx

Description: Sets 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 field field already exists, the operation is invalid. If key does not exist, a new hash table is created and the HSETNX command is executed.
Parameters: Key field value
Return value: Set succeeded, return 1. Returns 0 if the given domain already exists and no action is executed.

3,hget

Description: Returns the value of the given field field in the hash table key.
Parameters: Key Field
Return value: The value of the given field. Returns nil when a given domain does not exist or if a given key does not exist.

4,hmset

Description: Sets multiple field-value (field-value) pairs to the hash table key at the same time. This command overwrites a domain that already exists in the hash table. If key does not exist, an empty hash table is created and performs a hmset operation.
Parameters: Key field value [field value ...]
Return value: If the command executes successfully, return OK. An error is returned when the key is not a hash table (hash) type.

5,hmget

Description: Returns the value of one or more given fields in the hash table key. Returns a nil value if the given domain does not exist in the hash table. Because a nonexistent key is treated as an empty hash table, a hmget operation on a nonexistent key returns a table with only a nil value.
Parameters: Key field [Field ...]
Return value: A table that contains the associated values for multiple given fields, and the order of the table values is the same as the order of the request for a given field parameter.

6,hgetall

Description: Returns all fields and values in the hash table key. In the return value, the value of the field immediately following each domain name (field name), so the length of the return value is twice times the size of the hash table.
Parameters: Key
Return value: Returns the value of the field and field of the hash table as a list. If key does not exist, returns an empty list.

7,hdel

Description: Deletes one or more of the specified fields in the hash table key, and the nonexistent fields are ignored.
Parameters: Key field [Field ...]
Return value: The number of fields that were successfully removed, excluding fields that were ignored.

8,hlen

Description: Returns the number of fields in the hash table key.
Parameters: Key
Return value: The number of fields in the hash table. Returns 0 when key does not exist.

9,hexists

Description: View whether a given field field exists in the hash table key.
Parameters: Key Field
Return value: Returns 1 if the hash table contains a given field. Returns 0 if the hash table does not contain the given domain, or key does not exist.

10,hincrby

Description: Adds increment increment to the value of the field field in the hash table key. An increment can also be a negative number, which is equivalent to subtracting a given field.
Parameters: Key field increment
Return value: The value of the field field in the hash table key after executing the Hincrby command.

11,hkeys

Description: Returns all the fields in the hash table key.
Parameters: Key
Return value: A table that contains all the fields in the hash table. Returns an empty table when the key does not exist.

12,hvals

Description: Returns all the values in the hash table key.
Parameters: Key
Return value: A table that contains all the values in the hash table. Returns an empty table when the key does not exist.

Code examples for the above 12 methods:

The code is as follows:
<?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); Results: 3
echo $redis->hincrby (' Test ', ' a ', 1); Results: 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

Describe:
Add one or more elements, and if the element already exists, update its socre value
Although an ordered set is ordered, it is also a collection and cannot repeat 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: Gets the sort element within a specific range, 0 represents the first element, 1 represents the second, and so on. -1 for the last one, 2 for the penultimate ...
Parameters:
Key
Start:long
End:long
Withscores:bool = False
return value: Array

15,zdelete, Zrem

Description: Deletes the specified member from the ordered collection.
Parameters:
Key
Member
return value: 1 or 0

16,zrevrange

Description: Returns all elements of the specified interval in the ordered set of key. These elements are arranged in the order of score from highest to lowest. For elements with the same score, they will be sorted in descending dictionary order. This command is similar to Zrange, except that the elements in the command are arranged in a different order than the former.
Parameters:
Key
Start:long
End:long
Withscores:bool = False
return value: Array

17,zrangebyscore, Zrevrangebyscore

Description: Returns all elements between Min and Max (Kit Harington score equals min or Max) in an ordered set corresponding to the key score. Elements are arranged in the order of score from lowest to highest. If the elements have the same score, they are sorted in the dictionary order.
Optional options limit can be used to get a range of matching elements. If the offset value is large, the ordered collection needs to be traversed before obtaining the element to be returned, thus increasing the time complexity of O (N). Optional Withscores allows you to return the element's score while returning the element, which is available since the Redis 2.0 version.
Parameters:
Key
Start:string
End:string
Options:array
return value: Array

18,zcount

Description: Returns the number of elements between Min and Max that correspond to the ordered set of key.
Parameters:
Key
Start:string
End:string
Return value: Array length

19,zremrangebyscore, Zreleterangebyscore

Description: Removes the Scroe element between Min and Max (which contains endpoints) in the ordered set of key equivalents. Starting with version 2.1.6, the interval endpoint min and Max can be excluded, as is the case with Zrangebyscore syntax.
Parameters:
Key
Start:double or "+inf" or "-inf" string
End:double or "+inf" or "-inf" string
Return value: Delete element count

20,zremrangebyrank, Zdeleterangebyrank

Description: Removes all elements in the ordered set of key corresponding to the rank value between start and stop. Start and stop start at 0, and both can be negative. When the index value is negative, it indicates that the offset value starts with the element with the highest score value in the ordered collection. For example: 1 represents the element with the highest score, and 2 represents the element with the secondary high score, and so on.
Parameters:
Key
Start:long
End:long
Return value: Delete element count

21,zsize, Zcard

Description: Returns the number of elements stored in the ordered set corresponding to the key.
Parameters: Key
Return value: Number of elements

22,zscore

Description: Returns the score value of member in the ordered set corresponding to the key. If member does not exist in an ordered collection, then NULL is returned.
Parameters: Key Member

23,zrank, Zrevrank

Description: Returns the index value of the member element in the ordered set corresponding to the key, and the elements are arranged in score from lowest to highest. The rank value (or index) starts at 0, which means that the rank value of the element with the lowest score value is 0. Use Zrevrank to get rank (or index) of elements that are arranged from highest to lowest.
Parameters: Key Member
Return value: Number

24,zincrby

Scroe of the member element in the ordered set corresponding to the key plus increment. If the specified member does not exist, the element is added and its score initial value is increment. If key does not exist, a new ordered list will be created that contains the unique element of member. If the key corresponds to a value that is not a sequence table, an error occurs. 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 score values.
Parameter: Key value member
Return value: Character type data

25,zunion

Description: The keys corresponding to the Numkeys ordered set to compute the collection and store the results in destination
Parameters: KeyOutput Arrayzsetkeys arrayweights aggregatefunction
Return value: An array of sets of

26,zinter

Description: The keys corresponding to the Numkeys ordered set compute the intersection and store the results in the destination
Parameters: KeyOutput Arrayzsetkeys arrayweights aggregatefunction
Return value: An array of intersections


13-26 code Example: 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 "<br>";
$redis->zadd (' key ', 0, ' val0 ');
$redis->zadd (' key ', 2, ' val2 ');
$redis->zadd (' key ', ten, ' 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 '); Results: 2.5

echo $redis->zrank (' key ', ' aaaa '); Results: 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 '); Results: 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)); Results: 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)); Results: 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)
?>

Go to: http://www.phperz.com/article/14/0820/4192.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.