Parcourir la source

优化长连接数据更新逻辑

lh il y a 4 jours
Parent
commit
46909a6a9f

+ 24 - 17
app/Http/Controllers/DeepSeek/DeepSeekController.php

@@ -132,28 +132,33 @@ class DeepSeekController extends BaseController
             ]);
         }
 
-        // 计数器
-        $counter = 0;
-        $tmp_url_count = 0;
-
+        $redis_key = "select-{$bid}-{$version_id}-{$cid}";
         // 主循环 - 定期发送数据
         while (true) {
             // 检查客户端是否断开连接
             if (connection_aborted()) {
                 exit();
             }
-            
-            $counter++;
-            if ($counter % 10 == 0) {
-                // 每10s判断是否有新url生成,如果有则发送消息
-                $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
-                ->where('cid', $cid)->where(function($query) {
+
+            // 判断是否有待更新数据,有则查询
+            $ids = Redis::smembers($redis_key);
+            if (count($ids) > 0) {
+                $update_ids = DB::table('mp_chapter_paragraph_audios')->whereIn('id', $ids)->where(function($query) {
                     return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
-                })->select('sequence', 'paragraph_audio_url', 'error_msg')
-                ->get()->map(function ($value) {
-                    return (array)$value;
-                })->toArray();
-                if (count($paragraph_urls) != $tmp_url_count) {
+                })->pluck('id')->toArray();
+
+                // 如果有更新数据,则发送消息
+                if (count($update_ids) > 0) {
+                    // 查询更新后的信息
+                    $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
+                    ->where('cid', $cid)->where(function($query) {
+                        return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
+                    })->select('sequence', 'paragraph_audio_url', 'error_msg')
+                    ->get()->map(function ($value) {
+                        return (array)$value;
+                    })->toArray();
+                    
+                    // 发送消息
                     $this->sendEvent([
                         'code'  => 0,
                         'msg'   => "",
@@ -162,11 +167,13 @@ class DeepSeekController extends BaseController
                         // 'type' => 'update',
                         // 'timestamp' => date('H:i:s')
                     ]);
-                    $tmp_url_count = count($paragraph_urls);
+
+                    // 删除已发送的id
+                    Redis::srem($redis_key, $update_ids);
                 }
             }
             
-            // 等待1秒后发送下一条消息
+            // 等待1秒
             sleep(1);
         }
     }

+ 6 - 0
app/Services/DeepSeek/DeepSeekService.php

@@ -167,6 +167,12 @@ class DeepSeekService
             $boolen = $id ? true : false;
         }
 
+        // 如果更新成功则加入查询队列
+        if ($boolen) {
+            $redis_key = "select-{$bid}-{$version_id}-{$cid}";
+            Redis::sadd($redis_key, $id);
+        }
+
         if ($boolen && !$continue) {
             $boolen = false;
             $client = new Client(['timeout' => 300, 'verify' => false]);