dqxyh48864 2016-02-06 13:58
浏览 83

同时使用php-redis从redis通道订阅和读取数据

I have to subscribe to all the channels of my Redis db and simultaneously read data from another hash in the same db node. The following is the code I have written for this using phpredis:

$notif = new Worker;

try {
    // connect redis
    $notif->connectRedisServer();

    // start the worker processing
    $notif->startWorkerProcess();
}
catch(Exception $e) {
    exit('Exception: '.$e->getMessage());
}

class Worker {

    private $redis;

    public $recentHash         = 'tracker.data.recent';
    public $notifHash          = 'tracker.settings.data';

    public $serverConnectionDetails = { //Redis server connection details
                                    };

    private $redis;

    // redis database used for getting recent record of every imei
    const RECENTDATA_DB = 1;

    public function connectRedisServer() {
        if(!empty($this->redis))
            return $this->redis; // already connected

        $this->redis = new Redis();

        if(!$this->redis->pconnect(
            $this->serverConnectionDetails['redis']['host'],
            $this->serverConnectionDetails['redis']['port']
        ))
            return 'Redis connection failed.';

        if(isset($this->serverConnectionDetails['redis']['password'])
            &&
           !$this->redis->auth($this->serverConnectionDetails['redis']['password'])
    )
            return 'Redis authentication failed.';

        return $this->redis;
    }

    public function startWorkerProcess() {                      
        $this->redis->select(self::RECENTDATA_DB);

        $this->redis->pSubscribe(array('*'), array($this, 'processRedisData'));
    }

    public function processRedisData($redis, $pattern, $chan, $msg) {
        $message     = rtrim((string) $msg,",");

        $tData       = json_decode($message, true);

        $tId         = (int) $tData['tID'];

        echo "Recent Published Data:";
        var_dump($tData);

        $data = $this->getNotifSettings($tId);

        if(!empty($data)) {
            echo "Redis Settings: ";
            var_dump($trackerSettings);
        }       
   }

    public function getNotifSettings($tId) {
        $data = $this->redis->hGet($this->notifHash, $tId); //This command doesn't return any data even if it exists for the $tId

        if($data === false)
            return null;

        $data = json_decode($data, true);

        return $data; // Always comes up as null
    }
}

The problem here is that once I get subscribed to all the channels on db1 in Redis. I don't get any result if I try to run HGET, even though the data for the given key exists in the db. I have put additional comments in the code above to explain where the problem is. Check getNotifSettings() function.

Any help will be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog