Table of Contents
1, connect
2,set
3, get
4, delete
5, setnx
6, exists
7, incr
8, decr
9, getMultiple
10, lpush
11, rpush
12, lpop
13, lsize,llen
14, lget
15, lset
16, lgetrange
17,lremove
18, sadd
19, sremove
20,smove
21,scontains
22,ssize
23,spop
24,sinter
25,sinterstore
26,sunion
27,sunionstore
28,sdiff
29,sdiffstore
30,smembers, sgetmembers
Home Backend Development PHP Tutorial Summary of common methods for operating redis in PHP

Summary of common methods for operating redis in PHP

Sep 08, 2017 am 09:14 AM
php redis Summarize

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 returned successfully : TRUE; Failure return: FALSE

Example:

 connect('127.0.0.1', 6379); 
var_dump($result); //结果:bool(true) 
?>
Copy after login

2,set

Description: Set the value of key and value
Parameter: Key Value
Return Value: BOOL Successful return: TRUE; Failure return: FALSE
Example:

 connect('127.0.0.1', 6379); 
$result = $redis->set('test',"11111111111"); 
var_dump($result);    //结果:bool(true) ?>
Copy after login

3, get

Description: Get the value of the specified key
Parameter: key
Return value: string or BOOL If the key does not exist, it returns FALSE. Otherwise, return the value corresponding to the specified key.
Example:

 connect('127.0.0.1', 6379); 
$result = $redis->get('test'); 
var_dump($result);   //结果:string(11) "11111111111" ?>
Copy after login

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: number of deleted items
Example:

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) ?>
Copy after login

5, setnx

Description: If the key does not exist in the database, set the key value parameter
Parameter: key value
Return value: BOOL Successful return: TRUE; Failure return: FALSE

Example:

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 ?>
Copy after login

6, exists

Description: Verify specified Whether the key exists
Parameter key
Return value: Bool Success return: TRUE; Failure return: FALSE
Example:

connect('127.0.0.1', 6379); 
$redis->set('test',"1111111111111"); 
var_dump($redis->exists('test'));  //结果:bool(true) ?>
Copy after login

7, incr

Description: Number increment Stores the key value key.
Parameters: key value: the value that will be added to the key
Return value: INT the new value
Example:

<p style="margin-bottom: 7px;">connect(&#39;127.0.0.1&#39;, 6379); <br/>$redis->set(&#39;test&#39;,"123"); <br/>var_dump($redis->incr("test"));  //结果:int(124) var_dump($redis->incr("test"));  //结果:int(125) ?><br/></p>
Copy after login

8, decr

Description: Store the key value numerically in descending order.
Parameters: key value: the value that will be added to the key
Return value: INT the new value
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test&#39;,"123"); 
var_dump($redis->decr("test"));  //结果:int(122) var_dump($redis->decr("test"));  //结果:int(121) ?>
Copy after login

9, getMultiple

Description: Get all Specifies the value of the key. 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:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test1&#39;,"1"); 
$redis->set(&#39;test2&#39;,"2"); 
$result = $redis->getMultiple(array(&#39;test1&#39;,&#39;test2&#39;)); 
print_r($result);   //结果:Array ( [0] => 1 [1] => 2 ) ?>
Copy after login

10, lpush

Description: Add a string value from 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 length of the array on success, false on failure
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
var_dump($redis->lpush("test","111"));   //结果:int(1) var_dump($redis->lpush("test","222"));   //结果:int(2) ?>
Copy after login

11, rpush

Description: Add a string from the end of the list value. 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 length of the array on success, false on failure
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
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) ?>
Copy after login

12, lpop

Description: Returns and removes the list The first element
Parameters: key
Return value: Returns the value of the first element if successful, false if failed
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->lpush("test","111"); 
$redis->lpush("test","222"); 
$redis->rpush("test","333"); 
$redis->rpush("test","444"); 
var_dump($redis->lpop("test"));  //结果:string(3) "222" ?>
Copy after login

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, this command returns FALSE.
Parameter: Key
Return value: Returns the array length on success, false on failure
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->lpush("test","111"); 
$redis->lpush("test","222"); 
$redis->rpush("test","333"); 
$redis->rpush("test","444"); 
var_dump($redis->lsize("test"));  //结果:int(4) ?>
Copy after login

14, lget

Description: Returns the specified key stored in the list specified Elements. 0 for the first element, 1 for the second... -1 for the last element, -2 for the second to last... Returns FALSE if the wrong index or key does not point to the list.
Parameter: key index
Return value: Return the value of the specified element successfully, false on failure
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$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" ?>
Copy after login

15, lset

Description: The index specified for the list Assign a new value, if the index does not exist, return false.
Parameter: key index value
Return value: Return true if successful, false if failed
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$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" ?>
Copy after login

16, lgetrange

Description:
Returns the specified element stored from start to end in the specified key list in the area, lGetRange(key, start, end). 0 the first element, 1 the second element... -1 the last element, -2 the second to last...
Parameters: key start end
Return value: Return the value found successfully, false on failure
Example:

 connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->lpush("test","111"); 
$redis->lpush("test","222"); 
print_r($redis->lgetrange("test",0,-1));  //结果:Array ( [0] => 222 [1] => 111 ) ?>
Copy after login

17,lremove

Description: Remove count matching values ​​from the head of the list. If count is zero, all matching elements are removed. If count is negative, the content is deleted from the end.
Parameter: key count value
Return value: Returns the number of deleted items if successful, false if failed
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->lpush(&#39;test&#39;,&#39;a&#39;); 
$redis->lpush(&#39;test&#39;,&#39;b&#39;); 
$redis->lpush(&#39;test&#39;,&#39;c&#39;); 
$redis->rpush(&#39;test&#39;,&#39;a&#39;); 
print_r($redis->lgetrange(&#39;test&#39;, 0, -1)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a ) var_dump($redis->lremove(&#39;test&#39;,&#39;a&#39;,2));   //结果:int(2) print_r($redis->lgetrange(&#39;test&#39;, 0, -1)); //结果:Array ( [0] => c [1] => b ) ?>
Copy after login

18, sadd

Description: Add a Key a value. If this value is already in this Key, return FALSE.
Parameter: key value
Return value: Return true on success, false on failure
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
var_dump($redis->sadd(&#39;test&#39;,&#39;111&#39;));   //结果:bool(true) var_dump($redis->sadd(&#39;test&#39;,&#39;333&#39;));   //结果:bool(true) print_r($redis->sort(&#39;test&#39;)); //结果:Array ( [0] => 111 [1] => 333 ) ?>
Copy after login

19, sremove

Description: Delete the value specified in Key
Parameter: key member
Return value: true or false
Example:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd(&#39;test&#39;,&#39;111&#39;); 
$redis->sadd(&#39;test&#39;,&#39;333&#39;); 
$redis->sremove(&#39;test&#39;,&#39;111&#39;); 
print_r($redis->sort(&#39;test&#39;));    //结果:Array ( [0] => 333 ) ?>
Copy after login

20,smove

Description: Move the value in Key1 to Key2
Parameters: srcKey dstKey member
Return value: true or false
Example

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->delete(&#39;test1&#39;); 
$redis->sadd(&#39;test&#39;,&#39;111&#39;); 
$redis->sadd(&#39;test&#39;,&#39;333&#39;); 
$redis->sadd(&#39;test1&#39;,&#39;222&#39;); 
$redis->sadd(&#39;test1&#39;,&#39;444&#39;); 
$redis->smove(&#39;test&#39;,"test1",&#39;111&#39;); 
print_r($redis->sort(&#39;test1&#39;));    //结果:Array ( [0] => 111 [1] => 222 [2] => 444 ) ?>
Copy after login

21,scontains

描述:检查集合中是否存在指定的值。
参数:key value
返回值:true or false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd(&#39;test&#39;,&#39;111&#39;); 
$redis->sadd(&#39;test&#39;,&#39;112&#39;); 
$redis->sadd(&#39;test&#39;,&#39;113&#39;); 
var_dump($redis->scontains(&#39;test&#39;, &#39;111&#39;)); //结果:bool(true) ?>
Copy after login

22,ssize

描述:返回集合中存储值的数量
参数:key
返回值:成功返回数组个数,失败0
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd(&#39;test&#39;,&#39;111&#39;); 
$redis->sadd(&#39;test&#39;,&#39;112&#39;); 
echo $redis->ssize(&#39;test&#39;);   //结果:2 ?>
Copy after login

23,spop

描述:随机移除并返回key中的一个值
参数:key
返回值:成功返回删除的值,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd("test","111"); 
$redis->sadd("test","222"); 
$redis->sadd("test","333"); 
var_dump($redis->spop("test"));  //结果:string(3) "333" ?>
Copy after login

24,sinter

描述:返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。
参数:key1, key2, keyN
返回值:成功返回数组交集,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$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" } ?>
Copy after login

25,sinterstore

描述:执行sInter命令并把结果储存到新建的变量中。
参数:

Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Copy after login
Copy after login

返回值:成功返回,交集的个数,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd("test","111"); 
$redis->sadd("test","222"); 
$redis->sadd("test","333"); 
$redis->sadd("test1","111"); 
$redis->sadd("test1","444"); 
var_dump($redis->sinterstore(&#39;new&#39;,"test","test1"));  //结果:int(1) var_dump($redis->smembers(&#39;new&#39;));  //结果:array(1) { [0]=> string(3) "111" } ?>
Copy after login

26,sunion

描述:
返回一个所有指定键的并集
参数:
Keys: key1, key2, … , keyN
返回值:成功返回合并后的集,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$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 ) ?>
Copy after login

27,sunionstore

描述:执行sunion命令并把结果储存到新建的变量中。
参数:

Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Copy after login
Copy after login

返回值:成功返回,交集的个数,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd("test","111"); 
$redis->sadd("test","222"); 
$redis->sadd("test","333"); 
$redis->sadd("test1","111"); 
$redis->sadd("test1","444"); 
var_dump($redis->sinterstore(&#39;new&#39;,"test","test1"));  //结果:int(4) print_r($redis->smembers(&#39;new&#39;));  //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) ?>
Copy after login

28,sdiff

描述:返回第一个集合中存在并在其他所有集合中不存在的结果
参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.
返回值:成功返回数组,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$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 ) ?>
Copy after login

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
Copy after login

返回值:成功返回数字,失败false
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd("test","111"); 
$redis->sadd("test","222"); 
$redis->sadd("test","333"); 
$redis->sadd("test1","111"); 
$redis->sadd("test1","444"); 
var_dump($redis->sdiffstore(&#39;new&#39;,"test","test1"));  //结果:int(2) print_r($redis->smembers(&#39;new&#39;));  //结果:Array ( [0] => 222 [1] => 333 ) ?>
Copy after login

30,smembers, sgetmembers

描述:
返回集合的内容
参数:Key: key
返回值:An array of elements, the contents of the set.
范例:

connect(&#39;127.0.0.1&#39;, 6379); 
$redis->delete(&#39;test&#39;); 
$redis->sadd("test","111"); 
$redis->sadd("test","222"); 
print_r($redis->smembers(&#39;test&#39;));  //结果:Array ( [0] => 111 [1] => 222 ) ?>
Copy after login

The above is the detailed content of Summary of common methods for operating redis in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles