<?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,
        ]);
    }
}
