优化随机查询云仓商品

This commit is contained in:
luofei 2023-05-31 12:53:09 +08:00
parent 8f7da59b5d
commit a36a775db4

View File

@ -225,19 +225,18 @@ class SpuRepository extends BaseRepository
$dataKey = 'CloudMerchantSpu_' . $merId;
$takenKey = 'CloudMerchantSpuTakenProductIds_' . $merId . '_page_'. $page;
$RedisCacheService = app()->make(RedisCacheService::class);
if (!$RedisCacheService->exists($takenKey)) {
$productIds = $RedisCacheService->sRandMember($dataKey, 10);
} else {
$keys = $RedisCacheService->sDiff($dataKey, $takenKey);
$number = min(count($keys), 10);
if ($number <= 0) {
return [];
}
$indexes = array_rand($keys, $number);
$productIds = [];
foreach ($indexes as $index) {
$productIds[] = $keys[$index];
}
if ($RedisCacheService->exists($takenKey)) {
return $RedisCacheService->get($dataKey);
}
$keys = $RedisCacheService->sDiff($dataKey, $takenKey);
$number = min(count($keys), 10);
if ($number <= 0) {
return [];
}
$indexes = array_rand($keys, $number);
$productIds = [];
foreach ($indexes as $index) {
$productIds[] = $keys[$index];
}
$RedisCacheService->sAdd($takenKey, ...$productIds);
$RedisCacheService->expire($takenKey, 60);