Authored by wuxiao

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

  1 +<?php
  2 +namespace console\controllers;
  3 +
  4 +use Yii;
  5 +use yii\console\Controller;
  6 +
  7 +/**
  8 + * Site controller
  9 + */
  10 +class TestController extends Controller
  11 +{
  12 +
  13 + /**
  14 + * TEST
  15 + */
  16 + public function actionIndex(){
  17 +
  18 + $this->stdout("this is console mode\n");
  19 +
  20 + return 0;
  21 +
  22 + }
  23 +}
  1 +<?php
  2 +namespace console\controllers;
  3 +
  4 +use Yii;
  5 +use yii\console\Controller;
  6 +use common\models\Video;
  7 +
  8 +/**
  9 + * Site controller
  10 + */
  11 +class VideoController extends Controller
  12 +{
  13 +
  14 + /**
  15 + * 同步回放观看人数到数据库
  16 + */
  17 + public function actionReplaynum(){
  18 + while(true){
  19 +
  20 + //获取前台展示的视频
  21 + $result = Video::find()
  22 + ->where(['status'=>1])
  23 + ->orderBy(['update_time'=>SORT_DESC])
  24 + ->all();
  25 + $this->stdout("[".date('Y-m-d H:i:s')."]New loop, check for ".count($result)." videos\n");
  26 +
  27 + if($result){
  28 + $keys = [];
  29 + foreach($result as $model){
  30 + $keys[$model->id] = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$model->id);
  31 + }
  32 + $values = $keys ? call_user_func_array(array(Yii::$app->redis,'mget'), $keys) : [];
  33 + //var_dump($keys,$values);exit;
  34 + foreach($result as $k=>$model){
  35 + if ($replay_num = $values[$k]){
  36 + $video_id = $model->id;
  37 + $this->stdout("\tupdate video ".$video_id." replay_num to ".$replay_num.":\n");
  38 + if (Video::updateAll(['replay_num' => $replay_num],['id'=>$video_id])){
  39 + $this->stdout("\tsuccess\n");
  40 + Yii::$app->redis->del($keys[$video_id]);
  41 + }else{
  42 + $this->stderr("\tfail\n");
  43 + }
  44 + }
  45 + }
  46 +
  47 + unset($keys,$values);
  48 + }
  49 + unset($result);
  50 + $this->stdout("Loop end\n");
  51 +
  52 + sleep(300);
  53 + }
  54 +
  55 + }
  56 +}
@@ -8,4 +8,10 @@ stillRunning=$(ps -ef |grep "room/stat" |grep -v "grep") @@ -8,4 +8,10 @@ stillRunning=$(ps -ef |grep "room/stat" |grep -v "grep")
8 if [ -z "$stillRunning" ] ; then 8 if [ -z "$stillRunning" ] ; then
9 nohup $phpcmd $phpscriptDir/yii room/stat >> $phpscriptlog/yoholive_room_stat.log 2>&1 & 9 nohup $phpcmd $phpscriptDir/yii room/stat >> $phpscriptlog/yoholive_room_stat.log 2>&1 &
10 fi 10 fi
  11 +
  12 +stillRunning=$(ps -ef |grep "video/replaynum" |grep -v "grep")
  13 +if [ -z "$stillRunning" ] ; then
  14 + nohup $phpcmd $phpscriptDir/yii video/replaynum >> $phpscriptlog/yoholive_video_replaynum.log 2>&1 &
  15 +fi
  16 +
11 sleep 5 17 sleep 5
@@ -175,7 +175,8 @@ class LivingController extends BaseController @@ -175,7 +175,8 @@ class LivingController extends BaseController
175 'url'=>$model->url, 175 'url'=>$model->url,
176 'live_title'=>$model->live_title, 176 'live_title'=>$model->live_title,
177 'live_room_id'=>(int)$model->room_id, 177 'live_room_id'=>(int)$model->room_id,
178 - 'audience_num'=>(int)($model->audience_num+$model->replay_num), 178 + 'audience_num'=>(int)$model->audience_num,
  179 + 'replay_num'=>(int)$model->replay_num,
179 'create_time'=>(int)$model->create_time, 180 'create_time'=>(int)$model->create_time,
180 ]; 181 ];
181 $ret[$k] = $row; 182 $ret[$k] = $row;
@@ -186,15 +187,17 @@ class LivingController extends BaseController @@ -186,15 +187,17 @@ class LivingController extends BaseController
186 ->bindValue(':app', $this->app) 187 ->bindValue(':app', $this->app)
187 ->getRawSql()]); 188 ->getRawSql()]);
188 $this->cache->set($key, $ret, Yii::$app->params['defaultCacheExpire'], $dependency); 189 $this->cache->set($key, $ret, Yii::$app->params['defaultCacheExpire'], $dependency);
189 - }else{  
190 - $keys = [];  
191 - foreach ($ret as $k=>$row){  
192 - $keys[] = sprintf("%svideo_id_%s_audience_num",Yii::$app->params['cache_prefix'],$row['video_id']);  
193 - }  
194 - $values = $keys ? call_user_func_array(array(Yii::$app->redis,'mget'), $keys) : [];  
195 - foreach ($values as $k=>$value){  
196 - $ret[$k]['audience_num'] = (int)($value ? : $ret[$k]['audience_num']);  
197 - } 190 + }
  191 +
  192 + $keys = [];
  193 + foreach ($ret as $k=>$row){
  194 + $keys[] = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$row['video_id']);
  195 + }
  196 + $values = $keys ? call_user_func_array(array(Yii::$app->redis,'mget'), $keys) : [];
  197 + foreach ($ret as $k=>$row){
  198 + $replay_num = (int)(@$values[$k] ? : $ret[$k]['replay_num']);
  199 + $ret[$k]['audience_num'] += $replay_num;
  200 + $ret[$k]['replay_num'] = $replay_num;
198 } 201 }
199 202
200 $this->renderJson(Yii::$app->params['success_code'],'',$ret); 203 $this->renderJson(Yii::$app->params['success_code'],'',$ret);
@@ -307,7 +310,8 @@ class LivingController extends BaseController @@ -307,7 +310,8 @@ class LivingController extends BaseController
307 'watermark'=>'', 310 'watermark'=>'',
308 'url'=>$model->url, 311 'url'=>$model->url,
309 'like_num'=>(int)$model->like_num, 312 'like_num'=>(int)$model->like_num,
310 - 'audience_num'=>(int)($model->audience_num+$model->replay_num), 313 + 'audience_num'=>(int)$model->audience_num,
  314 + 'replay_num'=>(int)$model->replay_num,
311 'live_title'=>$model->live_title, 315 'live_title'=>$model->live_title,
312 'live_start_time'=>(int)$model->live_start_time, 316 'live_start_time'=>(int)$model->live_start_time,
313 'live_end_time'=>(int)$model->live_end_time, 317 'live_end_time'=>(int)$model->live_end_time,
@@ -340,17 +344,23 @@ class LivingController extends BaseController @@ -340,17 +344,23 @@ class LivingController extends BaseController
340 } 344 }
341 345
342 //缓存回放观看人数 346 //缓存回放观看人数
343 - $key = sprintf("%svideo_id_%s_audience_num",Yii::$app->params['cache_prefix'],$video_id);  
344 - if (!$audience_num = Yii::$app->redis->get($key)){  
345 - $audience_num = (int)$ret['audience_num'];  
346 - Yii::$app->redis->setex($key, Yii::$app->params['defaultCacheExpire']*2, $audience_num); 347 + $key = sprintf("%svideo_id_%s_replay_num",Yii::$app->params['cache_prefix'],$video_id);
  348 + if (!$replay_num = (int)Yii::$app->redis->get($key)){
  349 + $replay_num = (int)$ret['replay_num'];
  350 + Yii::$app->redis->set($key, $replay_num);
347 } 351 }
348 - $ret['audience_num'] = $audience_num; 352 + //$ret['audience_num'] = $audience_num;
349 353
350 //观看回放人数+1 354 //观看回放人数+1
351 - if (Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]) && Yii::$app->redis->incr($key)){ 355 + /*if (Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]) && Yii::$app->redis->incr($key)){
352 $ret['audience_num'] += 1; 356 $ret['audience_num'] += 1;
  357 + }*/
  358 +
  359 + if (Yii::$app->redis->incr($key)){
  360 + $replay_num += 1;
353 } 361 }
  362 + $ret['audience_num'] += $replay_num;
  363 + $ret['replay_num'] = $replay_num;
354 364
355 $this->renderJson(Yii::$app->params['success_code'],'',$ret); 365 $this->renderJson(Yii::$app->params['success_code'],'',$ret);
356 }else 366 }else