Redis has a lot of operations, previously saw a more full blog, but now can not find. Find something to search for half a day, the following to organize the example of the PHP processing Redis, the personal feel commonly used some examples. The following examples are based on the Php-redis extension. 1,connect Description: The instance is connected to a redis. Parameters: Host:string,port:int return value: BOOL successful return: TRUE; failed return: false example: Copy code code as follows: <?php $redis = new Redis (); $result = $redis->connect (' 127.0.0.1 ', 6379); Var_dump ($result); Result: BOOL (TRUE)?> 2,set Description: Sets the value parameter for key and value: Key value return value: BOOL successful return: TRUE; failed return: false example: Copy code code as follows: <?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 parameter for the specified key: Key return value: String or bool returns FALSE if the key does not exist. Otherwise, returns the value for the specified key. Example: Copy code code as follows: <?php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $result = $redis->get (' Test '); Var_dump ($result); Result: String (one) "11111111111"?> 4,delete Description: Deletes the specified key parameter: A key, or an indeterminate number of arguments, for each key array: Key1 key2 Key3 ... keyn return value: Number of items deleted example: copying code The code is as follows: <?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 parameter argument: Key value return value: BOOL successful return: TRUE; failed return: false example: Copy code code as follows: <?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 has a parameter key return value: Bool successfully returns: TRUE; failed return: false example: Copy code code as follows: <?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. Parameter: Key value: The value to be added to the key return value: INT The new value instance: Copy code code as follows: <?php $redis = new R Edis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->set (' Test ', "123"); Var_dump ($redis->incR ("test")); Result: Int (124) Var_dump ($redis->incr ("test")); Result: Int (?>) 8,DECR Description: The numeric decrement stores the key value. Parameters: Key value: The value to be added to the key return value: INT The new value instance: the copy code code is as follows: <?php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->set (' Test ', "123"); Var_dump ($redis->decr ("test")); Result: Int (122) var_dump ($redis->decr ("test")); Result: Int (121)? >9,getmultiple Description: Gets the value of all specified keys. If one or more keys do not exist, the value of the key in the array is a false parameter: A list array that contains the key values returns a value: Returns an array instance containing the values of all keys: Copy code code as follows: <?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); Result: Array ([0] = 1 [1] = 2)?> 10,lpush Description: Adds a string value to the list header. 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: Successful return of array length, failed false instance: Copy code code as follows: <?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")); Result: Int (2)?> 11,rpush Description: Adds a string value to 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: Successful return array length, failure false Example: Copy code code as follows: <?php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->delete (' Test '); Var_dump ($redis->lpush ("Test", "111")); Result: Int (1) var_dump ($redis->lpush ("Test", "222")); Result: Int (2) var_dump ($redis->rpush ("Test", "333")); Result: Int (3) var_dump ($redis->rpush ("Test", "444")); Results: Int (4)?> 12,lpop Description: Returns and removes the first element parameter of the list: Key return value: The value of the first element is returned successfully, the failure returns false example: the copy code code is as follows: <?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"?> 13,lsize,llen Description: The length of the returned 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. Parameter: Key return value: Successful return array length, fail false example: Copy code code as follows: <?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")); Result: Int (4)?> 14,lget Description: Returns the specified key stored in the specified element in the list. 0 first element, 1 two ...-1 last element,-2 the penultimate ... The wrong index or key does not point to the list and returns false. Parameter: Key index return value: Successfully returns the value of the specified element, failure false Example: Copy code code as follows: <?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 by the list, and returns False if no such index exists. Parameter: Key index value return: Successful return true, fail false example: Copy code code as follows: <?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 Description: Returns the specified element in the specified key list in the range, starting at the end of the store, Lgetrange (key, start, end). 0 first element, 1 second element ...-1 last element,-2 second from the bottom ... Parameters: Key Start end return value: Successfully returns the value of the lookup, failure false example: Copy code code as follows: <?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)); Result: Array ([0] = 222 [1] = 111)?> 17,lremove Description: Removes count-matched values from the head starting from the list. If Count is zero, all matching elements are deleted. If count is negative, the content is deleted from the tail. Parameters: Key Count value return value: Successfully returns the number of deletions, failure false Example: Copy code code as follows: <?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)); Results: 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, failure false example: Copy code code as follows: <?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 ')); Result: Array ([0] = 111 [1] = 333)?> 19,sremove Description: Delete the value value parameter specified in key: Key member return value: TRUE or false example: Copy code code as follows: & lt;? 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: Move value in Key1 to Key2 parameter: Srckey Dstkey member return value: TRUE or false example copy code code as follows: < ;? 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 ')); Result: Array ([0] = 111 [1] = 222 [2] = = 444)?> 21,scontains Description: Checks whether the specified value exists in the collection. Parameter: Key value return value: TRUE or false example: Copy code code as follows: <?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 values stored in the collection parameter: Key return value: The number of successfully returned array, failure 0 Example: Copy code code as follows: <?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 parameter in key: Key return value: Successful return of deleted value, failed false example: Copy code code as follows: <?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 specified keys. If you specify only one key, the command generates a member of the collection. Returns False if a key does not exist. Parameters: Key1, Key2, Keyn return value: Successful return array intersection, failure false Example: Copy code code as follows: <?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 result in the newly created variable. Parameters: Key:dstkey, the Key to store the diff into. Keys:key1, Key2 ... keyN. Key1. KeyN is intersected as in SInter. Return value: Successful return, number of intersections, failure false Example: Copy code code as follows: <?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")); Result: Int (1) var_dump ($redis->smembers (' new ')); Result: Array (1) {[0]=> string (3) "111"}?> 26,sunion Description: Returns a union parameter for all specified keys: keys:key1, Key2, ..., Keyn return value: Successful return of merged set, failed FAL SE Example: Copy code code as follows: <?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] = 111 [1] = 222 [2] = 333 [3] = 444)?> 27,sunionstore Description: Executes the sunion command and stores the result in the newly created variable. Parameters: Key:dstkey, the Key to store the diff into. Keys:key1, Key2 ... keyN. Key1. KeyN is intersected as In SInter. Return value: Successful return, number of intersections, failure false Example: Copy code code as follows: <?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")); Result: Int (4) Print_r ($redis->smembers (' new ')); Results: Array ([0] = 111 [1] = 222 [2] = 333 [3] = 444)?> 28,sdiff Description: Returns a result parameter that exists in the first collection and does not exist in all other collections: Keys: Key1, Key2, ..., keyn:any number of keys corresponding to sets in Redis. Return value: Returns the array successfully, false example: Copy code code as follows: <?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")); Result: Array ([0] = 222 [1] = 333)?> 29,sdiffstore Description: Executes the Sdiff command and stores the result in the newly created 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: Successful return digit, failure false example: Copy code code as follows: <?php $red is = 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")); Result: Int (2) print_r ($redis->smembers (' new ')); Results: Array ([0] = 222 [1] = 333)?> 30,smembers, sgetmembers Description: Returns the contents of the collection parameter: Key:key return value: An Array of elements, t He contents of the set. Example: The copy code code is as follows: <?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 ')); Result: Array ([0] = 111 [1] = 222)?>
30 PHP Operations Redis Common Method code example