cachePrefix . 'all'; $cachedSeeds = Cache::get($cacheKey); if ($cachedSeeds) { return $cachedSeeds; } // 从数据库获取 $seeds = FarmSeed::with('outputs')->get(); // 缓存结果 Cache::put($cacheKey, $seeds, $this->cacheExpiration); return $seeds; } catch (\Exception $e) { Log::error('获取所有种子失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return collect(); } } /** * 获取指定类型的种子 * * @param int $type * @return Collection */ public function getSeedsByType(int $type): Collection { try { // 尝试从缓存获取 $cacheKey = $this->cachePrefix . 'type:' . $type; $cachedSeeds = Cache::get($cacheKey); if ($cachedSeeds) { return $cachedSeeds; } // 从数据库获取 $seeds = FarmSeed::where('type', $type) ->with('outputs') ->get(); // 缓存结果 Cache::put($cacheKey, $seeds, $this->cacheExpiration); return $seeds; } catch (\Exception $e) { Log::error('获取指定类型种子失败', [ 'type' => $type, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return collect(); } } /** * 获取种子详情 * * @param int $seedId * @return FarmSeed|null */ public function getSeedDetail(int $seedId): ?FarmSeed { try { // 尝试从缓存获取 $cacheKey = $this->cachePrefix . 'id:' . $seedId; $cachedSeed = Cache::get($cacheKey); if ($cachedSeed) { return $cachedSeed; } // 从数据库获取 $seed = FarmSeed::with('outputs')->find($seedId); if (!$seed) { return null; } // 缓存结果 Cache::put($cacheKey, $seed, $this->cacheExpiration); return $seed; } catch (\Exception $e) { Log::error('获取种子详情失败', [ 'seed_id' => $seedId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 获取种子的产出配置 * * @param int $seedId * @return Collection */ public function getSeedOutputs(int $seedId): Collection { try { // 尝试从缓存获取 $cacheKey = $this->cachePrefix . 'outputs:' . $seedId; $cachedOutputs = Cache::get($cacheKey); if ($cachedOutputs) { return $cachedOutputs; } // 从数据库获取 $outputs = FarmSeedOutput::where('seed_id', $seedId) ->orderByDesc('probability') ->get(); // 缓存结果 Cache::put($cacheKey, $outputs, $this->cacheExpiration); return $outputs; } catch (\Exception $e) { Log::error('获取种子产出配置失败', [ 'seed_id' => $seedId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return collect(); } } /** * 根据物品ID获取种子 * * @param int $itemId * @return FarmSeed|null */ public function getSeedByItemId(int $itemId): ?FarmSeed { try { // 尝试从缓存获取 $cacheKey = $this->cachePrefix . 'item:' . $itemId; $cachedSeed = Cache::get($cacheKey); if ($cachedSeed) { return $cachedSeed; } // 从数据库获取 $seed = FarmSeed::where('item_id', $itemId)->first(); if (!$seed) { return null; } // 缓存结果 Cache::put($cacheKey, $seed, $this->cacheExpiration); return $seed; } catch (\Exception $e) { Log::error('根据物品ID获取种子失败', [ 'item_id' => $itemId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 清除种子缓存 * * @param int|null $seedId * @return bool */ public function clearSeedCache(?int $seedId = null): bool { try { if ($seedId) { // 清除指定种子的缓存 Cache::forget($this->cachePrefix . 'id:' . $seedId); Cache::forget($this->cachePrefix . 'outputs:' . $seedId); // 获取种子信息 $seed = FarmSeed::find($seedId); if ($seed) { // 清除种子类型缓存 Cache::forget($this->cachePrefix . 'type:' . $seed->type); // 清除物品ID缓存 Cache::forget($this->cachePrefix . 'item:' . $seed->item_id); } } else { // 清除所有种子缓存 Cache::forget($this->cachePrefix . 'all'); // 清除所有类型缓存 for ($type = 1; $type <= 3; $type++) { Cache::forget($this->cachePrefix . 'type:' . $type); } } return true; } catch (\Exception $e) { Log::error('清除种子缓存失败', [ 'seed_id' => $seedId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } }