A complete list of redis commands under PHP
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 )
Copy after login
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 )
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
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'));
Copy after login
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的值的数量
Copy after login
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
计算总个数
Copy after login
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');
Copy after login
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是单位
Copy after login
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是单位
1. Master-slave mode在redis.conf中,添加slaveof ip 端口 可作为从redis,只能读取与同步
slaveof <masterip> <masterport>
Copy after login
在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()
Copy after login
redis::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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example
