Files
webman-buildadmin/app/common/service/GameHotDataCoordinator.php
zhenhui 1eed3cf0f7 1.增加互斥锁:保证缓存和数据库数据一致性
2.增加消费队列,保证mysql数据的正常保存
2026-04-20 14:13:48 +08:00

48 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\common\service;
/**
* 缓存与队列统一入口:落库后先同步回源 Redis再入队幂等任务削峰。
*/
final class GameHotDataCoordinator
{
public static function afterUserCommitted(int $userId): void
{
if ($userId <= 0) {
return;
}
GameHotDataRedis::userReplaceCacheFromDb($userId);
GameHotDataWriteQueue::enqueue([
'op' => GameHotDataWriteQueue::OP_USER_REFRESH,
'id' => $userId,
]);
}
public static function afterGameConfigKeyCommitted(string $configKey): void
{
if ($configKey === '') {
return;
}
GameHotDataRedis::gameConfigReplaceFromDb($configKey);
GameHotDataWriteQueue::enqueue([
'op' => GameHotDataWriteQueue::OP_GC_REFRESH,
'key' => $configKey,
]);
}
/**
* @param int|null $recordId 有 id 时刷新该行并清除活跃/最新聚合键null 时仅清除聚合键
*/
public static function afterGameRecordCommitted(?int $recordId): void
{
GameHotDataRedis::gameRecordSyncCachesAfterDbWrite($recordId);
GameHotDataWriteQueue::enqueue([
'op' => GameHotDataWriteQueue::OP_GR_SYNC,
'id' => $recordId,
]);
}
}