Authored by wuxiao

回看人数bug修复,前台redis暂存,后台守护进程同步

<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
/**
* Site controller
*/
class TestController extends Controller
{
/**
* TEST
*/
public function actionIndex(){
$this->stdout("this is console mode\n");
return 0;
}
}
... ...
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
use common\models\Video;
/**
* Site controller
*/
class VideoController extends Controller
{
/**
* 同步回放观看人数到数据库
*/
public function actionReplaynum(){
while(true){
//获取前台展示的视频
$result = Video::find()
->where(['status'=>1])
->orderBy(['update_time'=>SORT_DESC])
->all();
$this->stdout("[".date('Y-m-d H:i:s')."]New loop, check for ".count($result)." videos\n");
if($result){
$keys = [];
foreach($result as $model){
$keys[$model->id] = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$model->id);
}
$values = $keys ? call_user_func_array(array(Yii::$app->redis,'mget'), $keys) : [];
//var_dump($keys,$values);exit;
foreach($result as $k=>$model){
if ($replay_num = $values[$k]){
$video_id = $model->id;
$this->stdout("\tupdate video ".$video_id." replay_num to ".$replay_num.":\n");
if (Video::updateAll(['replay_num' => $replay_num],['id'=>$video_id])){
$this->stdout("\tsuccess\n");
Yii::$app->redis->del($keys[$video_id]);
}else{
$this->stderr("\tfail\n");
}
}
}
unset($keys,$values);
}
unset($result);
$this->stdout("Loop end\n");
sleep(300);
}
}
}
... ...
... ... @@ -8,4 +8,10 @@ stillRunning=$(ps -ef |grep "room/stat" |grep -v "grep")
if [ -z "$stillRunning" ] ; then
nohup $phpcmd $phpscriptDir/yii room/stat >> $phpscriptlog/yoholive_room_stat.log 2>&1 &
fi
stillRunning=$(ps -ef |grep "video/replaynum" |grep -v "grep")
if [ -z "$stillRunning" ] ; then
nohup $phpcmd $phpscriptDir/yii video/replaynum >> $phpscriptlog/yoholive_video_replaynum.log 2>&1 &
fi
sleep 5
\ No newline at end of file
... ...
... ... @@ -175,7 +175,8 @@ class LivingController extends BaseController
'url'=>$model->url,
'live_title'=>$model->live_title,
'live_room_id'=>(int)$model->room_id,
'audience_num'=>(int)($model->audience_num+$model->replay_num),
'audience_num'=>(int)$model->audience_num,
'replay_num'=>(int)$model->replay_num,
'create_time'=>(int)$model->create_time,
];
$ret[$k] = $row;
... ... @@ -186,15 +187,17 @@ class LivingController extends BaseController
->bindValue(':app', $this->app)
->getRawSql()]);
$this->cache->set($key, $ret, Yii::$app->params['defaultCacheExpire'], $dependency);
}else{
}
$keys = [];
foreach ($ret as $k=>$row){
$keys[] = sprintf("%svideo_id_%s_audience_num",Yii::$app->params['cache_prefix'],$row['video_id']);
$keys[] = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$row['video_id']);
}
$values = $keys ? call_user_func_array(array(Yii::$app->redis,'mget'), $keys) : [];
foreach ($values as $k=>$value){
$ret[$k]['audience_num'] = (int)($value ? : $ret[$k]['audience_num']);
}
foreach ($ret as $k=>$row){
$replay_num = (int)(@$values[$k] ? : $ret[$k]['replay_num']);
$ret[$k]['audience_num'] += $replay_num;
$ret[$k]['replay_num'] = $replay_num;
}
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
... ... @@ -307,7 +310,8 @@ class LivingController extends BaseController
'watermark'=>'',
'url'=>$model->url,
'like_num'=>(int)$model->like_num,
'audience_num'=>(int)($model->audience_num+$model->replay_num),
'audience_num'=>(int)$model->audience_num,
'replay_num'=>(int)$model->replay_num,
'live_title'=>$model->live_title,
'live_start_time'=>(int)$model->live_start_time,
'live_end_time'=>(int)$model->live_end_time,
... ... @@ -340,17 +344,23 @@ class LivingController extends BaseController
}
//缓存回放观看人数
$key = sprintf("%svideo_id_%s_audience_num",Yii::$app->params['cache_prefix'],$video_id);
if (!$audience_num = Yii::$app->redis->get($key)){
$audience_num = (int)$ret['audience_num'];
Yii::$app->redis->setex($key, Yii::$app->params['defaultCacheExpire']*2, $audience_num);
$key = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$video_id);
if (!$replay_num = (int)Yii::$app->redis->get($key)){
$replay_num = (int)$ret['replay_num'];
Yii::$app->redis->set($key, $replay_num);
}
$ret['audience_num'] = $audience_num;
//$ret['audience_num'] = $audience_num;
//观看回放人数+1
if (Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]) && Yii::$app->redis->incr($key)){
/*if (Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]) && Yii::$app->redis->incr($key)){
$ret['audience_num'] += 1;
}*/
if (Yii::$app->redis->incr($key)){
$replay_num += 1;
}
$ret['audience_num'] += $replay_num;
$ret['replay_num'] = $replay_num;
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
}else
... ...