1.增加互斥锁:保证缓存和数据库数据一致性
2.增加消费队列,保证mysql数据的正常保存
This commit is contained in:
47
app/common/service/GameHotDataCoordinator.php
Normal file
47
app/common/service/GameHotDataCoordinator.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user