盲盒转盘优化-重构-修改调试

This commit is contained in:
2025-10-29 15:11:50 +08:00
parent 2814c8a7b7
commit e2aea224c9
2 changed files with 23 additions and 30 deletions

View File

@@ -25,7 +25,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
$this->redis->connect(config('redis.host'), config('redis.port')); // 根据实际配置调整主机和端口
// 选择数据库1
$this->redis->select(1);
$this->cache_time = 60 *24;
$this->cache_time = 60*60; //缓存一小时
} catch (\Exception $e) {
Log::record('Redis连接失败: ' . $e->getMessage(), 'error');
$this->redis = null;
@@ -336,7 +336,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
Cache::set($cacheKey, $pan_gift_bag_detail, $this->cache_time);
Cache::set("pan_total_quantity".$gift_bag_id, $total_quantity, $this->cache_time);
Cache::set("pan_total_remaining".$gift_bag_id, $total_remaining, $this->cache_time);
Cache::set("pan_total_draw_times".$gift_bag_id, $total_draw_times, $this->cache_time);
Cache::set("pan_total_draw".$gift_bag_id, $total_draw_times, $this->cache_time);
}
return $pan_gift_bag_detail;
}
@@ -708,30 +708,18 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
// 批量更新
foreach ($inventoryUpdates as $giftId => $count) {
$giftBagDetail = Db::name("vs_gift_bag_detail")
->where('id', $giftId)
->find();
$giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $giftId);
if (!$giftBagDetail) {
throw new \Exception("礼物详情不存在ID: " . $giftId);
}
$upRemainingNumber = $giftBagDetail['remaining_number'] - $count;
if($upRemainingNumber!=$giftBagDetailCached['remaining_number']){
$this->redis->setex( 'blind_box_draw_errors_' . date('Y-m-d-H-i-s'), 86400 * 7, "有并发礼物数量不一致礼物ID: " . $giftId . '数据库数量: '.$upRemainingNumber. '缓存数量: '.$giftBagDetailCached['remaining_number']. ' ' .json_encode($giftBagDetail) . ' ' .json_encode($giftBagDetailCached));
if(empty($giftBagDetailCached)){
$giftBagDetail = db::name("vs_gift_bag_detail")->where('id',$giftId)->find();
$upRemainingNumber = $giftBagDetail['remaining_number'] - $count;
}else{
$upRemainingNumber = $giftBagDetailCached['remaining_number'];
}
// 检查库存是否足够
if ($upRemainingNumber < 0) {
throw new \Exception("礼物库存不足ID: " . $giftId);
}
if($upRemainingNumber = $giftBagDetail['remaining_number']){
return;
}
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([
'remaining_number' => $upRemainingNumber
]);
if (!$ret) {
throw new \Exception('更新礼物剩余数量失败');
throw new \Exception('更新礼物剩余数量失败 ID: ' . $giftId);
}
}
}