From 031f66da2209ae74ebf2d965a17ec8f22eec7aae Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Thu, 8 Jun 2023 14:48:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=95=86=E5=93=81=E4=B8=8A?= =?UTF-8?q?=E6=9E=B6=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/model/store/product/CloudProduct.php | 2 ++ app/common/repositories/store/product/SpuRepository.php | 7 ++++--- app/listener/CloudProduct.php | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/common/model/store/product/CloudProduct.php b/app/common/model/store/product/CloudProduct.php index ec392fc4..2f4d7c0d 100644 --- a/app/common/model/store/product/CloudProduct.php +++ b/app/common/model/store/product/CloudProduct.php @@ -17,6 +17,8 @@ use app\common\model\BaseModel; class CloudProduct extends BaseModel { + const CacheKey = 'CloudMerchantSpu_%s'; + public static function tablePk(): string { return 'product_id'; diff --git a/app/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index 128e036b..2cc66f70 100644 --- a/app/common/repositories/store/product/SpuRepository.php +++ b/app/common/repositories/store/product/SpuRepository.php @@ -10,6 +10,7 @@ // +---------------------------------------------------------------------- namespace app\common\repositories\store\product; +use app\common\model\store\product\CloudProduct; use app\common\repositories\store\coupon\StoreCouponProductRepository; use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\StoreActivityRepository; @@ -179,7 +180,7 @@ class SpuRepository extends BaseRepository } if ($rand) { - $dataKey = 'CloudMerchantSpu_' . $where['mer_id']; + $dataKey = sprintf(CloudProduct::CacheKey, $where['mer_id']); $RedisCacheService = app()->make(RedisCacheService::class); $exists = $RedisCacheService->exists($dataKey); if (!$exists) { @@ -224,8 +225,8 @@ class SpuRepository extends BaseRepository */ public function getSpuFromCache($merId, $page): array { - $dataKey = 'CloudMerchantSpu_' . $merId; - $takenKey = 'CloudMerchantSpu_' . $merId . '_page_'. $page; + $dataKey = sprintf(CloudProduct::CacheKey, $merId); + $takenKey = $dataKey . '_page_'. $page; $remainKey = 'CloudMerchantSpuRemain_' . $merId; /** @var RedisCacheService $RedisCacheService */ $RedisCacheService = app()->make(RedisCacheService::class); diff --git a/app/listener/CloudProduct.php b/app/listener/CloudProduct.php index d666cb47..7dcdddd2 100644 --- a/app/listener/CloudProduct.php +++ b/app/listener/CloudProduct.php @@ -13,6 +13,9 @@ class CloudProduct public function handle($event) { + if (!$event['source_mer_id']) { + return true; + } $sourceMerchant = Merchant::getDB()->where('mer_id', $event['source_mer_id'])->field('category_id,type_id')->find(); if ($sourceMerchant['type_id'] != Merchant::TypeStore) { return true; @@ -36,12 +39,13 @@ class CloudProduct $repo->update((int)$productId, $values); } } + $cacheKey = sprintf(\app\common\model\store\product\CloudProduct::CacheKey, $cloudMerchant); $RedisCacheService = app()->make(RedisCacheService::class); foreach ($event['product_id'] as $productId) { if ($event['status'] == 1) { - $RedisCacheService->SADD('CloudMerchantSpu_' . $cloudMerchant, $productId); + $RedisCacheService->SADD($cacheKey, $productId); } else { - $RedisCacheService->SREM('CloudMerchantSpu_' . $cloudMerchant, $productId); + $RedisCacheService->SREM($cacheKey, $productId); } } }