温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

redis管道pipeline怎么用

发布时间:2021-11-15 10:25:01 阅读:384 作者:小新 栏目:软件技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

小编给大家分享一下redis管道pipeline怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Redis使用的是客户端-服务器(CS)模型和请求/响应协议的TCP服务器。这意味着通常情况下一个请求会遵循以下步骤:

  • 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响应。

  • 服务端处理命令,并将结果返回给客户端。

普通模式与管道模式
分析
  • 普通模式:由于通信会有网络延迟,假如client和server之间的包传输时间需要0.125秒。那么上面的三个命令6个报文至少需要0.75秒才能完成。这样即使redis每秒能处理100个命令,而我们的client也只能一秒钟发出四个命令。这显然没有充分利用 redis的处理能力。

  • 管道模式:(pipeline)可以一次性发送多条命令并在执行完后一次性将结果返回,pipeline通过减少客户端与redis的通信次数来实现降低往返延时时间,而且Pipeline 实现的原理是队列,而队列的原理是时先进先出,这样就保证数据的顺序性。 Pipeline 的默认的同步的个数为53个,也就是说arges中累加到53条数据时会把数据提交。其过程如下图所示:client可以将三个命令放到一个tcp报文一起发送,server则可以将三条命令的处理结果放到一个tcp报文返回。
      需要注意到是用 pipeline方式打包命令发送,redis必须在处理完所有命令前先缓存起所有命令的处理结果。打包的命令越多,缓存消耗内存也越多。所以并不是打包的命令越多越好。具体多少合适需要根据具体情况测试。
      
    redis管道pipeline怎么用


案例一:将100万条数据写入redis
//产生100万条数据到指定文件
declare(strict_types=1);//开启强类型模式

function random($length$numeric false)
{
    $seed base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16$numeric ? 10 35);
    $seed $numeric ? (str_replace('0'''$seed) . '012340567890') : ($seed 'zZ' . strtoupper($seed));
    if ($numeric) {
        $hash '';
    } else {
        $hash chr(rand(126) + rand(01) * 32 + 64);
        $length--;
    }
    $max strlen($seed) - 1;
    for ($i 0$i $length$i++) {
        $hash .= $seed{mt_rand(0$max)};
    }
    return $hash;
}

$filePath './data.txt';
for ($i 0$i <= 1000000$i++) {
    $str random(10true);
    file_put_contents($filePath$str . PHP_EOL, FILE_APPEND);
}
//读取数据通过管道方式写入到redis
$lines file_get_contents($filePath);//获取文件内容
ini_set('memory_limit''-1');//不要限制Mem大小,否则会报错

$arr explode(PHP_EOL, $lines);//转换成数组

//echo $arr['1000000'] ?? 'null';

try {
    $redis new \Redis();
    $redis->connect('192.168.1.9'6379);
    $redis->auth('*****');//密码验证
    $redis->select(0);//选择库
    $redis->pipeline();//开启管道

    foreach ($arr as $key => $value) {
        $redis->hsetNx('helloworld', (string)$key$value);
    }
    $redis->exec();

    echo $redis->hGet('helloworld''1000000') . PHP_EOL;
    echo $redis->hGet('helloworld''1000001') . PHP_EOL;
} catch (\Exception $e) {
    echo $e->getMessage();
}


案例二:通过管道批量设置与读取
//批量设置
try {
    $redis = new \Redis();
    $redis->connect('192.168.1.9', 6379);
    $redis->auth('******');
    $redis->select(0);
    $redis->pipeline();//开启管道

    $redis->set('str1''h');
    $redis->set('str2''e');
    $redis->set('str3''l');
    $redis->set('str4''l');
    $redis->set('str5''o');
    $redis->set('str6''w');
    $redis->set('str7''o');
    $redis->set('str8''r');
    $redis->set('str9''l');
    $redis->set('str10''d');
    $result = $redis->exec();
    print_r($result);
} catch (\Exception $e) {
    echo $e->getMessage();
}

结果:
Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 1
)
//批量读取
try {
    $redis new \Redis();
    $redis->connect('192.168.1.9'6379);
    $redis->auth('******');
    $redis->select(0);
    $redis->pipeline();//开启管道

    $redis->get('str1');
    $redis->get('str2');
    $redis->get('str3');
    $redis->get('str4');
    $redis->get('str5');
    $redis->get('str6');
    $redis->get('str7');
    $redis->get('str8');
    $redis->get('str9');
    $redis->get('str10');
    $result $redis->exec();
    print_r($result);
} catch (\Exception $e) {
    echo $e->getMessage();
}

结果:
Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => w
    [6] => o
    [7] => r
    [8] => l
    [9] => d
)

以上是“redis管道pipeline怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×