This article mainly introduces php methods and code examples for operating hash and zset data in redis. This article describes 26 groups of functions or methods. For more information, see
This article mainly introduces php methods and code examples for operating hash and zset data in redis. This article describes 26 groups of functions or methods. For more information, see
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:
The 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"
";
$ Redis-> hSetNx ('test', 'key1', 'World ');
Echo $ redis-> hget ('test', 'key1'); // result: hello
$ Redis-> delete ('test ');
$ Redis-> hSetNx ('test', 'key1', 'World ');
Echo"
";
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