info('开始重建农场缓存...'); try { // 清除所有农场相关缓存 $this->clearCache(); // 重建种子配置缓存 $this->rebuildSeedCache(); // 重建种子产出配置缓存 $this->rebuildSeedOutputCache(); // 重建房屋配置缓存 $this->rebuildHouseConfigCache(); // 重建土地类型配置缓存 $this->rebuildLandTypeCache(); // 重建土地升级配置缓存 $this->rebuildLandUpgradeConfigCache(); $this->info('农场缓存重建完成'); $this->info('农场配置JSON文件已生成到 public/json/ 目录'); Log::info('农场缓存重建成功'); return 0; } catch (\Exception $e) { $this->error('农场缓存重建失败: ' . $e->getMessage()); Log::error('农场缓存重建失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 清除所有农场相关缓存 * * @return void */ private function clearCache() { $this->info('清除现有缓存...'); // 获取所有缓存键 $keys = Cache::get($this->cachePrefix . 'keys', []); // 清除每个缓存 foreach ($keys as $key) { Cache::forget($key); } // 清除缓存键列表 Cache::forget($this->cachePrefix . 'keys'); $this->info('现有缓存已清除'); } /** * 重建种子配置缓存 * * @return void */ private function rebuildSeedCache() { $this->info('重建种子配置缓存...'); // 获取所有种子 $seeds = FarmSeed::all(); // 缓存所有种子 $this->cacheData($this->cachePrefix . 'seeds:all', $seeds); // 按类型缓存种子 $seedsByType = $seeds->groupBy('type'); foreach ($seedsByType as $type => $typeSeeds) { $this->cacheData($this->cachePrefix . 'seeds:type:' . $type, $typeSeeds); } // 单独缓存每个种子 foreach ($seeds as $seed) { $this->cacheData($this->cachePrefix . 'seeds:id:' . $seed->id, $seed); } // 生成JSON文件 $this->saveJsonToFile('farm_seed.json', [ 'generated_at' => now()->toDateTimeString(), 'seeds' => $seeds->toArray() ]); $this->info('种子配置缓存重建完成'); } /** * 重建种子产出配置缓存 * * @return void */ private function rebuildSeedOutputCache() { $this->info('重建种子产出配置缓存...'); // 获取所有种子产出 $seedOutputs = FarmSeedOutput::all(); // 缓存所有种子产出 $this->cacheData($this->cachePrefix . 'seed_outputs:all', $seedOutputs); // 按种子ID缓存产出 $outputsBySeedId = $seedOutputs->groupBy('seed_id'); foreach ($outputsBySeedId as $seedId => $outputs) { $this->cacheData($this->cachePrefix . 'seed_outputs:seed_id:' . $seedId, $outputs); } // 生成JSON文件 $this->saveJsonToFile('farm_seed_output.json', [ 'generated_at' => now()->toDateTimeString(), 'seed_outputs' => $seedOutputs->toArray() ]); $this->info('种子产出配置缓存重建完成'); } /** * 重建房屋配置缓存 * * @return void */ private function rebuildHouseConfigCache() { $this->info('重建房屋配置缓存...'); // 获取所有房屋配置 $houseConfigs = FarmHouseConfig::all(); // 缓存所有房屋配置 $this->cacheData($this->cachePrefix . 'house_configs:all', $houseConfigs); // 按等级缓存房屋配置 foreach ($houseConfigs as $config) { $this->cacheData($this->cachePrefix . 'house_configs:level:' . $config->level, $config); } // 生成JSON文件 $this->saveJsonToFile('farm_house.json', [ 'generated_at' => now()->toDateTimeString(), 'house_configs' => $houseConfigs->toArray() ]); $this->info('房屋配置缓存重建完成'); } /** * 重建土地类型配置缓存 * * @return void */ private function rebuildLandTypeCache() { $this->info('重建土地类型配置缓存...'); // 获取所有土地类型 $landTypes = FarmLandType::all(); // 缓存所有土地类型 $this->cacheData($this->cachePrefix . 'land_types:all', $landTypes); // 按特殊类型缓存 $specialTypes = $landTypes->where('is_special', true); $normalTypes = $landTypes->where('is_special', false); $this->cacheData($this->cachePrefix . 'land_types:special', $specialTypes); $this->cacheData($this->cachePrefix . 'land_types:normal', $normalTypes); // 单独缓存每个土地类型 foreach ($landTypes as $type) { $this->cacheData($this->cachePrefix . 'land_types:id:' . $type->id, $type); } // 生成JSON文件 $this->saveJsonToFile('farm_land_type.json', [ 'generated_at' => now()->toDateTimeString(), 'land_types' => $landTypes->toArray() ]); $this->info('土地类型配置缓存重建完成'); } /** * 重建土地升级配置缓存 * * @return void */ private function rebuildLandUpgradeConfigCache() { $this->info('重建土地升级配置缓存...'); // 获取所有土地升级配置 $upgradeConfigs = FarmLandUpgradeConfig::all(); // 缓存所有升级配置 $this->cacheData($this->cachePrefix . 'land_upgrade_configs:all', $upgradeConfigs); // 按起始类型缓存 $configsByFromType = $upgradeConfigs->groupBy('from_type_id'); foreach ($configsByFromType as $fromTypeId => $configs) { $this->cacheData($this->cachePrefix . 'land_upgrade_configs:from:' . $fromTypeId, $configs); } // 按目标类型缓存 $configsByToType = $upgradeConfigs->groupBy('to_type_id'); foreach ($configsByToType as $toTypeId => $configs) { $this->cacheData($this->cachePrefix . 'land_upgrade_configs:to:' . $toTypeId, $configs); } // 生成JSON文件 $this->saveJsonToFile('farm_land_upgrade.json', [ 'generated_at' => now()->toDateTimeString(), 'upgrade_configs' => $upgradeConfigs->toArray() ]); $this->info('土地升级配置缓存重建完成'); } /** * 缓存数据并记录缓存键 * * @param string $key * @param mixed $data * @return void */ private function cacheData(string $key, $data) { // 缓存数据 Cache::put($key, $data, $this->cacheExpiration); // 记录缓存键 $keys = Cache::get($this->cachePrefix . 'keys', []); $keys[] = $key; Cache::put($this->cachePrefix . 'keys', array_unique($keys), $this->cacheExpiration); } /** * 将JSON数据保存到文件 * * @param string $filename 文件名 * @param array $data 要保存的数据 * @return bool 是否保存成功 */ private function saveJsonToFile(string $filename, array $data): bool { try { // 确保目录存在 $directory = 'public/json'; if (!file_exists($directory)) { mkdir($directory, 0755, true); } // 将数据保存为JSON文件 $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); $filePath = $directory . '/' . $filename; file_put_contents($filePath, $jsonContent); Log::info('Farm JSON file saved to: ' . $filePath); return true; } catch (\Exception $e) { Log::error('Save farm JSON to file failed: ' . $e->getMessage()); return false; } } }