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' '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') $columnData = cache('GoodsListByColumnId:'.$columnId);
->where($onSaleWhere) if (!empty($columnData)) {
->whereFindInSet('column_ids', $columnId) $count = $columnData['count'] ?? 0;
->order($order) $list = $columnData['list'] ?? [];
->page($page_no, $page_size) $more = is_more($count, $page_no, $page_size);
->select();
return [
'lists' => $list,
'page_no' => $page_no,
'page_size' => $page_size,
'count' => $count,
'more' => $more
];
}
$count = Goods::where($onSaleWhere) $count = Goods::where($onSaleWhere)
->whereFindInSet('column_ids', $columnId) ->whereFindInSet('column_ids', $columnId)
->count(); ->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() : []; $list = $list ? $list->toArray() : [];
// 写入缓存
cache('GoodsListByColumnId:'.$columnId, ['list' => $list, 'count' => $count], 600);
$more = is_more($count, $page_no, $page_size); $more = is_more($count, $page_no, $page_size);
$data = [ $data = [

View File

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