PHP Redis Learning Notes

Source: Internet
Author: User
Tags array length diff numeric min numeric value php redis redis

1,connect
Description: The instance is connected to a redis.
Parameters: Host:string,port:int
Return value: BOOL successfully returned: TRUE; failed return: FALSE

<?php
$redis = new Redis ();
$result = $redis-&gt;connect (' 127.0.0.1 ', 6379);
Var_dump ($result); Result: BOOL (TRUE)
?>
2,set
Description: Sets the value of key and value
Parameter: Key Value
Return value: BOOL successfully returned: TRUE; failed return: FALSE
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$result = $redis->set (' Test ', ' 11111111111 ');
Var_dump ($result); Result: BOOL (TRUE)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$result = $redis->set (' Test ', ' 11111111111 ');
Var_dump ($result); Result: BOOL (TRUE)
?>
3,get

Description: Gets the value for the specified key
Parameters: Key
Return value: String or bool returns FALSE if the key does not exist. Otherwise, the value that corresponds to the specified key is returned.

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$result = $redis->get (' Test ');
Var_dump ($result); Result: String (11) "11111111111"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$result = $redis->get (' Test ');
Var_dump ($result); Result: String (11) "11111111111"
?>

4,delete

Description: Deletes the specified key
Parameters: A key, or an indeterminate number of arguments, each of the key arrays: Key1 key2 Key3 ... Keyn
Return value: Number of items deleted
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
echo $redis->get (' Test '); Results: 1111111111111
$redis->delete (' Test ');
Var_dump ($redis->get (' Test ')); Result: bool (FALSE)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
echo $redis->get (' Test '); Results: 1111111111111
$redis->delete (' Test ');
Var_dump ($redis->get (' Test ')); Result: bool (FALSE)
?>
5,setnx

Description: If the key is not present in the database, set the key value parameter
Parameter: Key value
Return value: BOOL successfully returned: TRUE; failed return: FALSE

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
$redis->setnx (' Test ', ' 22222222 ');
echo $redis->get (' Test '); Results: 1111111111111
$redis->delete (' Test ');
$redis->setnx (' Test ', ' 22222222 ');
echo $redis->get (' Test '); Results: 22222222
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
$redis->setnx (' Test ', ' 22222222 ');
echo $redis->get (' Test '); Results: 1111111111111
$redis->delete (' Test ');
$redis->setnx (' Test ', ' 22222222 ');
echo $redis->get (' Test '); Results: 22222222
?>
6,exists

Description: Verifies that the specified key exists
Parameter key
Return value: Bool successfully returned: TRUE; failed return: FALSE

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
Var_dump ($redis->exists (' Test ')); Result: BOOL (TRUE)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 1111111111111 ');
Var_dump ($redis->exists (' Test ')); Result: BOOL (TRUE)
?>
7,incr

Description: Numeric increment stores key value keys.
Parameters: Key value: Values to be added to the key
Return value: INT the new value
Instance:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 123 ');
Var_dump ($redis->incr ("test")); Results: Int (124)
Var_dump ($redis->incr ("test")); Results: Int (125)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 123 ');
Var_dump ($redis->incr ("test")); Results: Int (124)
Var_dump ($redis->incr ("test")); Results: Int (125)
?>
8,decr

Description: Numeric decrement stores key values.
Parameters: Key value: Values to be added to the key
Return value: INT the new value
Instance:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 123 ');
Var_dump ($redis->decr ("test")); Results: Int (122)
Var_dump ($redis->decr ("test")); Results: Int (121)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' 123 ');
Var_dump ($redis->decr ("test")); Results: Int (122)
Var_dump ($redis->decr ("test")); Results: Int (121)
?>
9,getmultiple

Description: Gets the value of all the specified keys. If one or more keys do not exist, the value of the key in the array is false
Parameter: An array of lists containing the key values
Return value: Returns an array of values containing all keys
Instance:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' test1 ', "1");
$redis->set (' Test2 ', "2");
$result = $redis->getmultiple (Array (' test1 ', ' test2 '));
Print_r ($result); Results: Array ([0] => 1 [1] => 2)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' test1 ', "1");
$redis->set (' Test2 ', "2");
$result = $redis->getmultiple (Array (' test1 ', ' test2 '));
Print_r ($result); Results: Array ([0] => 1 [1] => 2)
?>
10,lpush

Description: Adds a string value from the header of the list. If the key does not exist, the list is created. Returns False if the key exists and is not a list.
Parameters: Key,value
Return value: The array length was successfully returned and failed false

Instance:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->lpush ("Test", "111")); Results: Int (1)
Var_dump ($redis->lpush ("Test", "222")); Results: Int (2)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->lpush ("Test", "111")); Results: Int (1)
Var_dump ($redis->lpush ("Test", "222")); Results: Int (2)
?>
11,rpush

Description: Adds a string value from the tail of the list. If the key does not exist, the list is created. Returns False if the key exists and is not a list.
Parameters: Key,value
Return value: The array length was successfully returned and failed false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->lpush ("Test", "111")); Results: Int (1)
Var_dump ($redis->lpush ("Test", "222")); Results: Int (2)
Var_dump ($redis->rpush ("Test", "333")); Results: Int (3)
Var_dump ($redis->rpush ("Test", "444")); Results: Int (4)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->lpush ("Test", "111")); Results: Int (1)
Var_dump ($redis->lpush ("Test", "222")); Results: Int (2)
Var_dump ($redis->rpush ("Test", "333")); Results: Int (3)
Var_dump ($redis->rpush ("Test", "444")); Results: Int (4)
?>
12,lpop

Description: Returns and removes the first element of a list
Parameters: Key
Return value: Returns the value of the first element successfully, failure returns false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lpop ("test")); Result: String (3) "222"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lpop ("test")); Result: String (3) "222"
?>
12,rpop

Description: Returns and removes the last element of a list
Parameters: Key
Return value: Returns the value of the last element successfully, failure returns false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->rpop ("test")); Result: String (3) "444"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->rpop ("test")); Result: String (3) "444"
?>
13,lsize,llen

Description: Returns the length of the list. If the list does not exist or is empty, the command returns 0. If the key is not a list, the command returns FALSE.
Parameters: Key
Return value: The array length was successfully returned and failed false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lsize ("test")); Results: Int (4)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lsize ("test")); Results: Int (4)
?>
14,lget

Description: Returns the element specified by the specified key stored in the list. 0 The first element, 1 of the two ...-1 last element,-the penultimate of 2 ... False if the wrong index or key does not point to the list.
Parameters: Key Index
Return value: Successfully returns the value of the specified element, false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lget ("Test", 3)); Result: String (3) "444"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
$redis->rpush ("Test", "333");
$redis->rpush ("Test", "444");
Var_dump ($redis->lget ("Test", 3)); Result: String (3) "444"
?>
15,lset

Description: Assigns a new value to the index specified for the list, and returns False if no such index exists.
Parameter: Key index value
Return value: Successful return true, failed false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
Var_dump ($redis->lget ("test", 1)); Result: String (3) "111"
Var_dump ($redis->lset ("Test", 1, "333")); Result: BOOL (TRUE)
Var_dump ($redis->lget ("test", 1)); Result: String (3) "333"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
Var_dump ($redis->lget ("test", 1)); Result: String (3) "111"
Var_dump ($redis->lset ("Test", 1, "333")); Result: BOOL (TRUE)
Var_dump ($redis->lget ("test", 1)); Result: String (3) "333"
?>
16,lgetrange

Describe:
Returns the specified element, Lgetrange (key, start, end), that begins in the list of specified keys in the zone. 0 first element, 1 second element ...-1 The last element, the second of 2 ...
Parameters: Key Start end
Return value: The value of the lookup was successfully returned and failed false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
Print_r ($redis->lgetrange ("test", 0,-1)); Results: Array ([0] => 222 [1] => 111)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush ("Test", "111");
$redis->lpush ("Test", "222");
Print_r ($redis->lgetrange ("test", 0,-1)); Results: Array ([0] => 222 [1] => 111)
?>
17,lremove

Description: Removes the count-matching value from the head starting from the list. If Count is zero, all matching elements are deleted. If count is a negative number, the content is deleted from the tail.
Parameter: Key count value
Return value: Successfully returns the number of deletes, failed false
Example:

<?php  
$redis = new Redis ();  
$redis->connect (' 127.0.0.1 ', 6379);  
$redis->delete (' test ');  
$redis->lpush (' Test ', ' a ');  
$redis->lpush (' Test ', ' B ');  
$redis->lpush (' Test ', ' C ');  
$redis->rpush (' Test ', ' a ');   
Print_r ($redis->lgetrange (' test ', 0,-1);//Result: Array ([0] => C [1] => b [2] => a [3] => a) &n bsp; 
Var_dump ($redis->lremove (' Test ', ' a ', 2));  //Result: Int (2)   
Print_r ($ Redis->lgetrange (' Test ', 0,-1)); Result: Array ([0] => C [1] => b)   
? > 

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->lpush (' Test ', ' a ');
$redis->lpush (' Test ', ' B ');
$redis->lpush (' Test ', ' C ');
$redis->rpush (' Test ', ' a ');
Print_r ($redis->lgetrange (' test ', 0,-1)); Result: Array ([0] => C [1] => b [2] => a [3] => a)
Var_dump ($redis->lremove (' Test ', ' a ', 2)); Results: Int (2)
Print_r ($redis->lgetrange (' test ', 0,-1)); Result: Array ([0] => C [1] => B)
?>
18,sadd

Description: Adds a value to a key. If this value is already in this key, it returns false.
Parameter: Key value
Return value: Successful return true, failed false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->sadd (' Test ', ' 111 ')); Result: BOOL (TRUE)
Var_dump ($redis->sadd (' Test ', ' 333 ')); Result: BOOL (TRUE)
Print_r ($redis->sort (' Test ')); Results: Array ([0] => [1] => 333)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
Var_dump ($redis->sadd (' Test ', ' 111 ')); Result: BOOL (TRUE)
Var_dump ($redis->sadd (' Test ', ' 333 ')); Result: BOOL (TRUE)
Print_r ($redis->sort (' Test ')); Results: Array ([0] => [1] => 333)
?>
19,sremove

Description: Deletes the value specified in key
Parameters: Key Member
Return value: TRUE or False
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 333 ');
$redis->sremove (' Test ', ' 111 ');
Print_r ($redis->sort (' Test ')); Result: Array ([0] => 333)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 333 ');
$redis->sremove (' Test ', ' 111 ');
Print_r ($redis->sort (' Test ')); Result: Array ([0] => 333)
?>
20,smove

Description: Moves value in Key1 to Key2
Parameters: Srckey Dstkey Member
Return value: TRUE or False
Example

<?php  
$redis = new Redis ();  
$redis->connect (' 127.0.0.1 ', 6379);  
$redis->delete (' test ');  
$redis->delete (' test1 ');  
$redis->sadd (' Test ', ' a ');  
$redis->sadd (' Test ', ' 333 ');  
$redis->sadd (' test1 ', ' 222 ');   
$redis->sadd (' test1 ', ' 444 ');  
$redis->smove (' Test ', ' test1 ', ' ') ';   
Print_r ($redis->sort (' test1 '));   //Result: Array ([0] => [1] => 222 [2] => 444) & nbsp; 
? > 

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->delete (' test1 ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 333 ');
$redis->sadd (' test1 ', ' 222 ');
$redis->sadd (' test1 ', ' 444 ');
$redis->smove (' Test ', ' test1 ', ' 111 ');
Print_r ($redis->sort (' test1 ')); Results: Array ([0] => [1] => 222 [2] => 444)
?>
21,scontains

Description: Checks to see if the specified value exists in the collection.
Parameter: Key value
Return value: TRUE or False
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 112 ');
$redis->sadd (' Test ', ' 113 ');
Var_dump ($redis->scontains (' Test ', ' 111 ')); Result: BOOL (TRUE)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 112 ');
$redis->sadd (' Test ', ' 113 ');
Var_dump ($redis->scontains (' Test ', ' 111 ')); Result: BOOL (TRUE)
?>
22,ssize

Description: Returns the number of stored values in the collection
Parameters: Key
Return value: Number of successfully returned array, failed 0
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 112 ');
echo $redis->ssize (' Test '); Results: 2
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd (' Test ', ' 111 ');
$redis->sadd (' Test ', ' 112 ');
echo $redis->ssize (' Test '); Results: 2
?>
23,spop

Description: Randomly remove and return a value in the key
Parameters: Key
Return value: Failed to return the deleted value successfully false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
Var_dump ($redis->spop ("test")); Result: String (3) "333"
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
Var_dump ($redis->spop ("test")); Result: String (3) "333"
?>
24,sinter

Description: Returns the intersection of all the specified keys. If you specify only one key, the command generates members of this collection. Returns False if a key is not present.
Parameters: Key1, Key2, Keyn
Return value: Successfully return array intersection, fail false

Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sinter ("Test", "test1")); Result: Array (1) {[0]=> string (3) "111"}
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sinter ("Test", "test1")); Result: Array (1) {[0]=> string (3) "111"}
?>
25,sinterstore

Description: Executes the sinter command and stores the results in a new variable.
Parameters:
Key:dstkey, the key to store the diff into.
Keys:key1, Key2 ... keyn. Key1.. Keyn are intersected as in sinter.
Return value: Successful return, number of intersections, failure false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sinterstore (' new ', "Test", "test1")); Results: Int (1)
Var_dump ($redis->smembers (' new ')); Result: Array (1) {[0]=> string (3) "111"}
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sinterstore (' new ', "Test", "test1")); Results: Int (1)
Var_dump ($redis->smembers (' new ')); Result: Array (1) {[0]=> string (3) "111"}
?>
26,sunion

Describe:
Returns a set of all the specified keys
Parameters:
Keys:key1, Key2, ..., Keyn
Return value: Successful return of the merged set failed false
Example:

<?php  
$redis = new Redis ();  
$redis->connect (' 127.0.0.1 ', 6379);  
$redis->delete (' test ');  
$redis->sadd ("Test", "a");  
$redis->sadd (" Test "," 222 ");  
$redis->sadd (" Test "," 333 ");  
$redis->sadd (" Test1 "," 111 ");   
$redis->sadd ("Test1", "444");  
Print_r ($redis->sunion ("Test", "test1"));  /Result: Array ([0] => [1] => 222 [2] => 333 [3] => 444)   
? > 

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Print_r ($redis->sunion ("Test", "test1")); Results: Array ([0] => [1] => 222 [2] => 333 [3] => 444)
?>
27,sunionstore

Description: Executes the sunion command and stores the results in a new variable.
Parameters:
Key:dstkey, the key to store the diff into.
Keys:key1, Key2 ... keyn. Key1.. Keyn are intersected as in sinter.
Return value: Successful return, number of intersections, failure false
Example:

<?php  
$redis = new Redis ();  
$redis->connect (' 127.0.0.1 ', 6379);  
$redis->delete (' test ');  
$redis->sadd ("Test", "a");  
$redis->sadd (" Test "," 222 ");  
$redis->sadd (" Test "," 333 ");  
$redis->sadd (" Test1 "," 111 ");   
$redis->sadd ("Test1", "444");  
Var_dump ($redis->sunionstore (' new ', ' Test ', " Test1 ")); //Result: Int (4)   
Print_r ($redis->smembers (' new ')); //Result: Array ([0] => 111 [1] = > 222 [2] => 333 [3] => 444)   
? > 

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sunionstore (' new ', "Test", "test1")); Results: Int (4)
Print_r ($redis->smembers (' new ')); Results: Array ([0] => [1] => 222 [2] => 333 [3] => 444)
?>
28,sdiff

Description: Returns a result that exists in the first collection and does not exist in all other collections
Parameters: Keys:key1, Key2, ..., keyn:any number of Keys corresponding to sets in Redis.
Return value: Array returned successfully, failed false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Print_r ($redis->sdiff ("Test", "test1")); Results: Array ([0] => 222 [1] => 333)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Print_r ($redis->sdiff ("Test", "test1")); Results: Array ([0] => 222 [1] => 333)
?>
29,sdiffstore

Description: Executes the Sdiff command and stores the results in a new variable.
Parameters:
Key:dstkey, the key to store the diff into.
Keys:key1, Key2, ..., keyn:any number of keys corresponding to sets in Redis
Return value: Successfully returning a number, failing false
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sdiffstore (' new ', "Test", "test1")); Results: Int (2)
Print_r ($redis->smembers (' new ')); Results: Array ([0] => 222 [1] => 333)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
$redis->sadd ("Test", "333");
$redis->sadd ("Test1", "111");
$redis->sadd ("Test1", "444");
Var_dump ($redis->sdiffstore (' new ', "Test", "test1")); Results: Int (2)
Print_r ($redis->smembers (' new ')); Results: Array ([0] => 222 [1] => 333)
?>
30,smembers, Sgetmembers

Describe:
Returns the contents of a collection
Parameters: Key:key
Return value: An array of elements, the contents of the set.
Example:

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
Print_r ($redis->smembers (' Test ')); Results: Array ([0] => [1] => 222)
?>

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->delete (' Test ');
$redis->sadd ("Test", "111");
$redis->sadd ("Test", "222");
Print_r ($redis->smembers (' Test ')); Results: Array ([0] => [1] => 222)
?>
Php-redis, there are a lot of different names, but functions like: Lrem and Lremove, here's no example.


The previous blog is mainly string type, list type and set type, below hash type and Zset type
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.
Instance 1
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.
Instance 1
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.
Instance 1

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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1
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.
Instance 1

<?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
Instance 2
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
Instance 2
15,zdelete, Zrem
Description: Deletes the specified member from the ordered collection.
Parameters:
Key
Member
return value: 1 or 0
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2
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
Instance 2

$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)
?>

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.