2
0

CacheWbwPreference.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\WbwAnalysis;
  7. use Illuminate\Support\Facades\DB;
  8. class CacheWbwPreference extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'cache:wbw.preference {--editor=} {--view=all}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '逐词解析的首选项预热';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (\App\Tools\Tools::isStop()) {
  39. return 0;
  40. }
  41. $prefix = 'wbw-preference';
  42. if (
  43. $this->option('view') === 'all' ||
  44. $this->option('view') === 'my'
  45. ) {
  46. $this->info('个人数据');
  47. /**
  48. * 个人数据算法
  49. * 最新优先
  50. */
  51. $wbw = WbwAnalysis::select(['wbw_word', 'type', 'editor_id']);
  52. $wbwCount = DB::select('SELECT count(*) from (
  53. SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
  54. if ($this->option('editor')) {
  55. $wbw = $wbw->where('editor_id', $this->option('editor'));
  56. $wbwCount = DB::select(
  57. 'SELECT count(*) from (
  58. SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
  59. [$this->option('editor')]
  60. );
  61. }
  62. $wbw = $wbw->groupBy(['wbw_word', 'type', 'editor_id'])->cursor();
  63. $bar = $this->output->createProgressBar($wbwCount[0]->count);
  64. $count = 0;
  65. foreach ($wbw as $key => $value) {
  66. $data = WbwAnalysis::where('wbw_word', $value->wbw_word)
  67. ->where('type', $value->type)
  68. ->where('editor_id', $value->editor_id)
  69. ->orderBy('updated_at', 'desc')
  70. ->value('data');
  71. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}", $data);
  72. $bar->advance();
  73. $count++;
  74. if ($count % 1000 === 0) {
  75. if (\App\Tools\Tools::isStop()) {
  76. return 0;
  77. }
  78. }
  79. }
  80. $bar->finish();
  81. }
  82. if (
  83. $this->option('view') === 'all' ||
  84. $this->option('view') === 'community'
  85. ) {
  86. $this->info('社区通用');
  87. /**
  88. * 社区数据算法
  89. * 多的优先
  90. */
  91. $wbw = WbwAnalysis::select(['wbw_word', 'type']);
  92. $count = DB::select('SELECT count(*) from (
  93. SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
  94. $wbw = $wbw->groupBy(['wbw_word', 'type'])->cursor();
  95. $bar = $this->output->createProgressBar($count[0]->count);
  96. foreach ($wbw as $key => $value) {
  97. $data = WbwAnalysis::where('wbw_word', $value->wbw_word)
  98. ->where('type', $value->type)
  99. ->selectRaw('data,count(*)')
  100. ->groupBy("data")
  101. ->orderBy("count", "desc")
  102. ->first();
  103. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0", $data->data);
  104. $bar->advance();
  105. }
  106. $bar->finish();
  107. }
  108. return 0;
  109. }
  110. }