Redisphp Example 1

Source: Internet
Author: User
Redisphp instance redis has a lot of operations. I saw a full blog before, but I cannot find it now. Search for things for half a day. Below are some examples of php processing redis, which I think are commonly used. The following example is based on the php-redis extension .? 1, connect? Description: the instance is connected to a Redis instance. parameter: host: redis php instance 1

Redis has many operations. I used to see a full blog, but I cannot find it now. Search for things for half a day. Below are some examples of php processing redis, which I think are commonly used. The following example is based on the php-redis extension.

?

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

?

?

 Connect ('1970. 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

?

?

    
 Connect ('2017. 0.0.1 ', 6379); $ 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.

?

?

    
 Connect ('2017. 0.0.1 ', 6379); $ result = $ redis-> get ('test'); var_dump ($ result); // result: 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

?

?

    
 Connect ('2017. 0.0.1 ', 6379); $ 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

?

?

 Connect ('2017. 0.0.1 ', 6379); $ 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

?

?

    
 Connect ('2017. 0.0.1 ', 6379); $ 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

?

 Connect ('2017. 0.0.1 ', 6379); $ 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

?

 Connect ('2017. 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 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.

?

 Connect ('2017. 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 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

?

 Connect ('2017. 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)?>

?

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

?

 Connect ('2017. 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"); // 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.

?

 Connect ('2017. 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. will rpop be removed after the return is successful or will it be deleted in any way?

?

Description: returns and removes the last element of the list.
Parameter: key
Return value: returns the value of the last element. if the return value fails, false is returned.

?

 Connect ('2017. 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: 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

?

 Connect ('2017. 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: 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

 Connect ('2017. 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: 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.

?

?

 Connect ('2017. 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 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.

?

?

    
 Connect ('2017. 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 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.

?

?

 Connect ('2017. 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); // 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.

?

?

 Connect ('2017. 0.0.1 ', 6379); $ redis-> delete ('test'); var_dump ($ redis-> sadd ('test', '123'); // result: bool (true) var_dump ($ redis-> sadd ('test', '000000'); // 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

?

?

 Connect ('2017. 0.0.1 ', 6379); $ redis-> delete ('test'); $ redis-> sadd ('test', '123 '); $ redis-> sadd ('test', '123456'); $ redis-> sremove ('test', '123456 '); 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

?

?

 Connect ('2017. 0.0.1 ', 6379); $ redis-> delete ('test'); $ redis-> delete ('test1'); $ redis-> sadd ('test ', '123'); $ redis-> sadd ('test', '123'); $ redis-> sadd ('test1', '123 '); $ redis-> sadd ('test1', '20140901'); $ redis-> smove ('test', "test1", '20140901 '); 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

?

?

 Connect ('2017. 0.0.1 ', 6379); $ redis-> delete ('test'); $ redis-> sadd ('test', '123 '); $ redis-> sadd ('test', '123456'); $ redis-> sadd ('test', '123456 '); var_dump ($ redis-> scontains ('test', '000000'); // Result: bool (true)?>

?

?

?

 Connect ('2017. 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)?>

?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.

?

?

 Connect ('2017. 0.0.1 ', 6379); $ redis-> delete ('test'); $ redis-> sadd ('test', '123 '); $ redis-> sadd ('test', '000000'); 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.

?

?

 Connect ('2017. 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 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

?

?

 Connect ('2017. 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: 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

?

 Connect ('2017. 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 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.

?

?

 Connect ('2017. 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 ")); // 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

?

 Connect ('2017. 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'); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)?>

?

?

?

28, sdiff (difference set)

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.

?

?

     
 Connect ('2017. 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: 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.

?

    
 Connect ('2017. 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'); // 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:

 Connect ('2017. 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)?>

?

Original article: http://blog.51yip.com/cache/1439.html

?

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.