ClearEmbeddingsCache.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Services\OpenSearchService;
  5. class ClearEmbeddingsCache extends Command
  6. {
  7. /**
  8. * 命令名称
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'embeddings:clear {text? : 指定要清理的文本,不传则清理全部缓存}';
  13. /**
  14. * 命令描述
  15. *
  16. * @var string
  17. */
  18. protected $description = '清理 Redis 中的 embedding 缓存';
  19. /**
  20. * 执行命令
  21. */
  22. public function handle(OpenSearchService $service)
  23. {
  24. $text = $this->argument('text');
  25. if ($text) {
  26. $ok = $service->clearEmbeddingCache($text);
  27. if ($ok) {
  28. $this->info("已清理指定文本的缓存: \"{$text}\"");
  29. } else {
  30. $this->warn("缓存不存在: \"{$text}\"");
  31. }
  32. } else {
  33. $count = $service->clearAllEmbeddingCache();
  34. $this->info("已清理所有 embedding 缓存,共 {$count} 条");
  35. }
  36. return 0;
  37. }
  38. }