Subscribing and reading data from redis channels using php-redis simultaneously -
i have subscribe channels of redis db , simultaneously read data hash in same db node. following code have written using phpredis:
$notif = new worker; try { // connect redis $notif->connectredisserver(); // start 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 getting recent record of every imei const recentdata_db = 1; public function connectredisserver() { if(!empty($this->redis)) return $this->redis; // 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 data if exists $tid if($data === false) return null; $data = json_decode($data, true); return $data; // comes null } } the problem here once subscribed channels on db1 in redis. don't result if try run hget, though data given key exists in db. have put additional comments in code above explain problem is. check getnotifsettings() function.
any appreciated.
Comments
Post a Comment