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 Qt下使用tcp获取数据的详细操作
    • ¥15 idea右下角设置编码是灰色的
    • ¥15 全志H618ROM新增分区
    • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
    • ¥15 NAO机器人的录音程序保存问题
    • ¥15 C#读写EXCEL文件,不同编译
    • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
    • ¥15 扩散模型sd.webui使用时报错“Nonetype”
    • ¥15 stm32流水灯+呼吸灯+外部中断按键
    • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符