search
HomeBackend DevelopmentPHP ProblemWhat are the common methods for operating redis in PHP?

php redis methods: connect(), set(), get(), delete(), setnx(), exists(), incr(), decr(), getMultiple(), lpush() , rpush(), lpop(), lget(), etc.

What are the common methods for operating redis in PHP?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php Common methods for operating redis

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:

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

2,set

Description: Set the value of key and value

Parameter: Key Value

Return value: BOOL Success return: TRUE; Failure return: FALSE

Example:

<?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 related to the specified key

Parameters: key

Return value: string or BOOL Returns FALSE if the key does not exist. Otherwise, return the value corresponding to the specified key.

Example:

<?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 undefined Number of parameters, array for each key: key1 key2 key3 ... keyN

Return value: Number of items deleted

Example:

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

Description: If it does not exist, set it. If it exists, do not change it.

Parameter: key value

Return value: BOOL Successful return: TRUE; Failure Return: FALSE

Example:

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

Description: Verify whether the specified key exists

Parameter key

Return value: Bool Successful return: TRUE; Failure return: FALSE

Example:

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

Description: Numeric increment storage key value key.

Parameters: key value: The value that will be added to the key

Return value: INT the new value

Example:

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

Description: The key value is stored in numerical decrement.

Parameters: key value: the value that will be added to the key

Return value: INT the new value

Instance:

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

Description: Get the values ​​of all specified keys. If one or more keys do not exist, the value of the key in the array is false, [the collection cannot be operated, otherwise it is false]

Parameters: a list array containing the key values

Return value: Returns an array containing the values ​​of all keys

Example:

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

Description: By the head of the list Add string 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

Instance:

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

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: Return the array length on success, false on failure

Example:

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

Description: Returns and removes the first element of the list

Parameters: key

Return value: Returns the value of the first element on success, on failure Return false

Example:

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

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: Return the array length on success, false on failure

Example:

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

Description: Returns the element with the specified key stored at the specified index in the list. 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.

Parameters: key index

Return value: Return the value of the specified element if successful, false if failed

Example:

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

Description: Assign a new value to the index specified in the list. If the index does not exist, return false.

Parameter: key index value

Return value: Return successfully true, failed false

Example:

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

Description:

Returns the specified key in the range Specified elements stored from start to end in the list [can print the entire list], lGetRange(key, start, end). 0 the first element, 1 the second element... -1 the last element, -2 the penultimate element...

Parameters: key start end

Return value: Successfully returns the value found , failed false

Example:

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

Description: Remove count matching values ​​from the list starting from the head. 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: Return the number of deleted items if successful, false if failed

Example:

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

Description: Add a value to a Key. If this value is already in this Key, return FALSE.

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

31,close

描述:释放资源

<?php
$redis = new redis();
$redis->connect(&#39;127.0.0.1&#39;, 6379);
$redis->close()
?>

推荐学习:《PHP视频教程

The above is the detailed content of What are the common methods for operating redis in PHP?. 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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor