PHP中利用Redis管道加快执行
$redis->muti($mode)->get($key)->set($key)->exec();
既然是这样的, 也就是说当我要使用管道执行一万次操作的时候需要写一万次操作在muti()的后面,,,还是我找到更好的写法?
设计者没有想到这个问题么?今天测试成功了
- <?
- php $redis = new Redis();
- $redis->connect('10.1.132.86', 6379);
- $pipe = $redis->multi(Redis::PIPELINE);
- for ($i = 0; $i < 10000; $i++) {
- $pipe->set("key::$i", str_pad($i, 4, '0', 0));
- $pipe->get("key::$i");
- }
- $replies = $pipe->exec(); echo " "; print_r($replies);
Description: Enter and exit transactional mode.
Parameters
(optional) Redis::MULTI
or Redis::PIPELINE
. Defaults toRedis::MULTI
. A Redis::MULTI
block of commands runs as a single transaction; aRedis::PIPELINE
block is simply transmitted faster to the server, but without any guarantee of atomicity.discard
cancels a transaction.
Return value
multi()
returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object untilexec()
is called.
Example
$ret = $redis->multi()
->set('key1', 'val1')
->get('key1')
->set('key2', 'val2')
->get('key2')
->exec();
/*
$ret == array(
0 => TRUE,
1 => 'val1',
2 => TRUE,
3 => 'val2');
*/
##############
简单数据mget也可以实现
mGet, getMultiple
Description: Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSE
at the position of the key.
Parameters
Array: Array containing the list of the keys
Return value
Array: Array containing the values related to keys in argument
Examples
$redis->set('key1', 'value1');
$redis->set('key2', 'value2');
$redis->set('key3', 'value3');
$redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
$redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value2', `FALSE`);
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· 一周 Star 破万的开源项目「GitHub 热点速览」
· 编码之道,道心破碎。
· 千万级大表,如何做性能调优?
· 不写代码,让 AI 生成手机 APP!保姆级教程
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方