phpredis recommends of to improve the real time performance of message queues
- 2020-05-15 02:30:33
- OfStack
The database stores are all in list form and you have to store two queues one for the message queue to store the data and one for the real-time reading of the data at redis
$redis->lpush($queenkey, json_encode($array));
$redis->lpush($listkey, json_encode($array));
/* message queue instance */
public function insertinfo()
{
$infos = array('info1' => mt_rand(10,100), 'info2' => mt_rand(10,100));
$this->insertinfos($infos, 'tutorial-list','tutoriallist');
}
public function insertinfos($array, $queenkey,$listkey)
{
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Store data in a list
$redis->lpush($queenkey, json_encode($array));
$redis->lpush($listkey, json_encode($array));
}
Read logic when redis key is gone read the database and then re-write list to read redis data if it is available
/* Read the instance */
public function getinfo()
{
$sql = 'select * from mobantestinfo';
$result = $this->getinfos('tutoriallist', $sql);
//redis key Don't empty Directly read redis
if(empty($result)){
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Get the stored data and output it
$result = $redis->lrange('tutoriallist', 0, -1);
foreach ($result as $k=>$v){
$result[$k]=json_decode($v,true);
}
print_r($result);
exit();
}
}
function getinfos($key, $sql)
{
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Get the stored data
$result = $redis->lrange($key, 0, 1);
if (empty($result)) {
$VModel = new HuanShanVoteModel();
$result = $VModel->query($sql);
// Re-place the form of the cache queue into the database
foreach ($result as $k=>$v){
// This is going to be inserted from the right To ensure that you follow the database in order 1 sample
$redis->rpush($key,json_encode($v));
}
} else {
$result=0;
}
return $result;
}