Commit fae5729e authored by 刘小敏's avatar 刘小敏

商品分类接口加缓存优化

parent 2b2b18fd
......@@ -2,7 +2,9 @@
namespace addons\shopro\controller;
use addons\shopro\service\goods\GoodsService;
use app\admin\model\shopro\Category as CategoryModel;
use app\admin\model\shopro\goods\Goods as GoodsModel;
use think\Cache;
class Category extends Common
......@@ -11,8 +13,25 @@ class Category extends Common
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
// 缓存键名
const CACHE_INDEX_KEY = 'shopro_category_index';
// 缓存键名
const CACHE_LIST_KEY = 'shopro_category_tree';
// 缓存有效期(1小时)
const CACHE_TTL = 3600;
public function index()
{
// 如果不需要刷新缓存,先尝试从缓存获取
$refresh = $this->request->param('refresh', 0);
if (!$refresh) {
$cacheData = Cache::get(self::CACHE_INDEX_KEY);
if ($cacheData) {
$this->success('分类详情', $cacheData);
}
}
$id = $this->request->param('id', 0);
$category = CategoryModel::where('parent_id', 0)->normal()->order('weigh', 'desc')->order('id', 'desc');
if ($id) {
......@@ -27,13 +46,13 @@ class Category extends Common
$childrenString = $category->getChildrenString($category);
$categories = CategoryModel::where('id', $category->id)->normal()->with([$childrenString])->find();
// 将结果存入缓存
Cache::set(self::CACHE_INDEX_KEY, $categories, self::CACHE_TTL);
$this->success('商城分类', $categories);
}
// 缓存键名
const CACHE_KEY = 'shopro_category_tree';
// 缓存有效期(24小时)
const CACHE_TTL = 86400;
/**
* 获取完整分类树(包含二级、三级、四级分类)
......@@ -47,7 +66,7 @@ class Category extends Common
// 如果不需要刷新缓存,先尝试从缓存获取
if (!$refresh) {
$cacheData = Cache::get(self::CACHE_KEY);
$cacheData = Cache::get(self::CACHE_LIST_KEY);
if ($cacheData) {
$this->success('分类列表', $cacheData);
}
......@@ -67,7 +86,7 @@ class Category extends Common
unset($category);
// 将结果存入缓存
Cache::set(self::CACHE_KEY, $categories, self::CACHE_TTL);
Cache::set(self::CACHE_LIST_KEY, $categories, self::CACHE_TTL);
$this->success('分类列表', $categories);
}
......
......@@ -102,6 +102,7 @@ class Order
// 这里会自动检测订单支付状态
$order = $payOper->score($order, $order->deduct_score);
// TODO 下单成功,增加用户积分,支付金额与积分1:1增加
}
// 删除购物车
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment