


What are the common methods for operating redis in PHP? Summary of methods for operating redis in PHP (with code)
What this article brings to you is what are the common methods for operating redis in PHP? A summary of the method of operating redis in PHP (with code) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Background: Redis is a new product that is very popular in SNS, and memcache has long existed. However, many websites use the functions provided by redis as memcache. This is a big talent but a small use. Here are 30 There are several ways to use redis that are worth knowing.
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
redis I have a lot of operations. I used to see a relatively comprehensive blog, 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 Successful return: TRUE; Failure return: FALSE
Example:
1 2 3 4 5 |
|
2, set
Description: Set the value of key and value
Parameter: Key Value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
1 2 3 4 5 6 |
|
3,get
Description: Get the value about the specified key
Parameter: key
Return value: string or BOOL If the key does not exist, return FALSE. Otherwise, return the value corresponding to the specified key.
Example:
1 2 3 4 5 6 |
|
4, delete
Description: Delete the specified key
Parameters: a key, or an unspecified number of parameters, an array for each key :key1 key2 key3 ... keyN
Return value: Number of deleted items
Example:
1 2 3 4 5 6 7 8 |
|
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:
1 2 3 4 5 6 7 8 9 10 |
|
6, exists
Description: Verify specified Whether the key exists
Parameter key
Return value: Bool Successful return: TRUE; Failure return: FALSE
Example:
1 2 3 4 5 6 |
|
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:
1 2 3 4 5 6 7 |
|
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:
1 2 3 4 5 6 7 |
|
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:
1 2 3 4 5 6 7 8 |
|
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: Return the array length if successful, false if failed
Instance:
1 2 3 4 5 6 7 |
|
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: Return the array length on success, false on failure
Example:
1 2 3 4 5 6 7 8 9 |
|
12, lpop
Description: Return and remove the list The first element
Parameter: key
Return value: Return the value of the first element successfully, return false
Example:
1 2 3 4 5 6 7 8 9 10 |
|
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.
Parameters: Key
Return value: Returns the array length on success, false on failure
Example:
1 2 3 4 5 6 7 8 9 10 |
|
14, lget
Description: Returns the specified key stored in the list specified Elements. 0 first element, 1 second... -1 last element, -2 second 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:
1 2 3 4 5 6 7 8 9 10 |
|
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:
1 2 3 4 5 6 7 8 9 10 |
|
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 penultimate element...
Parameter: key start end
Return value: Return the value found successfully, false on failure
Example:
1 2 3 4 5 6 7 8 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
18, sadd
Description: Add a Key a value. If this value is already in this Key, return FALSE.
Parameter: key value
Return value: true on success, false on failure
Example:
1 2 3 4 5 6 7 8 |
|
19,sremove
描述:删除Key中指定的value值
参数:key member
返回值:true or false
范例:
1 2 3 4 5 6 7 8 9 |
|
20,smove
描述:将Key1中的value移动到Key2中
参数:srcKey dstKey member
返回值:true or false
范例:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
21,scontains
描述:检查集合中是否存在指定的值。
参数:key value
返回值:true or false
范例:
1 2 3 4 5 6 7 8 9 |
|
22,ssize
描述:返回集合中存储值的数量
参数:key
返回值:成功返回数组个数,失败0
范例:
1 2 3 4 5 6 7 8 |
|
23,spop
描述:随机移除并返回key中的一个值
参数:key
返回值:成功返回删除的值,失败false
范例:
1 2 3 4 5 6 7 8 9 |
|
24,sinter
描述:返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。
参数:key1, key2, keyN
返回值:成功返回数组交集,失败false
范例:
1 2 3 4 5 6 7 8 9 10 11 |
|
25,sinterstore
描述:执行sInter命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
26,sunion
描述:
返回一个所有指定键的并集
参数:
Keys: key1, key2, … , keyN
返回值:成功返回合并后的集,失败false
范例:
1 2 3 4 5 6 7 8 9 10 11 |
|
27,sunionstore
描述:执行sunion命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
28,sdiff
描述:返回第一个集合中存在并在其他所有集合中不存在的结果
参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.
返回值:成功返回数组,失败false
范例:
1 2 3 4 5 6 7 8 9 10 11 |
|
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
范例:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
30,smembers, sgetmembers
描述:
返回集合的内容
参数:Key: key
返回值:An array of elements, the contents of the set.
范例:
1 2 3 4 5 6 7 8 |
|
php-redis当中,有很多不同名字,但是功能一样的函数,例如:lrem和lremove,这里就不例举
相关推荐:
The above is the detailed content of What are the common methods for operating redis in PHP? Summary of methods for operating redis in PHP (with code). 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

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
