1.增加互斥锁:保证缓存和数据库数据一致性

2.增加消费队列,保证mysql数据的正常保存
This commit is contained in:
2026-04-20 14:13:48 +08:00
parent 614fb00ec4
commit 1eed3cf0f7
23 changed files with 836 additions and 255 deletions

View File

@@ -4,7 +4,8 @@ namespace app\admin\controller\config;
use app\common\controller\Backend;
use app\common\library\game\DepositTier as DepositTierLib;
use app\common\service\GameHotDataRedis;
use app\common\service\GameHotDataCoordinator;
use app\common\service\GameHotDataLock;
use InvalidArgumentException;
use support\think\Db;
use support\Response;
@@ -104,30 +105,39 @@ class DepositTier extends Backend
}
$now = time();
try {
$exists = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => DepositTierLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '充值档位 JSON 数组(独立表单维护)',
'create_time' => $now,
'update_time' => $now,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(DepositTierLib::CONFIG_KEY);
$lock = GameHotDataLock::tryAcquire(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey);
if (!$lock['acquired']) {
return $this->error('该配置正在被其他操作占用,请稍后再试');
}
try {
try {
$exists = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => DepositTierLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '充值档位 JSON 数组(独立表单维护)',
'create_time' => $now,
'update_time' => $now,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
GameHotDataRedis::gameConfigForget(DepositTierLib::CONFIG_KEY);
GameHotDataCoordinator::afterGameConfigKeyCommitted(DepositTierLib::CONFIG_KEY);
return $this->success(__('Saved successfully'));
return $this->success(__('Saved successfully'));
} finally {
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
}
}
}

View File

@@ -6,7 +6,8 @@ namespace app\admin\controller\config;
use app\common\controller\Backend;
use app\common\library\game\StreakWinReward as StreakWinRewardLib;
use app\common\service\GameHotDataRedis;
use app\common\service\GameHotDataCoordinator;
use app\common\service\GameHotDataLock;
use support\think\Db;
use support\Response;
use Throwable;
@@ -91,33 +92,42 @@ class StreakWinReward extends Backend
}
$encoded = StreakWinRewardLib::encodeForDb($payload);
$now = time();
Db::startTrans();
try {
$exists = Db::name('game_config')->where('config_key', StreakWinRewardLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', StreakWinRewardLib::CONFIG_KEY)->update([
'config_value' => $encoded,
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => StreakWinRewardLib::CONFIG_KEY,
'config_value' => $encoded,
'value_type' => 'json',
'remark' => '连胜奖励',
'create_time' => $now,
'update_time' => $now,
]);
}
Db::commit();
} catch (Throwable $e) {
Db::rollback();
return $this->error($e->getMessage());
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(StreakWinRewardLib::CONFIG_KEY);
$lock = GameHotDataLock::tryAcquire(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey);
if (!$lock['acquired']) {
return $this->error('该配置正在被其他操作占用,请稍后再试');
}
StreakWinRewardLib::clearCache();
GameHotDataRedis::gameConfigForget(StreakWinRewardLib::CONFIG_KEY);
try {
Db::startTrans();
try {
$exists = Db::name('game_config')->where('config_key', StreakWinRewardLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', StreakWinRewardLib::CONFIG_KEY)->update([
'config_value' => $encoded,
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => StreakWinRewardLib::CONFIG_KEY,
'config_value' => $encoded,
'value_type' => 'json',
'remark' => '连胜奖励',
'create_time' => $now,
'update_time' => $now,
]);
}
Db::commit();
} catch (Throwable $e) {
Db::rollback();
return $this->success('保存成功');
return $this->error($e->getMessage());
}
StreakWinRewardLib::clearCache();
GameHotDataCoordinator::afterGameConfigKeyCommitted(StreakWinRewardLib::CONFIG_KEY);
return $this->success('保存成功');
} finally {
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
}
}
}

View File

@@ -3,7 +3,8 @@
namespace app\admin\controller\config;
use app\common\controller\Backend;
use app\common\service\GameHotDataRedis;
use app\common\service\GameHotDataCoordinator;
use app\common\service\GameHotDataLock;
use app\common\library\game\ZiHuaDictionary as ZiHuaDictionaryLib;
use InvalidArgumentException;
use support\think\Db;
@@ -104,30 +105,39 @@ class ZiHuaDictionary extends Backend
}
$now = time();
try {
$exists = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => ZiHuaDictionaryLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '36字花字典 JSON 数组(独立表单维护)',
'create_time' => $now,
'update_time' => $now,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(ZiHuaDictionaryLib::CONFIG_KEY);
$lock = GameHotDataLock::tryAcquire(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey);
if (!$lock['acquired']) {
return $this->error('该配置正在被其他操作占用,请稍后再试');
}
try {
try {
$exists = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => ZiHuaDictionaryLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '36字花字典 JSON 数组(独立表单维护)',
'create_time' => $now,
'update_time' => $now,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
GameHotDataRedis::gameConfigForget(ZiHuaDictionaryLib::CONFIG_KEY);
GameHotDataCoordinator::afterGameConfigKeyCommitted(ZiHuaDictionaryLib::CONFIG_KEY);
return $this->success(__('Saved successfully'));
return $this->success(__('Saved successfully'));
} finally {
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
}
}
}