gpt4 book ai didi

php - 需要有关 BLPOP Predis PHP 的支持

转载 作者:IT王子 更新时间:2023-10-29 06:14:16 24 4
gpt4 key购买 nike

我正在使用 BLPOP 构建聊天消息系统长轮询。我使用 Predis PHP。当我运行 test/get -> 它以 30 秒超时运行 okie。

在运行 test/get 时,我尝试通过 test/push 推送数据,但它有问题。推送不会立即执行,除非 test/get 完成。 test/push 需要 30 秒。

我使用命令行推送数据:RPUSH message:test hello -> 它立即执行,非常好。

require './vendor/autoload.php';

class Test extends CI_Controller {
public $keyChat = 'message:test';
public function __construct() {
parent::__construct();
}

public function push() {
$redis = new Predis\Client(['host' => 'localhost','port' => 6379]);
$redis->rpush($this->keyChat, 'hello you');
$redis->expire($this->keyChat, 3600);
echo "send message success";
}

public function get() {
$redis = new Predis\Client(['host' => '127.0.0.1','port' => 6379]);
$res = $redis->blpop($this->keyChat, 30);
var_dump($res);
}
}

最佳答案

如果你查看了BLPOP文档,你会发现这是一个阻塞操作,我想你正试图通过实例化另一个Redis客户端来推送,所以BLPOP可以找到值并返回它。

30 秒问题,是 BLPOP 解锁的超时时间,如下所示:

$redis->blpop($this->keyChat, 30);

当你从命令行推送它时它起作用的原因是它是一个不同的连接,我不确定 Predis 的情况,但我认为当你尝试 RPUSH 时返回相同的连接 ,这就是它给你这个问题的原因。

关于php - 需要有关 BLPOP Predis PHP 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36544216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com