redis管道pipeline怎么用

发布时间:2021-11-15 10:25:01 作者:小新
来源:亿速云 阅读:347

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

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

普通模式与管道模式
分析

案例一:将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(1, 26) + rand(0, 1) * 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(10, true);
    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怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. MongoDB 聚合管道(Aggregation Pipeline)
  2. redis pipe管道

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

redis pipeline

上一篇:yum和源码如何安装redis

下一篇:redis包括连接池的示例分析

相关阅读

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

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