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 arrayset
添加
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 )
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的尾部,推送到列表的前面
一般用户队列防止丢失
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'));
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的值的数量
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
计算总个数
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');
Other featurespublish
消息的发布和订阅
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是单位
1. Master-slave mode在redis.conf中,添加slaveof ip 端口 可作为从redis,只能读取与同步
slaveof <masterip> <masterport>
2. The pipeline mode of redis is large When executing files with data, use this value transfer to reduce timeredis::multi()
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!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
