聚水潭订单标签明细筛选选项改为缓存并预热

233 万行上做 distinct 扫描要几秒;新增 JstOrderLabelOptions(按公司缓存 24 小时),页面读取缓存,导入命令结束时失效并预热,避免每次打开页面都等待。
This commit is contained in:
qiuxiang 2026-09-30 17:12:27 +08:00
parent 5ee0813490
commit 77ac3c58fa
3 changed files with 78 additions and 16 deletions

View File

@ -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,

View File

@ -0,0 +1,72 @@
<?php
namespace App\Business;
use App\Models\Business\JstOrderLabel;
use Illuminate\Support\Facades\Cache;
/**
* 聚水潭订单标签明细的筛选下拉选项。
*
* 表里有 200 万行以上,distinct 扫描一次要几秒;这里按公司缓存 24 小时,
* 导入命令结束时会主动预热,避免每次打开页面都慢。
*/
class JstOrderLabelOptions
{
private const TTL_HOURS = 24;
private const LIMIT = 500;
/** @return list<string> */
public function labels(int $companyId): array
{
return $this->remember($companyId, 'label');
}
/** @return list<string> */
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<string> */
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}";
}
}

View File

@ -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<string> */
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
{