Code example of 30 common php methods for redis operations

Source: Internet
Author: User
This article mainly introduces 30 common php method code examples for redis operations. In this article, there are actually more than 30 methods that can be used to operate data of the string, list, and set types, for more information, see

This article mainly introduces 30 common php method code examples for redis operations. In this article, there are actually more than 30 methods that can be used to operate data of the string, list, and set types, for more information, see

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

Example:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:


<? Php
$ 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:

The Code is as follows:

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.