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
31,setex,ttl
Home Backend Development PHP Tutorial php操作redis常用操作方法代码小结

php操作redis常用操作方法代码小结

Jun 20, 2016 pm 01:03 PM
redis


这篇文章主要介绍了php操作redis常用方法代码例子,可以操作string类型、list类型和set类型的数据,需要的朋友可以参考下

下面整理一下php处理redis的phpredis这个扩展的例子。

1,connect

描述:实例连接到一个Redis.
参数:host: string,port: int
返回值:BOOL 成功返回:TRUE;失败返回:FALSE

< ?php 
$redis = new redis(); 
$result = $redis->connect(&#39;127.0.0.1&#39;, 6379); 
var_dump($result); //结果:bool(true) 
?> 
Copy after login

2,set

描述:设置key和value的值
参数:Key Value
返回值:BOOL 成功返回:TRUE;失败返回:FALSE

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

3,get

描述:获取有关指定键的值
参数:key
返回值:string或BOOL 如果键不存在,则返回 FALSE。否则,返回指定键对应的value值。

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

4,delete

描述:删除指定的键
参数:一个键,或不确定数目的参数,每一个关键的数组:key1 key2 key3 … keyN
返回值:删除的项数

< ?php 
$redis = new redis(); 
$redis->connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test&#39;,"1111111111111"); 
echo $redis->get(&#39;test&#39;);   //结果:1111111111111 
$redis->delete(&#39;test&#39;); 
var_dump($redis->get(&#39;test&#39;));  //结果:bool(false) 
?> 
Copy after login

5,setnx

描述:如果在数据库中不存在该键,设置关键值参数
参数:key value
返回值:BOOL 成功返回:TRUE;失败返回:FALSE

< ?php 
$redis = new redis(); 
$redis->connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test&#39;,"1111111111111"); 
$redis->setnx(&#39;test&#39;,"22222222"); 
echo $redis->get(&#39;test&#39;);  //结果:1111111111111 
$redis->delete(&#39;test&#39;); 
$redis->setnx(&#39;test&#39;,"22222222"); 
echo $redis->get(&#39;test&#39;);  //结果:22222222 
?> 
Copy after login

6,exists

描述:验证指定的键是否存在
参数key
返回值:Bool 成功返回:TRUE;失败返回:FALSE

< ?php 
$redis = new redis(); 
$redis->connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test&#39;,"1111111111111"); 
var_dump($redis->exists(&#39;test&#39;));  //结果:bool(true) 
?> 
Copy after login

7,incr

描述:数字递增存储键值键.
参数:key value:将被添加到键的值
返回值:INT the new value

< ?php 
$redis = new redis(); 
$redis->connect(&#39;127.0.0.1&#39;, 6379); 
$redis->set(&#39;test&#39;,"123"); 
var_dump($redis->incr("test"));  //结果:int(124) 
var_dump($redis->incr("test"));  //结果:int(125) 
?>
 
Copy after login

8,decr

描述:数字递减存储键值。
参数:key value:将被添加到键的值
返回值:INT the new value

< ?php 
$redis = new redis(); 
$redis->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

描述:取得所有指定键的值。如果一个或多个键不存在,该数组中该键的值为假
参数:其中包含键值的列表数组
返回值:返回包含所有键的值的数组

< ?php 
$redis = new redis(); 
$redis->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

描述:由列表头部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。
参数:key,value
返回值:成功返回数组长度,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:由列表尾部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。
参数:key,value
返回值:成功返回数组长度,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:返回和移除列表的第一个元素
参数:key
返回值:成功返回第一个元素的值 ,失败返回false

< ?php 
$redis = new redis(); 
$redis->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

描述:返回的列表的长度。如果列表不存在或为空,该命令返回0。如果该键不是列表,该命令返回FALSE。
参数:Key
返回值:成功返回数组长度,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:返回指定键存储在列表中指定的元素。 0第一个元素,1第二个… -1最后一个元素,-2的倒数第二…错误的索引或键不指向列表则返回FALSE。
参数:key index
返回值:成功返回指定元素的值,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:为列表指定的索引赋新的值,若不存在该索引返回false.
参数:key index value
返回值:成功返回true,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:
返回在该区域中的指定键列表中开始到结束存储的指定元素,lGetRange(key, start, end)。0第一个元素,1第二个元素… -1最后一个元素,-2的倒数第二…
参数:key start end
返回值:成功返回查找的值,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:从列表中从头部开始移除count个匹配的值。如果count为零,所有匹配的元素都被删除。如果count是负数,内容从尾部开始删除。
参数:key count value
返回值:成功返回删除的个数,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:为一个Key添加一个值。如果这个值已经在这个Key中,则返回FALSE。
参数:key value
返回值:成功返回true,失败false

< ?php 
$redis = new redis(); 
$redis->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

描述:删除Key中指定的value值
参数:key member
返回值:true or false

< ?php 
$redis = new redis(); 
$redis->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

描述:将Key1中的value移动到Key2中
参数:srcKey dstKey member
返回值:true or false

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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.
返回值:成功返回,交集的个数,失败false

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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.
返回值:成功返回,交集的个数,失败false

< ?php 
$redis = new redis(); 
$redis->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

< ?php 
$redis = new redis(); 
$redis->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
返回值:成功返回数字,失败false

< ?php 
$redis = new redis(); 
$redis->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.

< ?php 
$redis = new redis(); 
$redis->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

31,setex,ttl

描述:
setex设置key,value的过期时间

ttl返回某key的过期剩余时间

$redis->setex("test",20,"value"); //设置key为test,value为value的过期时间为20秒
echo $redis->get("test");
echo $redis->ttl("test"); //返回剩余过期时间(已过期返回-2,无过期时间返回-1)
Copy after login

php-redis当中,有很多不同名字,但是功能一样的函数,例如:lrem和lremove,这里就不例举了。


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

See all articles