Forráskód Böngészése

feat(Game): 新增Item缓存功能

- 添加ItemCache逻辑类
- 实现ItemCacheService服务
- 创建ItemQuantityChangedListener监听器
- 移除废弃的测试命令和服务提供者
Your Name 8 hónapja
szülő
commit
3b871e9f2c

+ 0 - 33
app/Module/Game/Commands/TestCommands.php

@@ -1,33 +0,0 @@
-<?php
-
-namespace App\Module\Game\Commands;
-
-
-use Illuminate\Console\Command;
-
-class TestCommands extends Command
-{
-    /**
-     * 控制台命令的名称和签名
-     *
-     * @var string
-     */
-    protected $signature = 'game:test';
-
-    /**
-     * 命令描述
-     *
-     * @var string
-     */
-    protected $description = 'Test commands';
-
-    /**
-     * 执行命令
-     */
-    public function handle()
-    {
-
-        echo '完成';
-    }
-
-}

+ 57 - 0
app/Module/Game/Listeners/ItemQuantityChangedListener.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Module\Game\Listeners;
+
+use App\Module\Game\Logics\ItemCache;
+use App\Module\GameItems\Events\ItemQuantityChanged;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 物品数量变更事件监听器
+ * 
+ * 监听物品数量变更事件,并调用逻辑层处理缓存逻辑
+ * 实现了ShouldQueue接口,表示它将在队列中异步处理事件
+ */
+class ItemQuantityChangedListener implements ShouldQueue
+{
+    /**
+     * 创建事件监听器
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * 处理事件
+     *
+     * 接收物品数量变更事件,并调用逻辑层的方法处理缓存
+     *
+     * @param ItemQuantityChanged $event 物品数量变更事件
+     * @return void
+     */
+    public function handle(ItemQuantityChanged $event): void
+    {
+        try {
+            Log::info('接收到物品数量变更事件', [
+                'user_id' => $event->userId,
+                'item_id' => $event->itemId,
+                'old_quantity' => $event->oldQuantity,
+                'new_quantity' => $event->newQuantity,
+                'change_amount' => $event->changeAmount,
+            ]);
+            
+            // 调用逻辑层处理缓存
+            ItemCache::handleItemQuantityChanged($event);
+        } catch (\Exception $e) {
+            Log::error('处理物品数量变更事件失败', [
+                'error' => $e->getMessage(),
+                'user_id' => $event->userId,
+                'item_id' => $event->itemId,
+            ]);
+        }
+    }
+}

+ 118 - 0
app/Module/Game/Logics/ItemCache.php

@@ -0,0 +1,118 @@
+<?php
+
+namespace App\Module\Game\Logics;
+
+use App\Module\GameItems\Events\ItemQuantityChanged;
+use App\Module\LCache\Cache;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 物品缓存逻辑类
+ *
+ * 负责处理物品变更事件的缓存逻辑,包括:
+ * 1. 将物品变更数据存入缓存
+ * 2. 按照用户进行存储
+ * 3. 同一物品多次变更进行数据覆盖
+ */
+class ItemCache
+{
+
+    /**
+     * 缓存键前缀
+     */
+    const CACHE_KEY_PREFIX = 'game:item:changed:';
+
+    /**
+     * 缓存过期时间(秒)
+     */
+    const CACHE_TTL = 3600; // 1小时
+
+    /**
+     * 处理物品数量变更事件
+     *
+     * 将物品变更数据存入缓存,按用户ID和物品ID进行存储
+     * 同一物品多次变更会覆盖之前的数据
+     *
+     * @param ItemQuantityChanged $event 物品数量变更事件
+     * @return void
+     */
+    public static function handleItemQuantityChanged(ItemQuantityChanged $event): void
+    {
+        try {
+            // 构建缓存键,按用户ID进行存储
+            $cacheKey = self::CACHE_KEY_PREFIX . $event->userId;
+
+            // 获取当前用户的物品变更缓存
+            $userItemsCache = Cache::get($cacheKey, []);
+
+            // 构建物品变更数据
+            $itemData = [
+                'item_id'       => $event->itemId,
+                'instance_id'   => $event->instanceId,
+                'old_quantity'  => $event->oldQuantity,
+                'new_quantity'  => $event->newQuantity,
+                'change_amount' => $event->changeAmount,
+                'user_item_id'  => $event->userItemId,
+                'updated_at'    => time(),
+            ];
+
+            // 使用物品ID作为键,实现同一物品多次变更的数据覆盖
+            $userItemsCache[$event->itemId] = $itemData;
+            dump($userItemsCache);
+            // 将更新后的数据存回缓存
+            Cache::put($cacheKey, $userItemsCache, self::CACHE_TTL);
+
+            Log::info('物品变更数据已缓存', [
+                'user_id'       => $event->userId,
+                'item_id'       => $event->itemId,
+                'change_amount' => $event->changeAmount,
+            ]);
+        } catch (\Exception $e) {
+            Log::error('物品变更数据缓存失败', [
+                'error'   => $e->getMessage(),
+                'user_id' => $event->userId,
+                'item_id' => $event->itemId,
+            ]);
+        }
+    }
+
+    /**
+     * 获取用户的物品变更缓存
+     *
+     * @param int $userId 用户ID
+     * @return array 用户的物品变更数据
+     */
+    public static function getUserItemChanges(int $userId): array
+    {
+        $cacheKey = self::CACHE_KEY_PREFIX . $userId;
+
+        return Cache::get($cacheKey, []);
+    }
+
+    /**
+     * 获取用户特定物品的变更缓存
+     *
+     * @param int $userId 用户ID
+     * @param int $itemId 物品ID
+     * @return array|null 物品变更数据,不存在时返回null
+     */
+    public static function getUserItemChange(int $userId, int $itemId): ?array
+    {
+        $userItemChanges = self::getUserItemChanges($userId);
+
+        return $userItemChanges[$itemId] ?? null;
+    }
+
+    /**
+     * 清除用户的物品变更缓存
+     *
+     * @param int $userId 用户ID
+     * @return void
+     */
+    public static function clearUserItemChanges(int $userId): void
+    {
+        $cacheKey = self::CACHE_KEY_PREFIX . $userId;
+        Cache::put($cacheKey, [], 0);
+    }
+
+}

+ 0 - 29
app/Module/Game/Providers/TestServiceProvider.php

@@ -1,29 +0,0 @@
-<?php
-
-namespace App\Module\Game\Providers;
-
-use App\Module\Game\Events\TestEvent;
-
-use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
-
-class TestServiceProvider extends ServiceProvider
-{
-    /**
-     * 事件到监听器的映射
-     *
-     * @var array<class-string, array<int, class-string>>
-     */
-    protected $listen = [
-        TestEvent::class => [
-
-        ],
-    ];
-
-    /**
-     * 注册任何事件监听器
-     */
-    public function boot(): void
-    {
-        parent::boot();
-    }
-}

+ 74 - 0
app/Module/Game/Services/ItemCacheService.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Module\Game\Services;
+
+use App\Module\Game\Logics\ItemCache;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 物品缓存服务类
+ * 
+ * 提供物品缓存相关的服务方法,用于外部调用
+ */
+class ItemCacheService
+{
+    /**
+     * 获取用户的物品变更缓存
+     * 
+     * @param int $userId 用户ID
+     * @return array 用户的物品变更数据
+     */
+    public function getUserItemChanges(int $userId): array
+    {
+        try {
+            return ItemCache::getUserItemChanges($userId);
+        } catch (\Exception $e) {
+            Log::error('获取用户物品变更缓存失败', [
+                'error' => $e->getMessage(),
+                'user_id' => $userId,
+            ]);
+            return [];
+        }
+    }
+    
+    /**
+     * 获取用户特定物品的变更缓存
+     * 
+     * @param int $userId 用户ID
+     * @param int $itemId 物品ID
+     * @return array|null 物品变更数据,不存在时返回null
+     */
+    public function getUserItemChange(int $userId, int $itemId): ?array
+    {
+        try {
+            return ItemCache::getUserItemChange($userId, $itemId);
+        } catch (\Exception $e) {
+            Log::error('获取用户特定物品变更缓存失败', [
+                'error' => $e->getMessage(),
+                'user_id' => $userId,
+                'item_id' => $itemId,
+            ]);
+            return null;
+        }
+    }
+    
+    /**
+     * 清除用户的物品变更缓存
+     * 
+     * @param int $userId 用户ID
+     * @return bool 操作是否成功
+     */
+    public function clearUserItemChanges(int $userId): bool
+    {
+        try {
+            ItemCache::clearUserItemChanges($userId);
+            return true;
+        } catch (\Exception $e) {
+            Log::error('清除用户物品变更缓存失败', [
+                'error' => $e->getMessage(),
+                'user_id' => $userId,
+            ]);
+            return false;
+        }
+    }
+}