feat: 首页接口优化 加缓冲

master
yin5th 2023-10-23 17:32:34 +08:00
parent fb3019f1c4
commit ac16a06d7f
2 changed files with 35 additions and 24 deletions

View File

@ -487,19 +487,40 @@ class GoodsLogic extends Logic
'id' => 'desc'
];
$list = Goods::field('id,name,image,market_price,min_price,sales_actual,column_ids,sort_weight,sales_virtual,(sales_actual + sales_virtual) as sales_total')
->where($onSaleWhere)
->whereFindInSet('column_ids', $columnId)
->order($order)
->page($page_no, $page_size)
->select();
$columnData = cache('GoodsListByColumnId:'.$columnId);
if (!empty($columnData)) {
$count = $columnData['count'] ?? 0;
$list = $columnData['list'] ?? [];
$more = is_more($count, $page_no, $page_size);
return [
'lists' => $list,
'page_no' => $page_no,
'page_size' => $page_size,
'count' => $count,
'more' => $more
];
}
$count = Goods::where($onSaleWhere)
->whereFindInSet('column_ids', $columnId)
->count();
if ($count > 0) {
$list = Goods::field('id,name,image,market_price,min_price,sales_actual,column_ids,sort_weight,sales_virtual,(sales_actual + sales_virtual) as sales_total')
->where($onSaleWhere)
->whereFindInSet('column_ids', $columnId)
->order($order)
->page($page_no, $page_size)
->select();
} else {
$list = [];
}
$list = $list ? $list->toArray() : [];
// 写入缓存
cache('GoodsListByColumnId:'.$columnId, ['list' => $list, 'count' => $count], 600);
$more = is_more($count, $page_no, $page_size);
$data = [

View File

@ -101,12 +101,9 @@ class IndexLogic extends Logic
*/
public static function getHots()
{
$hots = session('home_hosts');
$hots = cache('home_hosts');
if (!empty($hots)) {
// 有缓存且未过期
if (!empty($hots['expire']) && $hots['expire'] > time()) {
return $hots['list'];
}
return $hots;
}
// 销售中商品:未删除/审核通过/已上架
$onSaleWhere = [
@ -120,15 +117,13 @@ class IndexLogic extends Logic
'sort_weight' => 'asc', // 商品权重
'id' => 'desc'
];
$hots = [];
$hots['list'] = Goods::field('id,name,image,min_price,market_price,sales_actual,create_time,sales_virtual,(sales_actual + sales_virtual) as sales_total')
$hots = Goods::field('id,name,image,min_price,market_price,sales_actual,create_time,sales_virtual,(sales_actual + sales_virtual) as sales_total')
->where($onSaleWhere)
->order($order)
->limit(9)
->select()
->toArray();
$hots['expire'] = time() + 600;//10分钟过期
session('home_hosts', $hots);
cache('home_hosts', $hots, 600);
return $hots;
}
@ -138,12 +133,9 @@ class IndexLogic extends Logic
*/
public static function getNews()
{
$news = session('home_news');
$news = cache('home_news');
if (!empty($news)) {
// 有缓存且未过期
if (!empty($news['expire']) && $news['expire'] > time()) {
return $news['list'];
}
return $news;
}
// 销售中商品:未删除/审核通过/已上架
$onSaleWhere = [
@ -158,15 +150,13 @@ class IndexLogic extends Logic
'id' => 'desc'
];
$news = [];
$news['list'] = Goods::field('id,name,image,min_price,market_price,sales_actual,create_time,sales_virtual,(sales_actual + sales_virtual) as sales_total')
$news = Goods::field('id,name,image,min_price,market_price,sales_actual,create_time,sales_virtual,(sales_actual + sales_virtual) as sales_total')
->where($onSaleWhere)
->order($order)
->limit(3)
->select()
->toArray();
$news['expire'] = time() + 600;//10分钟过期
session('home_news', $news);
cache('home_news', $news, 600);
return $news;
}