新需求-活动需求-盲盒转盘调通盘-巡乐会-调试

This commit is contained in:
2025-10-16 18:27:45 +08:00
parent d284ee8346
commit 8269218c7d

View File

@@ -767,28 +767,8 @@ class BlindBoxTurntableGiftDrawWorld extends Model
*/ */
private function handleXlhOperations($room_id, $xlh_ext, $currentXlhPeriodsNum,$addcurrentXlhPeriodsNum,$room) private function handleXlhOperations($room_id, $xlh_ext, $currentXlhPeriodsNum,$addcurrentXlhPeriodsNum,$room)
{ {
// 增加对 $xlh_ext 的安全检查
if (empty($xlh_ext) || !isset($xlh_ext['open_condition'])) {
return;
}
// 使用 Redis 锁防止并发问题
$lockKey = "xlh_operation_lock_{$room_id}";
$lockAcquired = false;
if ($this->redis) {
// 尝试获取锁最多等待3秒
$lockValue = uniqid();
for ($i = 0; $i < 30; $i++) {
if ($this->redis->set($lockKey, $lockValue, ['NX', 'EX' => 10])) {
$lockAcquired = true;
break;
}
usleep(100000); // 等待100ms
}
}
try {
$xlhPeriodsNum = $this->getCachedXlhPeriodsNum("get");
$xlhIsPiaoPing = 0; $xlhIsPiaoPing = 0;
$xlhPeriodsNum = $this->getCachedXlhPeriodsNum("get");
if($xlhPeriodsNum < $xlh_ext['open_condition']['waiting_start_num'] && $currentXlhPeriodsNum >= $xlh_ext['open_condition']['waiting_start_num']){ if($xlhPeriodsNum < $xlh_ext['open_condition']['waiting_start_num'] && $currentXlhPeriodsNum >= $xlh_ext['open_condition']['waiting_start_num']){
$xlhIsPiaoPing = 1; $xlhIsPiaoPing = 1;
} }
@@ -802,38 +782,12 @@ class BlindBoxTurntableGiftDrawWorld extends Model
if ($xlhIsPiaoPing == 1 || $xlhIsPiaoPing == 2) { if ($xlhIsPiaoPing == 1 || $xlhIsPiaoPing == 2) {
$this->handleXlhPiaoPing($room_id, $xlh_ext, $xlhIsPiaoPing); $this->handleXlhPiaoPing($room_id, $xlh_ext, $xlhIsPiaoPing);
} }
// 减少不必要的数据库查询
$shouldUpdateStatus = false;
if($xlhPeriodsNum <= $xlh_ext['open_condition']['waiting_start_num']){ if($xlhPeriodsNum <= $xlh_ext['open_condition']['waiting_start_num']){
$shouldUpdateStatus = true;
} else {
// 使用缓存优化,避免每次都查询数据库
$panXlhCacheKey = "pan_xlh_status";
$hasActiveXlh = Cache::get($panXlhCacheKey);
if ($hasActiveXlh === null) {
$pan_xlh = db::name('vs_room_pan_xlh')
->where('send_time', 0)
->order('id', 'desc')
->find();
$hasActiveXlh = !empty($pan_xlh) ? 1 : 0;
Cache::set($panXlhCacheKey, $hasActiveXlh, 30); // 缓存30秒
}
if (!$hasActiveXlh) {
$shouldUpdateStatus = true;
// 清除缓存以便下次重新检查
Cache::rm($panXlhCacheKey);
}
}
if ($shouldUpdateStatus) {
$this->updateAndPushXlhStatus($room_id, $xlh_ext, $currentXlhPeriodsNum); $this->updateAndPushXlhStatus($room_id, $xlh_ext, $currentXlhPeriodsNum);
} }else{
} finally { $pan_xlh = db::name('vs_room_pan_xlh')->where('send_time',0)->order('id desc')->find();
// 释放锁 if(empty($pan_xlh)){
if ($lockAcquired && $this->redis) { $this->updateAndPushXlhStatus($room_id, $xlh_ext, $currentXlhPeriodsNum);
$this->redis->del($lockKey);
} }
} }
} }
@@ -1054,26 +1008,13 @@ class BlindBoxTurntableGiftDrawWorld extends Model
*/ */
private function getCachedXlhPeriodsNum($type="get",$num=1) { private function getCachedXlhPeriodsNum($type="get",$num=1) {
$cacheKey = "xlh_periods_num"; $cacheKey = "xlh_periods_num";
if ($type == "set") {
if ($this->redis) {
// 使用 Redis 原子操作
return $this->redis->incrBy($cacheKey, $num);
} else {
// 回退到缓存操作
$xlh_periods_num = Cache::get($cacheKey) ?? 0; $xlh_periods_num = Cache::get($cacheKey) ?? 0;
if($type=="set"){
$xlh_periods_num = $xlh_periods_num + $num; $xlh_periods_num = $xlh_periods_num + $num;
Cache::set($cacheKey, $xlh_periods_num, 0); Cache::set($cacheKey, $xlh_periods_num, 0);
}
return $xlh_periods_num; return $xlh_periods_num;
} }
} else {
if ($this->redis) {
$value = $this->redis->get($cacheKey);
return $value !== false ? (int)$value : 0;
} else {
return Cache::get($cacheKey) ?? 0;
}
}
}
/** /**
* 获取缓存的巡乐会期数 * 获取缓存的巡乐会期数
*/ */