CacheDictPreference.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\UserDict;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Tools\RedisClusters;
  7. class CacheDictPreference extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'cache:dict.preference';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '从第三方字典中提取首选项';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. if (\App\Tools\Tools::isStop()) {
  38. return 0;
  39. }
  40. $prefix = 'dict-preference';
  41. $words = UserDict::select(['word', 'language'])
  42. ->groupBy(['word', 'language'])
  43. ->cursor();
  44. $wordCount = DB::select('SELECT count(*) from (
  45. SELECT word,language from user_dicts group by word,language) T');
  46. $bar = $this->output->createProgressBar($wordCount[0]->count);
  47. $count = 0;
  48. foreach ($words as $key => $word) {
  49. $meaning = UserDict::where('word', $word->word)
  50. ->where('language', $word->language)
  51. ->where('source', '_PAPER_RICH_')
  52. ->whereNotNull('mean')
  53. ->value('mean');
  54. $meaning = trim($meaning, " $");
  55. if (!empty($meaning)) {
  56. $m = explode('$', $meaning);
  57. RedisClusters::put("{$prefix}/{$word->word}/{$word->language}", $m[0]);
  58. }
  59. $bar->advance();
  60. $count++;
  61. if ($count % 1000 === 0) {
  62. if (\App\Tools\Tools::isStop()) {
  63. return 0;
  64. }
  65. }
  66. }
  67. $bar->finish();
  68. return 0;
  69. }
  70. }