From ac16a06d7fb5b410242561292e8139ad2cea18ab Mon Sep 17 00:00:00 2001 From: yin5th <541304803@qq.com> Date: Mon, 23 Oct 2023 17:32:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E5=8A=A0=E7=BC=93=E5=86=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/logic/GoodsLogic.php | 33 +++++++++++++++++++++++------ server/app/api/logic/IndexLogic.php | 26 +++++++---------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/server/app/api/logic/GoodsLogic.php b/server/app/api/logic/GoodsLogic.php index ef73d955..709e7922 100644 --- a/server/app/api/logic/GoodsLogic.php +++ b/server/app/api/logic/GoodsLogic.php @@ -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 = [ diff --git a/server/app/api/logic/IndexLogic.php b/server/app/api/logic/IndexLogic.php index a8502980..924d1cca 100644 --- a/server/app/api/logic/IndexLogic.php +++ b/server/app/api/logic/IndexLogic.php @@ -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; }