优化随机查询云仓商品

This commit is contained in:
luofei 2023-05-31 15:19:31 +08:00
parent ddd743103a
commit d00c62f859

View File

@ -188,7 +188,8 @@ class SpuRepository extends BaseRepository
$RedisCacheService->SADD($dataKey, $productId);
}
}
$where['product_id'] = $this->getSpuFromCache($where['mer_id'], $page);
$where['product_id'] = $RedisCacheService->sRandMember($dataKey, 10);
// $where['product_id'] = $this->getSpuFromCache($where['mer_id'], $page);
unset($where['mer_id'], $where['page']);
$page = 1;
}
@ -225,12 +226,17 @@ class SpuRepository extends BaseRepository
{
$dataKey = 'CloudMerchantSpu_' . $merId;
$takenKey = 'CloudMerchantSpuTakenProductIds_' . $merId . '_page_'. $page;
$allTakenKey = 'CloudMerchantSpuTakenProductIds_' . $merId;
/** @var RedisCacheService $RedisCacheService */
$RedisCacheService = app()->make(RedisCacheService::class);
if ($RedisCacheService->exists($takenKey)) {
return $RedisCacheService->sMembers($takenKey);
}
$keys = $RedisCacheService->sDiff($dataKey, $takenKey);
if ($RedisCacheService->sCard($allTakenKey) == $RedisCacheService->sCard($dataKey)) {
$keys = $RedisCacheService->sRandMember($dataKey, 10);
} else {
$keys = $RedisCacheService->sDiff($dataKey, $allTakenKey);
}
$number = min(count($keys), 10);
if ($number <= 0) {
return [];
@ -241,6 +247,7 @@ class SpuRepository extends BaseRepository
$productIds[] = $keys[$index];
}
$RedisCacheService->sAdd($takenKey, ...$productIds);
$RedisCacheService->sAdd($allTakenKey, ...$productIds);
$RedisCacheService->expire($takenKey, 60);
return $productIds;
}