Files
coolcoth.com/app/Models/Product.php
T
Lucanlee 4d36a284ff perf: V0.9.1-0.9.3 性能优化(count/featured 全表→聚合、请求内缓存、二级索引、PSI 未读铃铛)
- V0.9.1 (P0): Model::count() 改 SELECT COUNT(*);新增请求内 identity-map 缓存;
  Product::featured() 改 WHERE+ LIMIT 避免全表
- V0.9.2 (P1): schema.sql 内联二级索引 + install/upgrades/009_perf_indexes.sql 幂等迁移
- V0.9.3 (P1): psi_unread_events() 改 COUNT + JSON_CONTAINS,消除每页 all()+循环
2026-08-08 19:47:35 +08:00

36 lines
1016 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use Core\Model;
class Product extends Model
{
protected $table = 'products';
protected $orderBy = 'sort_order';
public function byCategory(int $catId): array
{
return $this->whereAll('category_id', $catId);
}
public function featured(int $limit = 6): array
{
// V0.9.1:改用 whereLimit('status',1,$limit)MySQL 下走带 LIMIT 的索引查询,
// 不再 SELECT * 全表拉回再 array_filter(原写法每次请求都全表扫描)。
return $this->whereLimit('status', 1, $limit);
}
public function specsArray($p): array
{
$s = $p['specs'] ?? '';
if (is_array($s)) return $s;
$dec = json_decode((string)$s, true);
return is_array($dec) ? $dec : [];
}
public function galleryArray($p): array
{
$g = $p['gallery'] ?? '';
if (is_array($g)) return $g;
$dec = json_decode((string)$g, true);
return is_array($dec) ? $g : [];
}
}