修改店铺云商品列表

This commit is contained in:
luofei 2023-06-12 14:32:59 +08:00
parent a8f42976bd
commit 09603ced7c
3 changed files with 27 additions and 26 deletions
app
common
model/store/product
repositories/store/product
listener

@ -17,7 +17,8 @@ use app\common\model\BaseModel;
class CloudProduct extends BaseModel class CloudProduct extends BaseModel
{ {
const CacheKey = 'CloudMerchantSpu_%s'; const CacheKey = 'CloudMerchantSpu_%s'; //店铺云商品缓存key
const TakenKey = 'CloudMerchantSpu_%s_page'; //已经取过的页码缓存key
public static function tablePk(): string public static function tablePk(): string
{ {

@ -220,38 +220,38 @@ class SpuRepository extends BaseRepository
* 从缓存中随机取出不重复数据 * 从缓存中随机取出不重复数据
* @param $merId * @param $merId
* @param $page * @param $page
* @param $limit
* @return array * @return array
* @throws \RedisException * @throws \RedisException
*/ */
public function getSpuFromCache($merId, $page): array public function getSpuFromCache($merId, $page, $limit): array
{ {
$dataKey = sprintf(CloudProduct::CacheKey, $merId); $dataKey = sprintf(CloudProduct::CacheKey, $merId);
$takenKey = $dataKey . '_page_'. $page; $takenKey = sprintf(CloudProduct::TakenKey, $merId);
$remainKey = 'CloudMerchantSpuRemain_' . $merId; /** @var RedisCacheService $redis */
/** @var RedisCacheService $RedisCacheService */ $redis = app()->make(RedisCacheService::class);
$RedisCacheService = app()->make(RedisCacheService::class); $taken = $redis->hGet($takenKey, $page);
if ($RedisCacheService->exists($takenKey)) { $taken = empty($taken) ? [] : explode(',', $taken);
return $RedisCacheService->sMembers($takenKey); if (!empty($taken)) {
return $taken;
} }
$remainCount = $RedisCacheService->sCard($remainKey); $all = $redis->sMembers($dataKey);
if ($remainCount == 0 && $page > 1) { $takenPage = $redis->hGetAll($takenKey);
$allTaken = [];
foreach ($takenPage as $value) {
$allTaken = array_merge(explode(',', $value), $allTaken);
}
$diff = array_diff($all, $allTaken);
if (empty($diff)) {
return []; return [];
} }
$productIds = []; $productIds = [];
if ($remainCount == 0) { $indexes = count($diff) >= $limit ? array_rand($diff, $limit) : array_keys($diff);
$productIds = $RedisCacheService->sRandMember($dataKey, 10); foreach ($indexes as $index) {
} else { $productIds[] = $diff[$index];
for ($i = 0; $i < min($remainCount, 10); $i++) {
$productIds[] = $RedisCacheService->sPop($remainKey);
}
} }
if ($page == 1) { $redis->hSet($takenKey, $page, implode(',', $productIds));
$allProductIds = $RedisCacheService->sMembers($dataKey); $redis->expire($takenKey, 120);
$remainProductIds = array_diff($allProductIds, $productIds);
$RedisCacheService->sAdd($remainKey, ...$remainProductIds);
}
$RedisCacheService->sAdd($takenKey, ...$productIds);
$RedisCacheService->expire($takenKey, 180);
return $productIds; return $productIds;
} }

@ -40,17 +40,17 @@ class CloudProduct
} }
} }
$cacheKey = sprintf(\app\common\model\store\product\CloudProduct::CacheKey, $cloudMerchant); $cacheKey = sprintf(\app\common\model\store\product\CloudProduct::CacheKey, $cloudMerchant);
$remainKey = 'CloudMerchantSpuRemain_' . $cloudMerchant; /** @var RedisCacheService $RedisCacheService */
$RedisCacheService = app()->make(RedisCacheService::class); $RedisCacheService = app()->make(RedisCacheService::class);
foreach ($event['product_id'] as $productId) { foreach ($event['product_id'] as $productId) {
if ($event['status'] == 1) { if ($event['status'] == 1) {
$RedisCacheService->SADD($cacheKey, $productId); $RedisCacheService->SADD($cacheKey, $productId);
$RedisCacheService->sAdd($remainKey, $productId);
} else { } else {
$RedisCacheService->SREM($cacheKey, $productId); $RedisCacheService->SREM($cacheKey, $productId);
$RedisCacheService->SREM($remainKey, $productId);
} }
} }
$takenKey = sprintf(\app\common\model\store\product\CloudProduct::TakenKey, $cloudMerchant);
$RedisCacheService->del($takenKey);
} }
} }