RebuildFarmCacheCommand.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Models\FarmHouseConfig;
  4. use App\Module\Farm\Models\FarmLandType;
  5. use App\Module\Farm\Models\FarmLandUpgradeConfig;
  6. use App\Module\Farm\Models\FarmSeed;
  7. use App\Module\Farm\Models\FarmSeedOutput;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * 重建农场缓存命令
  13. */
  14. class RebuildFarmCacheCommand extends Command
  15. {
  16. /**
  17. * 命令名称
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'farm:rebuild-cache';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '重建农场模块的缓存数据';
  28. /**
  29. * 缓存键前缀
  30. *
  31. * @var string
  32. */
  33. protected $cachePrefix = 'farm:';
  34. /**
  35. * 缓存过期时间(秒)
  36. *
  37. * @var int
  38. */
  39. protected $cacheExpiration = 86400; // 24小时
  40. /**
  41. * 执行命令
  42. *
  43. * @return int
  44. */
  45. public function handle()
  46. {
  47. $this->info('开始重建农场缓存...');
  48. try {
  49. // 清除所有农场相关缓存
  50. $this->clearCache();
  51. // 重建种子配置缓存
  52. $this->rebuildSeedCache();
  53. // 重建种子产出配置缓存
  54. $this->rebuildSeedOutputCache();
  55. // 重建房屋配置缓存
  56. $this->rebuildHouseConfigCache();
  57. // 重建土地类型配置缓存
  58. $this->rebuildLandTypeCache();
  59. // 重建土地升级配置缓存
  60. $this->rebuildLandUpgradeConfigCache();
  61. $this->info('农场缓存重建完成');
  62. $this->info('农场配置JSON文件已生成到 public/json/ 目录');
  63. Log::info('农场缓存重建成功');
  64. return 0;
  65. } catch (\Exception $e) {
  66. $this->error('农场缓存重建失败: ' . $e->getMessage());
  67. Log::error('农场缓存重建失败', [
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. return 1;
  72. }
  73. }
  74. /**
  75. * 清除所有农场相关缓存
  76. *
  77. * @return void
  78. */
  79. private function clearCache()
  80. {
  81. $this->info('清除现有缓存...');
  82. // 获取所有缓存键
  83. $keys = Cache::get($this->cachePrefix . 'keys', []);
  84. // 清除每个缓存
  85. foreach ($keys as $key) {
  86. Cache::forget($key);
  87. }
  88. // 清除缓存键列表
  89. Cache::forget($this->cachePrefix . 'keys');
  90. $this->info('现有缓存已清除');
  91. }
  92. /**
  93. * 重建种子配置缓存
  94. *
  95. * @return void
  96. */
  97. private function rebuildSeedCache()
  98. {
  99. $this->info('重建种子配置缓存...');
  100. // 获取所有种子
  101. $seeds = FarmSeed::all();
  102. // 缓存所有种子
  103. $this->cacheData($this->cachePrefix . 'seeds:all', $seeds);
  104. // 按类型缓存种子
  105. $seedsByType = $seeds->groupBy('type');
  106. foreach ($seedsByType as $type => $typeSeeds) {
  107. $this->cacheData($this->cachePrefix . 'seeds:type:' . $type, $typeSeeds);
  108. }
  109. // 单独缓存每个种子
  110. foreach ($seeds as $seed) {
  111. $this->cacheData($this->cachePrefix . 'seeds:id:' . $seed->id, $seed);
  112. }
  113. // 生成JSON文件
  114. $this->saveJsonToFile('farm_seed.json', [
  115. 'generated_at' => now()->toDateTimeString(),
  116. 'seeds' => $seeds->toArray()
  117. ]);
  118. $this->info('种子配置缓存重建完成');
  119. }
  120. /**
  121. * 重建种子产出配置缓存
  122. *
  123. * @return void
  124. */
  125. private function rebuildSeedOutputCache()
  126. {
  127. $this->info('重建种子产出配置缓存...');
  128. // 获取所有种子产出
  129. $seedOutputs = FarmSeedOutput::all();
  130. // 缓存所有种子产出
  131. $this->cacheData($this->cachePrefix . 'seed_outputs:all', $seedOutputs);
  132. // 按种子ID缓存产出
  133. $outputsBySeedId = $seedOutputs->groupBy('seed_id');
  134. foreach ($outputsBySeedId as $seedId => $outputs) {
  135. $this->cacheData($this->cachePrefix . 'seed_outputs:seed_id:' . $seedId, $outputs);
  136. }
  137. // 生成JSON文件
  138. $this->saveJsonToFile('farm_seed_output.json', [
  139. 'generated_at' => now()->toDateTimeString(),
  140. 'seed_outputs' => $seedOutputs->toArray()
  141. ]);
  142. $this->info('种子产出配置缓存重建完成');
  143. }
  144. /**
  145. * 重建房屋配置缓存
  146. *
  147. * @return void
  148. */
  149. private function rebuildHouseConfigCache()
  150. {
  151. $this->info('重建房屋配置缓存...');
  152. // 获取所有房屋配置
  153. $houseConfigs = FarmHouseConfig::all();
  154. // 缓存所有房屋配置
  155. $this->cacheData($this->cachePrefix . 'house_configs:all', $houseConfigs);
  156. // 按等级缓存房屋配置
  157. foreach ($houseConfigs as $config) {
  158. $this->cacheData($this->cachePrefix . 'house_configs:level:' . $config->level, $config);
  159. }
  160. // 生成JSON文件
  161. $this->saveJsonToFile('farm_house.json', [
  162. 'generated_at' => now()->toDateTimeString(),
  163. 'house_configs' => $houseConfigs->toArray()
  164. ]);
  165. $this->info('房屋配置缓存重建完成');
  166. }
  167. /**
  168. * 重建土地类型配置缓存
  169. *
  170. * @return void
  171. */
  172. private function rebuildLandTypeCache()
  173. {
  174. $this->info('重建土地类型配置缓存...');
  175. // 获取所有土地类型
  176. $landTypes = FarmLandType::all();
  177. // 缓存所有土地类型
  178. $this->cacheData($this->cachePrefix . 'land_types:all', $landTypes);
  179. // 按特殊类型缓存
  180. $specialTypes = $landTypes->where('is_special', true);
  181. $normalTypes = $landTypes->where('is_special', false);
  182. $this->cacheData($this->cachePrefix . 'land_types:special', $specialTypes);
  183. $this->cacheData($this->cachePrefix . 'land_types:normal', $normalTypes);
  184. // 单独缓存每个土地类型
  185. foreach ($landTypes as $type) {
  186. $this->cacheData($this->cachePrefix . 'land_types:id:' . $type->id, $type);
  187. }
  188. // 生成JSON文件
  189. $this->saveJsonToFile('farm_land_type.json', [
  190. 'generated_at' => now()->toDateTimeString(),
  191. 'land_types' => $landTypes->toArray()
  192. ]);
  193. $this->info('土地类型配置缓存重建完成');
  194. }
  195. /**
  196. * 重建土地升级配置缓存
  197. *
  198. * @return void
  199. */
  200. private function rebuildLandUpgradeConfigCache()
  201. {
  202. $this->info('重建土地升级配置缓存...');
  203. // 获取所有土地升级配置
  204. $upgradeConfigs = FarmLandUpgradeConfig::all();
  205. // 缓存所有升级配置
  206. $this->cacheData($this->cachePrefix . 'land_upgrade_configs:all', $upgradeConfigs);
  207. // 按起始类型缓存
  208. $configsByFromType = $upgradeConfigs->groupBy('from_type_id');
  209. foreach ($configsByFromType as $fromTypeId => $configs) {
  210. $this->cacheData($this->cachePrefix . 'land_upgrade_configs:from:' . $fromTypeId, $configs);
  211. }
  212. // 按目标类型缓存
  213. $configsByToType = $upgradeConfigs->groupBy('to_type_id');
  214. foreach ($configsByToType as $toTypeId => $configs) {
  215. $this->cacheData($this->cachePrefix . 'land_upgrade_configs:to:' . $toTypeId, $configs);
  216. }
  217. // 生成JSON文件
  218. $this->saveJsonToFile('farm_land_upgrade.json', [
  219. 'generated_at' => now()->toDateTimeString(),
  220. 'upgrade_configs' => $upgradeConfigs->toArray()
  221. ]);
  222. $this->info('土地升级配置缓存重建完成');
  223. }
  224. /**
  225. * 缓存数据并记录缓存键
  226. *
  227. * @param string $key
  228. * @param mixed $data
  229. * @return void
  230. */
  231. private function cacheData(string $key, $data)
  232. {
  233. // 缓存数据
  234. Cache::put($key, $data, $this->cacheExpiration);
  235. // 记录缓存键
  236. $keys = Cache::get($this->cachePrefix . 'keys', []);
  237. $keys[] = $key;
  238. Cache::put($this->cachePrefix . 'keys', array_unique($keys), $this->cacheExpiration);
  239. }
  240. /**
  241. * 将JSON数据保存到文件
  242. *
  243. * @param string $filename 文件名
  244. * @param array $data 要保存的数据
  245. * @return bool 是否保存成功
  246. */
  247. private function saveJsonToFile(string $filename, array $data): bool
  248. {
  249. try {
  250. // 确保目录存在
  251. $directory = 'public/json';
  252. if (!file_exists($directory)) {
  253. mkdir($directory, 0755, true);
  254. }
  255. // 将数据保存为JSON文件
  256. $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  257. $filePath = $directory . '/' . $filename;
  258. file_put_contents($filePath, $jsonContent);
  259. Log::info('Farm JSON file saved to: ' . $filePath);
  260. return true;
  261. } catch (\Exception $e) {
  262. Log::error('Save farm JSON to file failed: ' . $e->getMessage());
  263. return false;
  264. }
  265. }
  266. }