Code example of 30 common php methods for redis operations, phpredis

Source: Internet
Author: User
Tags php code examples

30 common php code examples for redis operations (reproduced), phpredis

1. connect
Description: The instance is connected to a Redis instance.
Parameter: host: string, port: int
Return Value: BOOL success return: TRUE; Failure return: FALSE
Example:

$ Redis = new redis ();
$ Result = $ redis-> connect ('2017. 0.0.1 ', 127 );
Var_dump ($ result); // result: bool (true)
?>
2. set
Description: sets the key and value values.
Parameter: Key Value
Return Value: BOOL success return: TRUE; Failure return: FALSE
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Result = $ redis-> set ('test', "11111111111 ");
Var_dump ($ result); // result: bool (true)
?>
3. get
Description: gets the value of a specified key.
Parameter: key
Return Value: string or BOOL. If the key does not exist, FALSE is returned. Otherwise, the value corresponding to the specified key is returned.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Result = $ redis-> get ('test ');
Var_dump ($ result); // The result is string (11) "11111111111"
?>
4. delete
Description: deletes a specified key.
Parameter: a key, or an uncertain number of parameters, each key array: key1 key2 key3... KeyN
Return Value: Number of deleted items
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
Echo $ redis-> get ('test'); // result: 1111111111111
$ Redis-> delete ('test ');
Var_dump ($ redis-> get ('test'); // result: bool (false)
?>
5. setnx
Description: if the key does not exist in the Database, set the key value parameter.
Parameter: key value
Return Value: BOOL success return: TRUE; Failure return: FALSE
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
$ Redis-> setnx ('test', "22222222 ");
Echo $ redis-> get ('test'); // result: 1111111111111
$ Redis-> delete ('test ');
$ Redis-> setnx ('test', "22222222 ");
Echo $ redis-> get ('test'); // result: 22222222
?>
6. exists
Description: checks whether the specified key exists.
Parameter key
Return Value: Bool success return: TRUE; Failure return: FALSE
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
Var_dump ($ redis-> exists ('test'); // result: bool (true)
?>
7. incr
Description: storage key-value for incremental data.
Parameter: key value: value to be added to the key
Return value: INT the new value
Instance:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "123 ");
Var_dump ($ redis-> incr ("test"); // result: int (124)
Var_dump ($ redis-> incr ("test"); // result: int (125)
?>

8. decr
Description: The storage key value of the decimal number.
Parameter: key value: value to be added to the key
Return value: INT the new value
Instance:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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 values of all specified keys. If one or more keys do not exist, the value of the key in the array is false.
Parameter: List array containing key values
Return Value: returns an array containing values of all keys.
Instance:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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 this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned value: the array length is returned successfully. If the returned value is false
Instance:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
Var_dump ($ redis-> lpush ("test", "111"); // result: int (1)
Var_dump ($ redis-> lpush ("test", "222"); // result: int (2)
?>

11. rpush
Description: adds a string value at the end of the list. If this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned value: the array length is returned successfully. If the returned value is false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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"); // result: int (4)
?>

12. lpop
Description: returns and removes the first element of the list.
Parameter: key
Return Value: the value of the first element is returned. If the return value fails, false is returned.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lpop ("test"); // The result is: string (3) "222"
?>

13. lsize, llen
Description: The length of the returned list. If the list does not exist or is empty, this command returns 0. If the key is not a list, the command returns FALSE.
Parameter: Key
Returned value: the array length is returned successfully. If the returned value is false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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: return the elements specified by the specified key stored in the list. 0: the first element, 1: the second... -1: The last element,-2's penultimate... If the wrong index or key does not point to the list, FALSE is returned.
Parameter: key index
Returned value: the value of the specified element is returned successfully. If the returned value is false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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); // The result is: string (3) "444"
?>

15. lset
Description: adds a value to the index specified in the list. If the index does not exist, false is returned.
Parameter: key index value
Return Value: true if the call is successful, false if the call fails.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
Var_dump ($ redis-> lget ("test", 1); // The result is: string (3) "111"
Var_dump ($ redis-> lset ("test", 1, "333"); // result: bool (true)
Var_dump ($ redis-> lget ("test", 1); // The result is: string (3) "333"
?>

16. lgetrange
Description:
Returns the specified Element lGetRange (key, start, end) from start to end in the specified key list of the region ). 0: the first element, 1: The second element... -1: The last element,-2's penultimate...
Parameter: key start end
Return Value: The Search value is returned successfully. If the query fails, false is returned.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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 header of the list. If count is zero, all matched elements are deleted. If count is a negative number, the content is deleted from the end.
Parameter: key count value
Return Value: the number of deleted items returned. If the deletion fails, the return value is false.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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] =>)
Var_dump ($ redis-> lremove ('test', 'A', 2); // result: 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 the value is already in this Key, FALSE is returned.
Parameter: key value
Return Value: true if the call is successful, false if the call fails.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
Var_dump ($ redis-> sadd ('test', '2013'); // result: bool (true)
Var_dump ($ redis-> sadd ('test', '2013'); // result: bool (true)
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 111 [1] => 333)
?>

19. sremove
Description: deletes the specified value in the Key.
Parameter: key member
Return Value: true or false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sremove ('test', '123 ');
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 333)
?>

20. smove
Description: Move the value in Key1 to Key2.
Parameter: srcKey dstKey member
Return Value: true or false
Example

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> delete ('test1 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> smove ('test', "test1", '123 ');
Print_r ($ redis-> sort ('test1'); // result: Array ([0] => 111 [1] => 222 [2] => 444)
?>

21. scontains
Description: checks whether a specified value exists in the set.
Parameter: key value
Return Value: true or false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Var_dump ($ redis-> scontains ('test', '2013'); // result: bool (true)
?>

22. ssize
Description: Number of stored values in the collection.
Parameter: key
Returned value: the number of returned arrays. If the return value is successful, the return value is 0.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Echo $ redis-> ssize ('test'); // result: 2
?>

23. spop
Description: Random removal and return a value in the key.
Parameter: key
Return Value: The deleted Value is returned. If the deletion fails, false is returned.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
Var_dump ($ redis-> spop ("test"); // The result is: string (3) "333"
?>

24. sinter
Description: returns the intersection of all specified keys. If only one key is specified, this command generates the members of this set. If a key does not exist, FALSE is returned.
Parameter: key1, key2, keyN
Returned value: array intersection is returned successfully. If the return value is false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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: run the sInter command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return Value: Success, number of intersections, failure, false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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 the union of all specified keys.
Parameters:
Keys: key1, key2 ,... , KeyN
Returned value: the merged set is returned successfully. If the merging fails, false is returned.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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"); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
?>

27. sunionstore
Description: Execute the sunion command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return Value: Success, number of intersections, failure, false
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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'); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
?>

28. sdiff
Description: return the result that exists in the first set and does not exist in all other sets.
Parameter: Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis.
Returned value: the array is returned successfully, and the returned value is false.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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: Execute the sdiff command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis
Return Value: a number is returned for success, and false is returned for failure.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ 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'); // result: Array ([0] => 222 [1] => 333)
?>

30. smembers, sgetmembers
Description:
Returns the content of the set.
Parameter: Key: key
Returned value: An array of elements, the contents of the set.
Example:

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
Print_r ($ redis-> smembers ('test'); // result: Array ([0] => 111 [1] => 222)
?>

 

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.