| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Models\FarmHouseConfig;
- use App\Module\Farm\Models\FarmLandType;
- use App\Module\Farm\Models\FarmLandUpgradeConfig;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Models\FarmSeedOutput;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- /**
- * 重建农场缓存命令
- */
- class RebuildFarmCacheCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:rebuild-cache';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '重建农场模块的缓存数据';
- /**
- * 缓存键前缀
- *
- * @var string
- */
- protected $cachePrefix = 'farm:';
- /**
- * 缓存过期时间(秒)
- *
- * @var int
- */
- protected $cacheExpiration = 86400; // 24小时
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->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;
- }
- }
- }
|