Table of Contents
2. The pipeline mode of redis is large When executing files with data, use this value transfer to reduce time
redis::multi()
Copy after login
" >2. The pipeline mode of redis is large When executing files with data, use this value transfer to reduce time
redis::multi()
Copy after login
3. Message publishing and subscription" >3. Message publishing and subscription
Home Backend Development PHP Tutorial A complete list of redis commands under PHP

A complete list of redis commands under PHP

Apr 28, 2018 pm 01:49 PM
php redis Encyclopedia

This article mainly introduces the complete list of redis commands under PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it

redis Using
application scenario cache, queue, data storage, acting on memory, it is easier to lose

##$user=User::all()-> toarray();

string type can only be a single string, not an array
set
添加
Redis::set('number', 1);
Redis::append('number',2);
//追加的
dd(Redis::strlen('number'));
//返回字符的长度
get
获取值
dd(redis::get('nember'));
getset 
先获取,完后设置该值
dd(redis::getset('nember','baidu123'));
incr 
适合做计数器
Redis::incr('number');
Redis::incrBy('number',3);
//直接加3
incrByFloat 
浮点数字直接加1.5
Redis::set('number', 1);
dd(redis::incrByFloat('number',0.03));
exists 
判断键值是否存在
dd(redis::exists('key')); 
//存在是1,不存在是0
mset mget 
批量操作
Redis::set('d', 555);
redis::mset(['a'=>1,'b'=>2,'c'=>3]);
print_r(redis::mget(['a','b','c','d']));
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 555 )
Copy after login

list type (list push pop llen)
lpus 
添加左到右面
redis::del('la');
redis::lpush('la',1);
redis::lpush('la',2);
redis::lpush('la',3);
dd(redis::lrange('la',0,6));
//展示la从0到6
rpush  添加右到左面
redis::rpush('ra',1);
redis::rpush('ra',2);
redis::rpush('ra',3);
dd(redis::lrange('ra',0,6));
dd(redis::lindex('ra',0));
//获取健的值
linsert  在列中插入(这里面的3是只第一个)
redis::linsert('ra',"BEFORE",'3','555');
//在ra队列中在3之前插入555
redis::linsert('ra',"AFTER",'3','666');
//在ra队列中在3之后插入666
dd(redis::lrange('ra',0,-1));
lpop  返回并删除列表的第一个元素(队列或秒杀)
echo(redis::lpop('ra'));
dd(redis::lrange('ra',0,-1));
rpop  返回并删除列表的最后一个元素(队列或秒杀)
echo(redis::rpop('ra'));
//返回空则列表为空
dd(redis::lrange('ra',0,-1));
blpop brpop 消息队列
redis::lpush('a',54);
redis::brpop('a',0);
//第二个参数是时间 0是永不阻塞
dd(redis::lrange('a',0,-1));
如果列表存在,则将字符串值添加到列表头(左侧)
redis::lpush('la',5);
redis::lpushx('la',6);
//右侧是rpush
redis::lpush('la',8);
dd(redis::lrange('la',0,-1));
lrem  删除指定的键值
redis::lrem('ra',6,1);
//lrem('ra',6,1) 删除左到右6个1
redis::lrem('ra',-1,1);
//lrem('ra',-1,1) 删除右到左1个1
redis::lrem('ra',0,1);
//lrem('ra',0,1) 删除全部1
dd(redis::lrange('ra',0,-1));
lset 修改 使用新值在索引处设置列表。
redis::lset('la',0,'aaaa');
dd(redis::lrange('la',0,-1));
ltrim 修剪现有列表 类似与php substr
redis::ltrim('la',0,2);
dd(redis::lrange('la',0,-1));
llen 列表的长度
dd(redis::llen('la'));
从列表的尾部弹出一个值,并将其推到另一个列表的前面。同样返回这个值
redis::rPopLPush('la','ra');
//将la的尾部,推送到列表的前面
一般用户队列防止丢失
Copy after login

set type (sadd scard sismember srem) the set content is not repeated
sadd sCard sisMember
 添加/求和/是否在集合中
redis::sadd('sa',1);
redis::sadd('sa',2);
redis::sadd('sa',3);
dd(redis::sCard('sa'));
//判断集合是否存在
redis::sisMember('sa',1)
//存在是1,不存在是0
sdiff 判断两个集合之间的差集
redis::sadd('sb',2);
redis::sadd('sb',3);
redis::sadd('sb',4);
dd(redis::sdiff('sa','sb'));
//返回不在sb中,但在sa中的值
smembers 随机删除
redis::spop('sb');
dd(redis::smembers('sb'));
srem 指定删除
redis::srem('sb',2);
//在集合sb中删除2的值
dd(redis::smembers('sb'));
sinter 判断两个集合之间的交集
dd(redis::sinter('sa','sb'));
执行几个集合的交集,并保存为新的集合
redis::sInterStore('sc','sa','sb');
//sa和sb的交集,存于sc中
dd(redis::sMembers('sc'));
//输出一个集合
将指定成员从srcKey中的集合移至dstKey处的集合
redis::smove('sa','sb',1);
//把sa中的1移到sb中
dd(redis::smembers('sb'));
返回多个集合的联合
dd(redis::sUnion('sa','sb'));
返回联合,并存于一个集合中
redis::sUnionStore('sd','sa','sc');
//把sa和sc的联合返回与sd中
dd(redis::smembers('sd'));
Copy after login

hash (hash ) type (hset, hget, hlen, hmget)
hset hget
hdel
存入/查询/删除
redis::hdel('a');
//必须先删除key为a的健,否则a本身被占用,无法给值
redis::hset('a','aaa','hello');
dd(redis::hget('a','aaa'));
hlen 
返回键a的长度
dd(redis::hlen('a'));
//a数组的下面有两个子集
hexists 
判断一个数组中是否包含某个键值
dd(redis::hexists('a','qwe'));
//1是ture 0是false
hmset hmget
批量存入全部的数组/批量获取
Redis::hmset('bbb', $user['1']);
dd(redis::hgetall('a'));
hsetnx 
给定hash默认值
本身a-aaa有值时候
redis::hsetnx('a','aaa','hello word');
dd(redis::hget('a','aaa'));
//hello
b-aaa无值的时候
redis::hSetNx('a','bbb','hello word');
dd(redis::hget('a','bbb'));
//hello word
hkeys 
类似array_keys()
dd(redis::hkeys('a'));
//数组的健和值相互替换
hvals 
类似与array_values()
dd(redis::hvals('a'));
//数组的健变成默认的0、1、2等
hstrlen 
相关字段的值的数量
dd(redis::hstrlen('a','bbb'));
//返回数组a下面健bbb的值的数量
Copy after login

sort set type (ordered set)
zadd zrem 
添加/删除
redis::zAdd('key1', 1, 'val1');
redis::zAdd('key1', 0, 'val0');
redis::zAdd('key1', 5, 'val5');
redis::zrem('key1','val1');
dd(redis::zRange('key1', 0, -1));
zcard
计算总个数
Copy after login

General commands
key 模糊搜索
redis::set('user1','my name is good man');
dd(redis::keys('user*'));
//返回的是数组,并且是user的key
dbsize 
计算key总的个数
redis::dbsize();
exists 
判断key是否存在
redis::exists('key');
//存在返回1,不存在返回0
del 
删除key
redis::del('key');
expire 
key在几秒以后过期
redis::expire('key',10);
//key在10秒以后过期.
persist 
去掉key的过期时间
redis::persist('key');
type 
key的类型
redis::type('key');
Copy after login

Other features
publish 
 消息的发布和订阅
redis::publish('aaa', 'hello, world!aaa');
redis::subscribe(array('chan-1'),'f');
//回调函数
function f($redis, $chan, $msg) {
dd($msg);
}
geo 
地理位置的定位
geoadd 添加
redis::geoadd('ggg',112.531212,37.806616,'aaa',112.130619,37.396616,'bbb');
dd(redis::geopos('ggg','beijin'));
//ggg为key,aaa与bbb为标识
geodist 计算两地距离
dd(redis::geodist('ggg','aaa','bbb','km'));
//计算aaa与bbb的距离,km是单位
Copy after login

1. Master-slave mode
在redis.conf中,添加slaveof ip 端口 可作为从redis,只能读取与同步
slaveof <masterip> <masterport>
Copy after login

2. The pipeline mode of redis is large When executing files with data, use this value transfer to reduce time
redis::multi()
Copy after login

3. Message publishing and subscription

Related recommendations:

PHP common function collection


#

The above is the detailed content of A complete list of redis commands under PHP. For more information, please follow other related articles on the PHP Chinese website!

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

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.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles