userId; // 获取当前用户的神像变更临时数据 $userGodsTemp = Cache::get($tempKey, []); // 计算过期时间戳 $expireTime = $event->buff->expire_time ? $event->buff->expire_time->timestamp : null; $now = time(); $duration = $expireTime ? ($expireTime - $now) : 0; // 构建神像变更数据 $godData = [ 'god_type' => $event->buff->buff_type, 'status' => true, // 激活状态 'expire_time' => $expireTime, 'duration' => $duration, 'updated_at' => $now, ]; // 使用神像类型作为键,实现同一神像多次变更的数据覆盖 $userGodsTemp[$event->buff->buff_type] = $godData; // 将更新后的数据存回临时存储 Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL); Log::info('神像变更数据已临时存储', [ 'user_id' => $event->userId, 'god_type' => $event->buff->buff_type, 'expire_time' => $expireTime, 'duration' => $duration, ]); } catch (\Exception $e) { Log::error('神像变更数据临时存储失败', [ 'error' => $e->getMessage(), 'user_id' => $event->userId, 'god_type' => $event->buff->buff_type ?? null, ]); } } /** * 获取用户的神像变更临时数据 * * @param int $userId 用户ID * @return GodChangeTempDto[] 用户的神像变更数据 */ public static function getUserGodChanges(int $userId): array { $tempKey = self::TEMP_KEY_PREFIX . $userId; $rawData = Cache::get($tempKey, []); $result = []; foreach ($rawData as $godType => $godData) { $result[$godType] = GodChangeTempDto::fromArray($godData); } return $result; } /** * 获取用户特定神像类型的变更临时数据 * * @param int $userId 用户ID * @param int $godType 神像类型 * @return GodChangeTempDto|null 神像变更数据,不存在时返回null */ public static function getUserGodChange(int $userId, int $godType): ?GodChangeTempDto { $userGodChanges = self::getUserGodChanges($userId); return $userGodChanges[$godType] ?? null; } /** * 清除用户的神像变更临时数据 * * @param int $userId 用户ID * @return void */ public static function clearUserGodChanges(int $userId): void { $tempKey = self::TEMP_KEY_PREFIX . $userId; Cache::put($tempKey, [], 0); } /** * 清除用户特定神像类型的变更临时数据 * * @param int $userId 用户ID * @param int $godType 神像类型 * @return void */ public static function clearUserGodChange(int $userId, int $godType): void { $tempKey = self::TEMP_KEY_PREFIX . $userId; $userGodsTemp = Cache::get($tempKey, []); if (isset($userGodsTemp[$godType])) { unset($userGodsTemp[$godType]); Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL); } } }