search
HomeBackend DevelopmentPHP TutorialDetailed explanation of steps to operate Redis with PHP

This time I will bring you a detailed explanation of the steps for operating Redis in PHP. What are the precautions for operating Redis in PHP? Here are practical cases, let’s take a look.

1. Redis connection and authentication

//连接参数:ip、端口、连接超时时间,连接成功返回true,否则返回false
$ret = $redis->connect('127.0.0.1', 6379, 30);
//密码认证:成功返回true,否则返回false
$ret = $redis->auth('123456');

2. String operation

//设置键值:成功返回true,否则返回false
$redis->set('mystr', 'Welcome!');
//获取键值:成功返回String类型键值,若key不存在或不是String类型则返回false
$ret = $redis->get('mystr');
//从某个key所存储的字符串的指定偏移量开始,替换为另一指定字符串,成功返回替换后新字符串的长度。
$ret = $redis->setRange('mystr', 7, ' to Beijing!');
//获取存储在指定key中字符串的子字符串。
$ret = $redis->getRange('mystr', 0, 6);
//设置新值,返回旧值:若key不存在则设置值,返回false
$ret = $redis->getSet('mystr', 'hi man');
//一次设置多个键值对:成功返回true。
$ret = $redis->mset(['name' => 'jet', 'age' => 18]);
//一次获取多个key的值:返回一个键值对数组,其中不存在的key值为false。
$ret = $redis->mget(['name', 'age']);
//设置指定key的值及其过期时间,单位:秒。
//参数:键名,过期时间,键值。成功返回true。
$ret = $redis->setex('name', 10, 'jetwu');
//以毫秒为单位设置指定key的值和过期时间。成功返回true。
$ret = $redis->psetex('name', 10, 'jetwu');
//key的值不存在时,才为其设置值。key不存在且设置成功返回true,否则返回false。
$ret = $redis->setnx('name', 'boby');
//setnx命令的批量操作。只有在给定所有key都不存在的时候才能设置成功,只要其中一个key存在,所有key都无法设置成功。
$ret = $redis->msetnx(['country' => 'China', 'city' => 'Shenzhen']);
//获取指定key存储的字符串的长度,key不存在返回0,不为字符串返回false。
$ret = $redis->strlen('name');
//将指定key存储的数字值增加1。若key不存在会先初始化为0再增加1,若key存储的不是整数值则返回false。成功返回key新值。
$ret = $redis->incr('age');
//给指定key存储的数字值增加指定增量值。
$ret = $redis->incrBy('age', 10);
//给指定key存储的数字值增加指定浮点数增量。
$ret = $redis->incrByFloat('age', 1.5);
//将指定key存储的数字值减一。
$ret = $redis->decr('age');
//将指定key存储的数字值减去指定减量值。
$ret = $redis->decrBy('age', 10);
//为指定key追加值到原值末尾,若key不存在则相对于set()函数。
$ret = $redis->append('mystr', 'haha');

3. Hash operation

//为hash表中的字段赋值。成功返回1,失败返回0。若hash表不存在会先创建表再赋值,若字段已存在会覆盖旧值。
$ret = $redis->hSet('user', 'realname', 'jetwu');
//获取hash表中指定字段的值。若hash表不存在则返回false。
$ret = $redis->hGet('user', 'realname');
//查看hash表的某个字段是否存在,存在返回true,否则返回false。
$ret = $redis->hExists('user', 'realname');
//删除hash表的一个字段,不支持删除多个字段。成功返回1,否则返回0。
$ret = $redis->hDel('user', 'realname');
//同时设置某个hash表的多个字段值。成功返回true。
$ret = $redis->hMset('user', ['name' => 'jet', 'age' => 18]);
//同时获取某个hash表的多个字段值。其中不存在的字段值为false。
$ret = $redis->hMget('user', ['name', 'age']);
//获取某个hash表所有的字段和值。
$ret = $redis->hGetAll('user');
//获取某个hash表所有字段名。hash表不存在时返回空数组,key不为hash表时返回false。
$ret = $redis->hKeys('user');
//获取某个hash表所有字段值。
$ret = $redis->hVals('user');
//为hash表中不存在的字段赋值。若hash表不存在则先创建,若字段已存在则不做任何操作。设置成功返回true,否则返回false。
$ret = $redis->hSetNx('user', 'realname', 'jetwu');
//获取某个hash表的字段数量。若hash表不存在返回0,若key不为hash表则返回false。
$ret = $redis->hLen('user');
//为hash表中的指定字段加上指定增量值,若增量值为负数则相当于减法操作。若hash表不存在则先创建,若字段不存在则先初始化值为0再进行操作,若字段值为字符串则返回false。设置成功返回字段新值。
$ret = $redis->hIncrBy('user', 'age', 10);
//为hash表中的指定字段加上指定浮点数增量值。
$ret = $redis->hIncrBy('user', 'age', 1.5);

4. List operation

//从list头部插入一个值。
$ret = $redis->lPush('city', 'guangzhou');
//从list尾部插入一个值。
$ret = $redis->rPush('city', 'guangzhou');
//获取列表指定区间中的元素。0表示列表第一个元素,-1表示最后一个元素,-2表示倒数第二个元素。
$ret = $redis->lrange('city', 0, -1);//查看队列所有元素
//将一个插入已存在的列表头部,列表不存在时操作无效。
$ret = $redis->lPushx('city', 'hangzhou');
//将一个或多个值插入已存在的列表尾部,列表不存在时操作无效。
$ret = $redis->rPushx('city', 'hangzhou');
//移除并返回列表的第一个元素,若key不存在或不是列表则返回false。
$ret = $redis->lPop('city');
//移除并返回列表的最后一个元素,若key不存在或不是列表则返回false。
$ret = $redis->rPop('city');
//移除并获取列表的第一个元素。如果列表没有元素则会阻塞列表直到等待超时或发现可弹出元素为止。
//参数:key,超时时间(单位:秒)
//返回值:[0=>key,1=>value],超时返回[]
$ret = $redis->blPop('city', 10);
//移除并获取列表的最后一个元素。如果列表没有元素则会阻塞列表直到等待超时或发现可弹出元素为止。
//参数:key,超时时间(单位:秒)
//返回值:[0=>key,1=>value],超时返回[]
$ret = $redis->brPop('city', 10);
//移除列表中最后一个元素,将其插入另一个列表头部,并返回这个元素。若源列表没有元素则返回false。
$ret = $redis->rpoplpush('city', 'city2');
//移除列表中最后一个元素,将其插入另一个列表头部,并返回这个元素。如果列表没有元素则会阻塞列表直到等待超时或发现可弹出元素为止。
//参数:源列表,目标列表,超时时间(单位:秒)
//超时返回false
$ret = $redis->brpoplpush('city', 'city2', 10);
//返回列表长度。
$ret = $redis->lLen('city');
//通过索引获取列表中的元素。若索引超出列表范围则返回false。
$ret = $redis->lindex('city', 0);
//通过索引设置列表中元素的值。若是索引超出范围,或对一个空列表进行lset操作,则返回false。
$ret = $redis->lSet('city', 2, 'changsha');
//在列表中指定元素前或后面插入元素。若指定元素不在列表中,或列表不存在时,不执行任何操作。
//参数:列表key,Redis::AFTER或Redis::BEFORE,基准元素,插入元素
//返回值:插入成功返回插入后列表元素个数,若基准元素不存在返回-1,若key不存在返回0,若key不是列表返回false。
$ret = $redis->lInsert('city', Redis::AFTER, 'changsha', 'nanjing');
//根据第三个参数count的值,移除列表中与参数value相等的元素。
//count > 0 : 从表头开始向表尾搜索,移除与value相等的元素,数量为count。
//count < 0 : 从表尾开始向表头搜索,移除与value相等的元素,数量为count的绝对值。
//count = 0 : 移除表中所有与value相等的值。
//返回实际删除元素个数
$ret = $redis->lrem(&#39;city&#39;, &#39;guangzhou&#39;, -2);
//对一个列表进行修剪,只保留指定区间的元素,其他元素都删除。成功返回true。
$ret = $redis->ltrim(&#39;city&#39;, 1, 4);

5. Set operation

//将一个元素加入集合,已经存在集合中的元素则忽略。若集合不存在则先创建,若key不是集合类型则返回false,若元素已存在返回0,插入成功返回1。
$ret = $redis->sAdd(&#39;myset&#39;, &#39;hello&#39;);
//返回集合中所有成员。
$ret = $redis->sMembers(&#39;myset&#39;);
//判断指定元素是否是指定集合的成员,是返回true,否则返回false。
$ret = $redis->sismember(&#39;myset&#39;, &#39;hello&#39;);
//返回集合中元素的数量。
$ret = $redis->scard(&#39;myset&#39;);
//移除并返回集合中的一个随机元素。
$ret = $redis->sPop(&#39;myset&#39;);
//返回集合中的一个或多个随机成员元素,返回元素的数量和情况由函数的第二个参数count决定:
//如果count为正数,且小于集合基数,那么命令返回一个包含count个元素的数组,数组中的元素各不相同。
//如果count大于等于集合基数,那么返回整个集合。
//如果count为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为count的绝对值。
$ret = $redis->sRandMember(&#39;myset&#39;, 2);
//移除集合中指定的一个元素,忽略不存在的元素。删除成功返回1,否则返回0。
$ret = $redis->srem(&#39;myset&#39;, &#39;hello&#39;);
//迭代集合中的元素。
//参数:key,迭代器变量,匹配模式,每次返回元素数量(默认为10个)
$ret = $redis->sscan(&#39;myset&#39;, $it, &#39;a*&#39;, 5);
//将指定成员从一个源集合移动到一个目的集合。若源集合不存在或不包含指定元素则不做任何操作,返回false。
//参数:源集合,目标集合,移动元素
$ret = $redis->sMove(&#39;myset&#39;, &#39;myset2&#39;, &#39;aaa&#39;);
//返回所有给定集合之间的差集,不存在的集合视为空集。
$ret = $redis->sDiff(&#39;myset&#39;, &#39;myset2&#39;, &#39;myset3&#39;);
//将所有给定集合之间的差集存储在指定的目的集合中。若目的集合已存在则覆盖它。返回差集元素个数。
//参数:第一个参数为目标集合,存储差集。
$ret = $redis->sDiffStore(&#39;myset3&#39;, &#39;myset&#39;, &#39;myset2&#39;);
//返回所有给定集合的交集,不存在的集合视为空集。
$ret = $redis->sInter(&#39;myset&#39;, &#39;myset2&#39;, &#39;myset3&#39;);
//将所有给定集合的交集存储在指定的目的集合中。若目的集合已存在则覆盖它。返回交集元素个数。
//参数:第一个参数为目标集合,存储交集。
$ret = $redis->sInterStore(&#39;myset4&#39;, &#39;myset&#39;, &#39;myset2&#39;, &#39;myset3&#39;);
//返回所有给定集合的并集,不存在的集合视为空集。
$ret = $redis->sUnion(&#39;myset&#39;, &#39;myset2&#39;, &#39;myset3&#39;);
//将所有给定集合的并集存储在指定的目的集合中。若目的集合已存在则覆盖它。返回并集元素个数。
//参数:第一个参数为目标集合,存储并集。
$ret = $redis->sUnionStore(&#39;myset4&#39;, &#39;myset&#39;, &#39;myset2&#39;, &#39;myset3&#39;);

6. Zset operation

//将一个或多个成员元素及其分数值加入到有序集当中。如果某个成员已经是有序集的成员,则更新这个成员的分数值,并通过重新插入这个成员元素,来保证该成员在正确的位置上。分数值可以是整数值或双精度浮点数。
$ret = $redis->zAdd(&#39;scores&#39;, 98, &#39;English&#39;, 90, &#39;physics&#39;);
//返回有序集中指定区间内的成员。成员按分数值递增排序,分数值相同的则按字典序来排序。
//参数:第四个参数表示是否返回各个元素的分数值,默认为false。
$ret = $redis->zRange(&#39;scores&#39;, 0, -1, true);//查看Zset所有成员以及它们各自的分数值
//返回有序集中指定区间内的成员。成员按分数值递减排序,分数值相同的则按字典序的逆序来排序。
$ret = $redis->zReverseRange(&#39;scores&#39;, 0, -1, true);
//返回有序集中指定分数区间的成员列表,按分数值递增排序,分数值相同的则按字典序来排序。默认使用闭区间。
$ret = $redis->zRangeByScore(&#39;scores&#39;, 90, 100, [&#39;withscores&#39;=>true]);
//返回有序集中指定分数区间的成员列表,按分数值递减排序,分数值相同的则按字典序的逆序来排序。注意,区间表示的时候大值在前,小值在后,默认使用闭区间。
$ret = $redis->zRevRangeByScore(&#39;scores&#39;, 100, 90, [&#39;withscores&#39;=>true]);
//迭代有序集合中的元素。
//返回值:[元素名=>分数值,,..]
$ret = $redis->zscan(&#39;scores&#39;, $it, &#39;&#39;, 10);
//返回指定有序集的元素数量。
$ret = $redis->zCard(&#39;scores&#39;);
//返回有序集中指定分数区间的成员数量。
$ret = $redis->zCount(&#39;scores&#39;, 90, 100);
//返回有序集中指定成员的分数值。若成员不存在则返回false。
$ret = $redis->zScore(&#39;scores&#39;, &#39;math&#39;);
//返回有序集中指定成员的排名,按分数值递增排序。分数值最小者排名为0。
$ret = $redis->zRank(&#39;scores&#39;, &#39;chemistry&#39;);
//返回有序集中指定成员的排名,按分数值递减排序。分数值最大者排名为0。
$ret = $redis->zRevRank(&#39;scores&#39;, &#39;chemistry&#39;);
//移除有序集中的一个或多个成员,忽略不存在的成员。返回删除的元素个数。
$ret = $redis->zRem(&#39;scores&#39;, &#39;chemistry&#39;, &#39;English&#39;);
//移除有序集中指定排名区间的所有成员。
$ret = $redis->zRemRangeByRank(&#39;scores&#39;, 0, 2);
//移除有序集中指定分数值区间的所有成员。
$ret = $redis->zRemRangeByScore(&#39;scores&#39;, 80, 90);
//对有序集中指定成员的分数值增加指定增量值。若为负数则做减法,若有序集不存在则先创建,若有序集中没有对应成员则先添加,最后再操作。
$ret = $redis->zIncrBy(&#39;scores&#39;, 2, &#39;Chinese&#39;);
//计算给定一个或多个有序集的交集,并将其存储到一个目的有序集中。结果集中某个成员的分数值是所有给定集下该成员分数值之和。
$ret = $redis->zinterstore(&#39;zset3&#39;, &#39;zset2&#39;, &#39;zset1&#39;);
//计算给定一个或多个有序集的并集,并将其存储到一个目的有序集中。结果集中某个成员的分数值是所有给定集下该成员分数值之和。
$ret = $redis->zunionstore(&#39;zset3&#39;, &#39;zset2&#39;, &#39;zset1&#39;);

I believe you have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

PHP prototype design pattern use case analysis

Detailed explanation of the php-fpm service startup script steps

The above is the detailed content of Detailed explanation of steps to operate Redis with 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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 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: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 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.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools