Php methods and code examples for operating hash and zset data in redis

Source: Internet
Author: User

The previous blog mainly belongs to the string, list, and set types. The following are hash and zset types.

1, hset

Description: sets the field value in the key field of the hash table to value. If the key does not exist, a new hash table is created and HSET is performed. If the field already exists in the hash table, the old value will be overwritten.
Parameter: key field value
Returned 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 is overwritten by the new value, 0 is returned.

2, hsetnx

Description: sets the field value in the key of the hash table to value. If the field does not exist. This operation is invalid if the field already exists. 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. 1 is returned. If the specified domain already exists and no operation is executed, 0 is returned.

3, hget

Description: return the field value of the given field in the hash table key.
Parameter: key field
Returned value: the value of the given field. If the specified domain does not exist or the specified key does not exist, nil is returned.

4, hmset

Description: sets multiple field-value pairs to the hash table key at the same time. This command overwrites the existing fields in the hash table. If the key does not exist, an empty hash table is created and HMSET is executed.
Parameter: key field value [field value...]
Return Value: If the command is successfully executed, OK is returned. If the key type is not hash, an error is returned.

5, hmet

Description: return the values of one or more given fields in the hash table key. If the given domain does not exist in the hash table, an nil value is returned. Because a key that does not exist is processed as an empty hash table, performing the hmet operation on a non-existent key will return a table with only nil values.
Parameter: key field [field...]
Return Value: a table that contains the associated values of multiple given domains. The table values are arranged in the same order as the request order for the given domain parameters.

6. hgetall

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

7. hdel

Description: deletes one or more specified fields in the hash table key. nonexistent fields are ignored.
Parameter: key field [field...]
Returned value: the number of successfully removed fields, excluding the ignored fields.

8, hlen

Description: returns the number of fields in the hash table key.
Parameter: key
Returned value: number of fields in the hash table. If the key does not exist, 0 is returned.

9, hexists

Description: checks whether the specified field exists in the key of the hash table.
Parameter: key field
Returned value: If the hash table contains a given field, 1 is returned. If the hash table does not contain a specified field or the key does not exist, 0 is returned.

10, hincrby

Description: adds an incremental increment to the field value of the hash table key. An increment can also be a negative number, which is equivalent to performing a subtraction operation on a given domain.
Parameter: key field increment
Returned value: the field value in the key of the hash table after the HINCRBY command is executed.

11, hkeys

Description: return all fields in the hash table key.
Parameter: key
Returned value: a table that contains all fields in the hash table. If the key does not exist, an empty table is returned.

12, hvals

Description: return all values in the key of the hash table.
Parameter: key
Returned value: a table that contains all values in the hash table. If the key does not exist, an empty table is returned.

Code example of the above 12 methods:

Copy codeThe Code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ 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'); // result: 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-> hmet ('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 socret value.
Although an ordered set is ordered, it is also a set and cannot repeat elements.
Update the score value of the original element.
Parameters:
Key
Score: double
Value: string
Return Value: 1 or 0

14, zrange

Description: gets the sorting element in a specific range. 0 indicates the first element, and 1 indicates the second. -1 indicates the last one, and-2 indicates the last two...
Parameters:
Key
Start: long
End: long
Withscores: bool = false
Returned value: Array

15, zdelete, zrem

Description: deletes a specified member from an ordered set.
Parameters:
Key
Member
Return Value: 1 or 0

16, zrevrange

Description: return all elements in the specified range in the sorted set corresponding to the key. These elements are arranged in the order from high to low. The elements with the same score are arranged alphabetically in descending order. This command is similar to ZRANGE, except that the order of elements in this command is different from that in ZRANGE.
Parameters:
Key
Start: long
End: long
Withscores: bool = false
Returned value: Array

17, zrangebyscore, zrevrangebyscore

Description: returns all the elements in the sorted set corresponding to the key that are between min and max (the package's score is equal to min or max ). Elements are arranged in ascending order of score. If the element has the same score, it will be arranged alphabetically.
The optional LIMIT option can be used to obtain matching elements within a certain range. If the offset value is large, the ordered set needs to be traversed before obtaining the elements to be returned, which increases the time complexity of O (N. The optional WITHSCORES option allows you to return the score of an element while returning the element. This option is available since Redis 2.0.
Parameters:
Key
Start: string
End: string
Options: array
Returned value: Array

18, zcount

Description: returns the number of elements in an ordered set of keys that intermediates between min and max.
Parameters:
Key
Start: string
End: string
Returned value: array Length

19, zremrangebyscore, zreleterangebyscore

Description: removes all the elements in the sorted set corresponding to the key that are located between min and max (including the endpoint. Starting from version 2.1.6, the interval endpoint min and max can be excluded, which is the same as the ZRANGEBYSCORE syntax.
Parameters:
Key
Start: double or "+ inf" or "-inf" string
End: double or "+ inf" or "-inf" string
Returned value: Number of deleted elements

20, zremrangebyrank, zdeleterangebyrank

Description: removes all elements whose rank values are between start and stop in the sorted set corresponding to the 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 indicates the element with the highest score,-2 indicates the element with the second highest score, and so on.
Parameters:
Key
Start: LONG
End: LONG
Returned value: Number of deleted elements

21, zsize, zcard

Description: returns the number of elements in the sorted set corresponding to the key.
Parameter: key
Returned value: number of elements

22, zscore

Description: returns the score value of the member in the sorted set corresponding to the key. If member does not exist in the sorted set, null is returned.
Parameter: key member

23, zrank, zrevrank

Description: return the index value of the member element in the sorted set corresponding to the key. The elements are sorted from low to high by score. The rank value (or index) starts from 0, which means that the rank value of the element with the lowest score value is 0. ZREVRANK can be used to obtain the rank (or index) of elements in the ascending order ).
Parameter: key member
Return Value: Number

24, zincrby

Add increment to the scroe of the member element in the sorted set corresponding to the key. If the specified member does not exist, this element is added and Its score initial value is increment. If the key does not exist, a new ordered list will be created, containing the unique element of 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 numeric values and receive double-precision floating point numbers. You can also provide a negative value to reduce the score value.
Parameter: key value member
Returned value: balanced data

25, zunion

Description: calculates the numkeys sorted sets corresponding to keys and stores the results in destination.
Parameter: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return Value: Union Array

26, zinter

Description: calculates the intersection of numkeys sorted sets corresponding to keys and stores the results in destination.
Parameter: keyOutput arrayZSetKeys arrayWeights aggregateFunction
Return Value: intersection Array


Example code of 13-26:
Copy codeThe Code is as follows:
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ 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); // 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, 'aaa ');
Echo $ redis-> zscore ('key', 'aaa'); // result: 2.5

Echo $ redis-> zrank ('key', 'aaa'); // result: 0
Echo $ redis-> zrevrank ('key', 'aaa'); // result: 1

$ Redis-> delete ('key ');

Echo $ redis-> zincrby ('key', 2, 'aaa'); // result: 2
Echo $ redis-> zincrby ('key', 1, 'aaa'); // 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)
?>

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.