背景:现在的数据已经写到了redis队列里面,完成了入栈的操作,后期打算从redis获取数据,完成出栈的操作,出栈后然后做一系列的逻辑处理

      环境: VMware虚拟机  内存:1G   硬盘:60G  php环境:PHP Version 5.6.31  php框架:TP

      测试:事先在redis里面插入了30W条数据,分别模拟php单进程和多进程(3个多进程)

php单进程代码:

public function sinsert() {
        //$redis = new Redis();  
        //$redis->connect('127.0.0.1', 6379);
        //写到list类型缓存里面
        //lpush('zc_hk_list', serialize($zc_hk))
        //$accountRepayModel = D('AccountRepay');
        $this->repay->start();
        for($i=1;$i<=300000;$i++) {
          $arr = array("k"=>$i,"v"=>"dan".$i);
          //var_dump($arr);//exit;         
            $this->redis->lpush('hongniu', serialize($arr));
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '全量写入redis数据发送脚本');
        $this->repay->clear();
        /*$key = 'name';
        $value = $redis->get($key);
        var_dump($value);*/
    }
    public function slist() {
        $this->repay->start();
        $list_len  = $this->redis->llen('hongniu');
        Log::write('单进程一共有:'.$list_len."条");

        if ($list_len == 0) return;
        while (true) {
            //$result     = $this->redis->rpop('hongniu');
            $result     = $this->redis->lpop('hongniu');
            if(empty($result)) {break;}
            $repayment_data= unserialize($result);
            if(is_array($repayment_data) && !empty($repayment_data)) {
             //Log::write('多进程写日志的结果是1:'.json_encode($repayment_data));
             Log::write('单进程写日志的结果是:'.json_encode($repayment_data));
            }
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '单进程导出redis数据脚本');
        $this->repay->clear();
    }

php多进程代码:

public function binsert() {
        //$redis = new Redis();  
        //$redis->connect('127.0.0.1', 6379);
        //写到list类型缓存里面
        //lpush('zc_hk_list', serialize($zc_hk))
        //$accountRepayModel = D('AccountRepay');
        $this->redis80 = new Redis();
        $this->redis80->connect('127.0.0.1', 6380);
        
        $this->redis81 = new Redis();
        $this->redis81->connect('127.0.0.1', 6381);
        $this->repay->start();
        for($i=1;$i<=300000;$i++) {
          $arr = array("k"=>$i,"v"=>"dan".$i);
          //var_dump($arr);//exit;
          if($i%3==0) {
            $this->redis->lpush('hongniu0', serialize($arr));
          }elseif($i%3==1) { //
            $this->redis80->lpush('hongniu1', serialize($arr));
          }else{
            $this->redis81->lpush('hongniu2', serialize($arr));
          }
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '全量分别写入不同redis数据发送脚本');
        $this->repay->clear();
        /*$key = 'name';
        $value = $redis->get($key);
        var_dump($value);*/
    }
    public function blist1() {
        $this->repay->start();
        $list_len  = $this->redis->llen('hongniu0');
        Log::write('多进程一一共有:'.$list_len."条");
     
        if ($list_len == 0) return;
        
        while (true) {
            //$result     = $this->redis->rpop('hongniu');
            $result     = $this->redis->lpop('hongniu0');
            if(empty($result)) {break;}
            $repayment_data= unserialize($result);
            if(is_array($repayment_data) && !empty($repayment_data)) {
             //Log::write('多进程写日志的结果是1:'.json_encode($repayment_data));
              Log::write('多进程写日志的结果是1:'.json_encode($repayment_data), Log::INFO, Log::FILE, LOG_PATH.'blist1_'.date('Ymd').'.log');
            }
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '多进程导出redis数据脚本1');
        $this->repay->clear();
    }  
    public function blist2() {
        $this->redis80 = new Redis();
        $this->redis80->connect('127.0.0.1', 6380);

        $this->repay->start();
        $list_len  = $this->redis80->llen('hongniu1');
        Log::write('多进程二一共有:'.$list_len."条");
        if ($list_len == 0) return;
        
        while (true) {
            //$result     = $this->redis->rpop('hongniu');
            $result     = $this->redis80->lpop('hongniu1');
            if(empty($result)) {break;}
            $repayment_data= unserialize($result);
            if(is_array($repayment_data) && !empty($repayment_data)) {
             //Log::write('多进程写日志的结果是1:'.json_encode($repayment_data));
              Log::write('多进程写日志的结果是2:'.json_encode($repayment_data), Log::INFO, Log::FILE, LOG_PATH.'blist2_'.date('Ymd').'.log');
            }
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '多进程导出redis数据脚本2');
        $this->repay->clear();
    }  
    public function blist3() {

        $this->redis81 = new Redis();
        $this->redis81->connect('127.0.0.1', 6381);
        
        $this->repay->start();
        $list_len  = $this->redis81->llen('hongniu2');
        Log::write('多进程三一共有:'.$list_len."条");
        if ($list_len == 0) return;
        
        while (true) {
            //$result     = $this->redis->rpop('hongniu');
            $result     = $this->redis81->lpop('hongniu2');
            if(empty($result)) {break;}
            $repayment_data= unserialize($result);
            if(is_array($repayment_data) && !empty($repayment_data)) {
             //Log::write('多进程写日志的结果是1:'.json_encode($repayment_data));
              Log::write('多进程写日志的结果是3:'.json_encode($repayment_data), Log::INFO, Log::FILE, LOG_PATH.'blist3_'.date('Ymd').'.log');
            }
        }
        $this->repay->stop();
        $spent_time = $this->repay->spent($echo=true, '多进程导出redis数据脚本3');
        $this->repay->clear();
    }

shell代码:

curl http://192.168.32.128/index.php/Business/blist1
curl http://192.168.32.128/index.php/Business/blist2
curl http://192.168.32.128/index.php/Business/blist3

测试结果如下所示:

总结:因为redis是单进程单线程的,在只有一个redis实例的情况下,不管开启多少个进程,读取速度不会变快!

所以想要读取速度加快可以开启多个redis实例,写入的时候写入到多个redis实例中,读取的时候也是从多个redis实例读取。






Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐