Authored by wuxiao

视频详情观看人数记录在redis中

... ... @@ -287,7 +287,7 @@ class LivingController extends BaseController
{//重播视频详情
//观看人数+1
Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]);
$incr = Video::updateAllCounters(['replay_num' => 1],['id'=>$video_id]);
$key = __CLASS__.__FUNCTION__.'video_id'.$video_id;
if (!$ret = $this->cache->get($key)){
... ... @@ -335,19 +335,20 @@ class LivingController extends BaseController
->getRawSql()]);
$this->cache->set($key, json_encode($ret), Yii::$app->params['defaultCacheExpire'], $dependency);
}else{
$model = Yii::$app->db->cache(function() use ($video_id){
return Video::findOne(['id'=>$video_id]);
},60);
if ($model){
$_ret = json_decode($ret,true);
$ret = array_merge($_ret,[
// 'like_num'=>(int)$model->like_num,
'audience_num'=>(int)($model->audience_num + $model->replay_num),
]);
$_ret = json_decode($ret,true);
$key = __CLASS__.__FUNCTION__.'video_id'.$video_id.'audience_num';
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);
}
$_ret['audience_num'] = $audience_num;
if ($incr && Yii::$app->redis->incr($key)){
$_ret['audience_num'] += 1;
}
$ret = $_ret;
}
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
}else
{
... ...