|
|
@@ -23,10 +23,7 @@ use UCore\Helper\Cache;
|
|
|
*/
|
|
|
class PetTemp
|
|
|
{
|
|
|
- /**
|
|
|
- * 临时数据键前缀 - 宠物创建
|
|
|
- */
|
|
|
- const TEMP_KEY_CREATED_PREFIX = 'game:pet:created:';
|
|
|
+ // 已移除 TEMP_KEY_CREATED_PREFIX,与 TEMP_KEY_UPDATE_PREFIX 合并
|
|
|
|
|
|
/**
|
|
|
* 临时数据键前缀 - 宠物状态变更
|
|
|
@@ -51,15 +48,14 @@ class PetTemp
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $petId 宠物ID
|
|
|
- * @param string $keyPrefix 缓存键前缀
|
|
|
* @param string $eventType 事件类型,用于日志
|
|
|
* @return void
|
|
|
*/
|
|
|
- private static function handlePetFullData(int $userId, int $petId, string $keyPrefix, string $eventType): void
|
|
|
+ private static function handlePetFullData(int $userId, int $petId, string $eventType): void
|
|
|
{
|
|
|
try {
|
|
|
// 构建临时数据键,按用户ID进行存储
|
|
|
- $tempKey = $keyPrefix . $userId;
|
|
|
+ $tempKey = self::TEMP_KEY_UPDATE_PREFIX . $userId;
|
|
|
|
|
|
// 获取当前用户的宠物临时数据
|
|
|
$userPetsTemp = Cache::get($tempKey, []);
|
|
|
@@ -124,7 +120,7 @@ class PetTemp
|
|
|
*/
|
|
|
public static function handlePetCreated(PetCreatedEvent $event): void
|
|
|
{
|
|
|
- self::handlePetFullData($event->userId, $event->petId, self::TEMP_KEY_CREATED_PREFIX, '创建');
|
|
|
+ self::handlePetFullData($event->userId, $event->petId, '创建');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -196,11 +192,8 @@ class PetTemp
|
|
|
*/
|
|
|
public static function getUserPetCreated(int $userId): array
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_CREATED_PREFIX . $userId;
|
|
|
- $cachedData = Cache::get($tempKey, []);
|
|
|
-
|
|
|
- // 使用fromCache方法处理缓存数据,确保返回正确类型的DTO对象
|
|
|
- return PetStatusTempDto::fromCache($cachedData);
|
|
|
+ // 使用统一的前缀,与宠物更新数据共用一个缓存键
|
|
|
+ return self::getUserPetUpdates($userId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -212,8 +205,8 @@ class PetTemp
|
|
|
*/
|
|
|
public static function getUserPetCreatedById(int $userId, int $petId): ?PetStatusTempDto
|
|
|
{
|
|
|
- $userPetCreated = self::getUserPetCreated($userId);
|
|
|
- return $userPetCreated[$petId] ?? null;
|
|
|
+ // 使用统一的方法,与宠物更新数据共用一个获取方法
|
|
|
+ return self::getUserPetUpdateById($userId, $petId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -254,7 +247,7 @@ class PetTemp
|
|
|
*/
|
|
|
public static function handlePetUpdate(PetUpdateEvent $event): void
|
|
|
{
|
|
|
- self::handlePetFullData($event->userId, $event->petId, self::TEMP_KEY_UPDATE_PREFIX, '更新');
|
|
|
+ self::handlePetFullData($event->userId, $event->petId, '更新');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -293,8 +286,8 @@ class PetTemp
|
|
|
*/
|
|
|
public static function clearUserPetCreated(int $userId): void
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_CREATED_PREFIX . $userId;
|
|
|
- Cache::put($tempKey, [], 0);
|
|
|
+ // 使用统一的方法,与宠物更新数据共用一个清除方法
|
|
|
+ self::clearUserPetUpdates($userId);
|
|
|
}
|
|
|
|
|
|
/**
|