【问题标题】:Why do php redis get data returns with colons为什么php redis获取数据返回时带冒号
【发布时间】:2022-12-28 10:36:50
【问题描述】:

问题

我写了代码的多线程实现,并尝试使用redis作为计数器,这是我的代码。当我尝试将redis用作计数器时,我经常在值中得到':'(冒号),有时却没有,是因为我循环太快而redis甚至没有注意到吗?

输出结果

cclilshy@192 debug % php st.php
registerRedis success!
registerSocket success!
1
2
3
1
2
string(2) ":5"
1
2
3
string(2) ":9"
3
string(1) "9"

// the up is the output. Why?

代码


$func = function($handle){
    for($i=0;$i<3;$i++){
        echo $handle->counter().PHP_EOL;
    }

    var_dump($handle->total());
};

//$handle->counter() :
public function counter($record = true){
        if($record = false){
            return $this->count;
        }
        $this->thread->counter();
        $this->count++;
        return $this->count;
}

//$handle->total() :
public function total(){
        return $this->thread->counter(false);
}

//$handle->thread->counter() : 
public function counter($record = true){
        if($record === false){
            return $this->redis->get('thread.' . $this->pids[0] . '.count');
        }
        return $this->redis->incr('thread.' . $this->pids[0] . '.count');
}

【问题讨论】:

    标签: php redis


    【解决方案1】:

    Redis 在设计上是单线程的,因此无论如何它都会以同步方式为您的线程提供服务。

    响应看起来像这样,因为 RESP 协议未被解析,它返回整数的原始表示。

    是的,原因之一可能是在您已经返回下一个值时解析器进程仍然被阻塞

    【讨论】:

      【解决方案2】:

      我通过为每个进程使用单独的 redis 连接解决了这个问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-03
        • 2014-01-31
        • 1970-01-01
        • 2019-03-27
        • 2017-04-05
        • 2011-08-13
        • 1970-01-01
        • 2018-04-24
        相关资源
        最近更新 更多