From 77ac3c58fa5f5eafdcace5ac2be218bf6bc418a1 Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Wed, 30 Sep 2026 17:12:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=9A=E6=B0=B4=E6=BD=AD=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E6=98=8E=E7=BB=86=E7=AD=9B=E9=80=89=E9=80=89?= =?UTF-8?q?=E9=A1=B9=E6=94=B9=E4=B8=BA=E7=BC=93=E5=AD=98=E5=B9=B6=E9=A2=84?= =?UTF-8?q?=E7=83=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 233 万行上做 distinct 扫描要几秒;新增 JstOrderLabelOptions(按公司缓存 24 小时),页面读取缓存,导入命令结束时失效并预热,避免每次打开页面都等待。 --- app/Business/JstOrderLabelImporter.php | 4 ++ app/Business/JstOrderLabelOptions.php | 72 +++++++++++++++++++++++++ app/Http/Controllers/Business/Pages.php | 18 +------ 3 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 app/Business/JstOrderLabelOptions.php diff --git a/app/Business/JstOrderLabelImporter.php b/app/Business/JstOrderLabelImporter.php index 7d5471a8..7f4f5381 100644 --- a/app/Business/JstOrderLabelImporter.php +++ b/app/Business/JstOrderLabelImporter.php @@ -112,6 +112,10 @@ class JstOrderLabelImporter ->distinct() ->count('label'); + // 导入会改变筛选选项,重新预热缓存,避免页面下次打开时做几秒的 distinct 扫描。 + app(JstOrderLabelOptions::class)->forget($companyId); + app(JstOrderLabelOptions::class)->warm($companyId); + return [ 'from' => $from, 'to' => $to, diff --git a/app/Business/JstOrderLabelOptions.php b/app/Business/JstOrderLabelOptions.php new file mode 100644 index 00000000..18dbbfea --- /dev/null +++ b/app/Business/JstOrderLabelOptions.php @@ -0,0 +1,72 @@ + */ + public function labels(int $companyId): array + { + return $this->remember($companyId, 'label'); + } + + /** @return list */ + public function shops(int $companyId): array + { + return $this->remember($companyId, 'shop_name'); + } + + public function warm(int $companyId): void + { + $this->labels($companyId); + $this->shops($companyId); + } + + public function forget(int $companyId): void + { + Cache::forget($this->key($companyId, 'label')); + Cache::forget($this->key($companyId, 'shop_name')); + } + + /** @return list */ + private function remember(int $companyId, string $column): array + { + $key = $this->key($companyId, $column); + $cached = Cache::get($key); + if (is_array($cached)) { + return $cached; + } + + $values = JstOrderLabel::query() + ->where('company_id', $companyId) + ->whereNotNull($column) + ->where($column, '<>', '') + ->distinct() + ->orderBy($column) + ->limit(self::LIMIT) + ->pluck($column) + ->all(); + + Cache::put($key, $values, now()->addHours(self::TTL_HOURS)); + + return $values; + } + + private function key(int $companyId, string $column): string + { + return "jst-order-labels:options:v1:{$companyId}:{$column}"; + } +} diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index be7ea85c..d5d08aa1 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -1336,8 +1336,8 @@ class Pages extends Controller 'filters' => $filters, 'totalRows' => $records->total(), 'filterOptions' => [ - 'shops' => $this->jstOrderLabelOptions('shop_name', 500), - 'labels' => $this->jstOrderLabelOptions('label', 500), + 'shops' => app(\App\Business\JstOrderLabelOptions::class)->shops((int) company_id()), + 'labels' => app(\App\Business\JstOrderLabelOptions::class)->labels((int) company_id()), ], ]; } @@ -1378,20 +1378,6 @@ class Pages extends Controller return $query; } - /** @return list */ - private function jstOrderLabelOptions(string $column, int $limit): array - { - return \App\Models\Business\JstOrderLabel::query() - ->where('company_id', company_id()) - ->whereNotNull($column) - ->where($column, '<>', '') - ->distinct() - ->orderBy($column) - ->limit($limit) - ->pluck($column) - ->all(); - } - /** 账单流水数据 › 聚水潭订单标签明细导出(按当前筛选,全量) */ public function exportJstOrderLabels(Request $request): StreamedResponse {