A complete list of redis commands under PHP

不言
Release: 2023-03-25 06:44:01
Original
2187 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template