Home  >  Article  >  Backend Development  >  A complete collection of common method codes and examples for using redis in PHP operations

A complete collection of common method codes and examples for using redis in PHP operations

不言
不言Original
2018-04-20 14:37:481546browse

This article introduces the code and examples of common methods for using redis in PHP operations. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1 .connect

Description: The instance connects to a Redis.

Parameters: host: string, port: int

Return value: BOOL Successfully returned : TRUE; Failure returns: FALSE
##

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

2. set

Description: Set the values ​​of key and value
Parameters: Key Value

Return value: BOOL Successful return: TRUE ;Failure return: 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)

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.

<?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"  
?>

4. delete

Description: Delete the specified key

Parameters: a key, or an indeterminate number of parameters, an array for each key: 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)  
?>

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

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)

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)

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)  
?>

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 )  
?>

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)

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)  
?>

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"

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)

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"

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"  
?>

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 )  
?>

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 )  
?>

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 )  
?>

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 )  
?>

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 )

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)  
?>

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  
?>

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"  
?>

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" }

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" }  
?>

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 )  
?>

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 )  
?>

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 )  
?>

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 )  
?>

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 )  
?>

相关推荐:

关于php操作mysql

php操作字符串转换成php数组的几种方法


The above is the detailed content of A complete collection of common method codes and examples for using redis in PHP operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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