UrsRebuildRelationCacheCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Logics\UrsRelationCacheLogic;
  4. use Illuminate\Console\Command;
  5. /**
  6. * URS用户关系缓存重建命令
  7. *
  8. * 用于批量重建URS用户关系缓存,提升查询性能
  9. */
  10. class UrsRebuildRelationCacheCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. */
  15. protected $signature = 'urs:rebuild-relation-cache
  16. {--batch-size=100 : 批处理大小}
  17. {--check : 仅检查完整性,不重建}
  18. {--fix : 修复发现的问题}';
  19. /**
  20. * 命令描述
  21. */
  22. protected $description = 'URS用户关系缓存重建命令';
  23. /**
  24. * 执行命令
  25. */
  26. public function handle()
  27. {
  28. $logic = new UrsRelationCacheLogic();
  29. // 仅检查完整性
  30. if ($this->option('check')) {
  31. $this->info('开始检查URS关系缓存完整性...');
  32. $result = $logic->checkRelationCacheIntegrity();
  33. if (isset($result['error'])) {
  34. $this->error('检查失败: ' . $result['error']);
  35. return 1;
  36. }
  37. $this->info('检查结果:');
  38. $this->table(['项目', '数量'], [
  39. ['总用户数', $result['total_users']],
  40. ['有缓存的用户数', $result['users_with_cache']],
  41. ['缺失缓存的用户数', $result['missing_users']],
  42. ['循环推荐关系数', $result['circular_relations']],
  43. ['孤立缓存数', $result['orphaned_caches']]
  44. ]);
  45. if ($result['missing_users'] > 0 || $result['orphaned_caches'] > 0) {
  46. $this->warn('发现问题,建议使用 --fix 参数修复');
  47. return 1;
  48. }
  49. $this->info('缓存完整性检查通过');
  50. return 0;
  51. }
  52. // 修复问题
  53. if ($this->option('fix')) {
  54. $this->info('开始修复URS关系缓存问题...');
  55. $result = $logic->fixRelationCacheIssues();
  56. if (isset($result['error'])) {
  57. $this->error('修复失败: ' . $result['error']);
  58. return 1;
  59. }
  60. $this->info('修复结果:');
  61. $this->table(['项目', '修复数量'], [
  62. ['缺失用户缓存', $result['fixed']['missing_users']],
  63. ['孤立缓存清理', $result['fixed']['orphaned_caches']]
  64. ]);
  65. $this->info('问题修复完成');
  66. return 0;
  67. }
  68. // 重建所有缓存
  69. $batchSize = (int) $this->option('batch-size');
  70. if (!$this->confirm('确定要重建所有URS用户关系缓存吗?这将清空现有缓存并重新生成。')) {
  71. $this->info('操作已取消');
  72. return 0;
  73. }
  74. $this->info("开始重建URS关系缓存,批处理大小: {$batchSize}");
  75. $progressBar = $this->output->createProgressBar();
  76. $progressBar->start();
  77. $result = $logic->rebuildAllRelationCache($batchSize);
  78. $progressBar->finish();
  79. $this->newLine();
  80. if (isset($result['error'])) {
  81. $this->error('重建失败: ' . $result['error']);
  82. return 1;
  83. }
  84. $this->info('重建完成:');
  85. $this->table(['项目', '数量'], [
  86. ['总用户数', $result['total']],
  87. ['成功数', $result['success']],
  88. ['失败数', $result['fail']]
  89. ]);
  90. if ($result['fail'] > 0) {
  91. $this->warn('部分用户缓存生成失败,请检查日志');
  92. return 1;
  93. }
  94. $this->info('所有URS用户关系缓存重建完成');
  95. return 0;
  96. }
  97. }