


30 code examples of commonly used methods for operating redis in PHP
This article mainly introduces 30 code examples of common methods for operating redis in PHP. This article actually has more than 30 methods, which can operate string type, list type and set type data. Friends in need can refer to it
There are many redis operations. I saw a relatively comprehensive blog before, but I can’t find it now. After searching for a long time, I will summarize some examples of PHP processing redis. I personally think some examples are commonly used. The following examples are all based on the php-redis extension.
1, connect
Description: The instance is connected to a Redis.
Parameters: host: string, port: int
Return value: BOOL Successfully returned : TRUE; Failure returns: FALSE
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); var_dump($result); //结果:bool(true) ?>
2,set
Description: Set the value of key and value
Parameter: Key Value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $result = $redis->set('test',"11111111111"); var_dump($result); //结果:bool(true) ?>
3,get
Description: Get the value of the specified key
parameters :key
Return value: string or BOOL If the key does not exist, return FALSE. Otherwise, return the value corresponding to the specified key.
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $result = $redis->get('test'); var_dump($result); //结果:string(11) "11111111111" ?>
4,delete
Description: Delete the specified key
Parameters: a key, or an undetermined number of parameters, an array for each key: key1 key2 key3 ... keyN
Return value: the number of items deleted
Example:
Copy 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'); //结果:1111111111111 $redis->delete('test'); var_dump($redis->get('test')); //结果:bool(false) ?>
5,setnx
Description: If in the database The key does not exist in, set the key value parameter
Parameter: key value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
Copy code The code is 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'); //结果:1111111111111 $redis->delete('test'); $redis->setnx('test',"22222222"); echo $redis->get('test'); //结果:22222222 ?>
6, exists
Description: Verify whether the specified key exists
Parameter key
Return Value: Bool Successful return: TRUE; Failure return: FALSE
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"1111111111111"); var_dump($redis->exists('test')); //结果:bool(true) ?>
7, incr
Description: Store key value key in numerical increment.
Parameters: key value: The value that will be added to the key
Return value: INT the new value
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"123"); var_dump($redis->incr("test")); //结果:int(124) var_dump($redis->incr("test")); //结果:int(125) ?>
8, decr
Description : Store key value in numerical decrement.
Parameters: key value: the value that will be added to the key
Return value: INT the new value
Example:
Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"123"); var_dump($redis->decr("test")); //结果:int(122) var_dump($redis->decr("test")); //结果:int(121) ?>
9, getMultiple
Description: Get the values of all specified keys. If one or more keys do not exist, the value of that key in the array is false
Parameters: Array of lists containing the values of the keys
Return value: Returns an array containing the values of all keys
Example:
Copy code The code is 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); //结果:Array ( [0] => 1 [1] => 2 ) ?>
10, lpush
Description: By Add a string value to the head of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Returns the array length if successful, false if failed
Example:
##Copy code The code is as follows:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); var_dump($redis->lpush("test","111")); //结果:int(1) var_dump($redis->lpush("test","222")); //结果:int(2) ?>
11, rpush
Description: Add a string value from the end of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.Parameters: key, value
Return value: Returns the array length if successful, false if failed
Example:
The code is as follows:<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->lpush("test","111")); //结果:int(1)
var_dump($redis->lpush("test","222")); //结果:int(2)
var_dump($redis->rpush("test","333")); //结果:int(3)
var_dump($redis->rpush("test","444")); //结果:int(4)
?>
Description: Return and remove the first element of the list
Parameter: keyReturn value: Return successfully The value of the first element, returns false on failure
Example:
The 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")); //结果:string(3) "222"
?>
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, this command returns FALSE.
Parameter: KeyReturn value: Return the array length if successful, false if failed
Example:
##Copy code
<?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")); //结果:int(4) ?>
14,lget
描述:返回指定键存储在列表中指定的元素。 0第一个元素,1第二个… -1最后一个元素,-2的倒数第二…错误的索引或键不指向列表则返回FALSE。
参数:key index
返回值:成功返回指定元素的值,失败false
范例:
复制代码 代码如下:
<?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)); //结果:string(3) "444" ?>
15,lset
描述:为列表指定的索引赋新的值,若不存在该索引返回false.
参数:key index value
返回值:成功返回true,失败false
范例:
复制代码 代码如下:
<?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)); //结果:string(3) "111" var_dump($redis->lset("test",1,"333")); //结果:bool(true) var_dump($redis->lget("test",1)); //结果:string(3) "333" ?>
16,lgetrange
描述:
返回在该区域中的指定键列表中开始到结束存储的指定元素,lGetRange(key, start, end)。0第一个元素,1第二个元素… -1最后一个元素,-2的倒数第二…
参数:key start end
返回值:成功返回查找的值,失败false
范例:
复制代码 代码如下:
<?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)); //结果:Array ( [0] => 222 [1] => 111 ) ?>
17,lremove
描述:从列表中从头部开始移除count个匹配的值。如果count为零,所有匹配的元素都被删除。如果count是负数,内容从尾部开始删除。
参数:key count value
返回值:成功返回删除的个数,失败false
范例:
复制代码 代码如下:
<?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)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a ) var_dump($redis->lremove('test','a',2)); //结果:int(2) print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b ) ?>
18,sadd
描述:为一个Key添加一个值。如果这个值已经在这个Key中,则返回FALSE。
参数:key value
返回值:成功返回true,失败false
范例:
复制代码 代码如下:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); var_dump($redis->sadd('test','111')); //结果:bool(true) var_dump($redis->sadd('test','333')); //结果:bool(true) print_r($redis->sort('test')); //结果:Array ( [0] => 111 [1] => 333 ) ?>
19,sremove
描述:删除Key中指定的value值
参数:key member
返回值:true or false
范例:
复制代码 代码如下:
<?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')); //结果:Array ( [0] => 333 ) ?>
20,smove
描述:将Key1中的value移动到Key2中
参数:srcKey dstKey member
返回值:true or false
范例
复制代码 代码如下:
<?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')); //结果:Array ( [0] => 111 [1] => 222 [2] => 444 ) ?>
21,scontains
描述:检查集合中是否存在指定的值。
参数:key value
返回值:true or false
范例:
复制代码 代码如下:
<?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')); //结果:bool(true) ?>
22,ssize
描述:返回集合中存储值的数量
参数:key
返回值:成功返回数组个数,失败0
范例:
复制代码 代码如下:
<?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'); //结果:2 ?>
23,spop
描述:随机移除并返回key中的一个值
参数:key
返回值:成功返回删除的值,失败false
范例:
复制代码 代码如下:
<?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")); //结果:string(3) "333" ?>
24,sinter
描述:返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。
参数:key1, key2, keyN
返回值:成功返回数组交集,失败false
范例:
复制代码 代码如下:
<?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")); //结果:array(1) { [0]=> string(3) "111" } ?>
25,sinterstore
描述:执行sInter命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
复制代码 代码如下:
<?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")); //结果:int(1) var_dump($redis->smembers('new')); //结果:array(1) { [0]=> string(3) "111" } ?>
26,sunion
描述:
返回一个所有指定键的并集
参数:
Keys: key1, key2, … , keyN
返回值:成功返回合并后的集,失败false
范例:
复制代码 代码如下:
<?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")); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) ?>
27,sunionstore
描述:执行sunion命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
复制代码 代码如下:
<?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")); //结果:int(4) print_r($redis->smembers('new')); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) ?>
28,sdiff
描述:返回第一个集合中存在并在其他所有集合中不存在的结果
参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.
返回值:成功返回数组,失败false
范例:
复制代码 代码如下:
<?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")); //结果:Array ( [0] => 222 [1] => 333 ) ?>
29,sdiffstore
描述:执行sdiff命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis
返回值:成功返回数字,失败false
范例:
复制代码 代码如下:
<?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")); //结果:int(2) print_r($redis->smembers('new')); //结果:Array ( [0] => 222 [1] => 333 ) ?>
30,smembers, sgetmembers
描述:
返回集合的内容
参数:Key: key
返回值:An array of elements, the contents of the set.
范例:
复制代码 代码如下:
<?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')); //结果:Array ( [0] => 111 [1] => 222 ) ?>
php-redis当中,有很多不同名字,但是功能一样的函数,例如:lrem和lremove,这里就不例举了。
The above is the detailed content of 30 code examples of commonly used methods for operating redis in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
