UrsTestRelationCacheCommand.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Test\TestRelationCacheScript;
  4. use Illuminate\Console\Command;
  5. /**
  6. * URS关系缓存测试命令
  7. */
  8. class UrsTestRelationCacheCommand extends Command
  9. {
  10. protected $signature = 'urs:test-relation-cache
  11. {action : 测试动作 (generate|check|batch|clear|stats)}
  12. {--user-id= : 指定URS用户ID}
  13. {--users= : 指定多个URS用户ID,用逗号分隔}';
  14. protected $description = 'URS关系缓存测试命令';
  15. public function handle()
  16. {
  17. $script = new TestRelationCacheScript();
  18. $action = $this->argument('action');
  19. switch ($action) {
  20. case 'generate':
  21. $userId = $this->option('user-id');
  22. if (!$userId) {
  23. $this->error('请使用 --user-id 指定URS用户ID');
  24. return 1;
  25. }
  26. $result = $script->testGenerateCacheForUser((int)$userId);
  27. $this->info('测试完成');
  28. break;
  29. case 'check':
  30. $result = $script->testCheckIntegrity();
  31. $this->info('检查完成');
  32. break;
  33. case 'batch':
  34. $users = $this->option('users');
  35. if (!$users) {
  36. $this->error('请使用 --users 指定URS用户ID列表,用逗号分隔');
  37. return 1;
  38. }
  39. $userIds = array_map('intval', explode(',', $users));
  40. $result = $script->testBatchGenerate($userIds);
  41. $this->info('批量测试完成');
  42. break;
  43. case 'clear':
  44. if ($this->confirm('确定要清理所有缓存数据吗?')) {
  45. $script->clearAllCache();
  46. $this->info('清理完成');
  47. } else {
  48. $this->info('操作已取消');
  49. }
  50. break;
  51. case 'stats':
  52. $stats = $script->showCacheStats();
  53. $this->info('统计完成');
  54. break;
  55. default:
  56. $this->error('无效的动作,支持的动作: generate, check, batch, clear, stats');
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. }