CacheDictPreference.php 2.0 KB

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