修改店铺云商品列表

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

View File

@ -17,7 +17,8 @@ use app\common\model\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
{

View File

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

View File

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