wesmiler il y a 2 ans
Parent
commit
9d06a754f7
1 fichiers modifiés avec 29 ajouts et 8 suppressions
  1. 29 8
      app/Services/LiveService.php

+ 29 - 8
app/Services/LiveService.php

@@ -200,6 +200,7 @@ class LiveService extends BaseService
                 VideoCollectModel::insert($data);
                 RedisService::clear("caches:videos:recommend:{$userId}_2");
                 RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
+                RedisService::clear("caches:live:users_{$id}_{$userId}");
             }
 
             // 更新播放量
@@ -219,9 +220,26 @@ class LiveService extends BaseService
         return $info;
     }
 
-    public function getUserList($params, $pageSize, $userId=0)
+    /**
+     * 获取直播间在线观众数据
+     * @param $params
+     * @param $pageSize
+     * @param int $userId
+     * @return array|mixed
+     */
+    public function getUserList($params, $pageSize=200, $userId=0)
     {
         $liveId = isset($params['live_id'])? $params['live_id'] : 0;
+        $cachekey = "caches:live:users_{$liveId}_{$userId}";
+        $datas = RedisService::get($cachekey);
+        if($datas || RedisService::exists($cachekey)){
+            return $datas? $datas : [
+                'current' => ['user_id'=> $userId,'rank'=>0,'reward_total'=>0],
+                'rank_limit'=> 200,
+                'total'    => 0,
+                'list'     => []
+            ];
+        }
         $rankNum = ConfigService::make()->getConfigByCode('live_show_rank', 200);
         $rankNum = $rankNum>20 && $rankNum <= 300? $rankNum : 200;
         $model = MemberModel::from('member as a')
@@ -241,7 +259,7 @@ class LiveService extends BaseService
 
         $datas = $model->limit($rankNum)->get();
         $datas = $datas? $datas->toArray() : [];
-        $rankData = ['rank'=>0,'reward_total'=>0];
+        $rankData = ['user_id'=> $userId,'rank'=>0,'reward_total'=>0];
         if ($datas) {
             foreach ($datas as $k=> &$item) {
                 $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
@@ -253,13 +271,15 @@ class LiveService extends BaseService
                     $rankData['reward_total'] = $item['reward_total']? format_num($item['reward_total']) : 0;
                 }
             }
-        }
 
-        // 用户排名在200名外的数据
-        if($rankData['rank'] <= 0 && $rankData['reward_total']<=0 && count($datas) > $rankNum){
-            $rewardTotal = VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'collect_id'=> $liveId,'source_type'=>2,'status'=>1,'mark'=>1])
-                ->value('reward_total');
-            $rankData['reward_total'] = $rewardTotal? format_num($rewardTotal) : 0;
+            // 用户排名在200名外的数据
+            if($rankData['rank'] <= 0 && $rankData['reward_total']<=0 && count($datas) > $rankNum){
+                $rewardTotal = VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'collect_id'=> $liveId,'source_type'=>2,'status'=>1,'mark'=>1])
+                    ->value('reward_total');
+                $rankData['reward_total'] = $rewardTotal? format_num($rewardTotal) : 0;
+            }
+
+            RedisService::set($cachekey, ['current' => $rankData,'rank_limit'=> $rankNum,'total'=> $total,'list'=> $datas,], rand(3,5));
         }
 
         return [
@@ -303,6 +323,7 @@ class LiveService extends BaseService
             RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_2", $data, rand(10,30));
             RedisService::clear("caches:videos:recommend:{$userId}_1_2");
             RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
+            RedisService::clear("caches:live:users_{$id}_{$userId}");
         }
 
         // 更新播放量