CacheWbwPreference.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. use App\Tools\RedisClusters;
  9. class CacheWbwPreference extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'cache:wbw.preference {--editor=} {--view=all}';
  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 = 'wbw-preference';
  43. if($this->option('view')==='all' ||
  44. $this->option('view')==='my'){
  45. $this->info('个人数据');
  46. /**
  47. * 个人数据算法
  48. * 最新优先
  49. */
  50. $wbw = WbwAnalysis::select(['wbw_word','type','editor_id']);
  51. $wbwCount = DB::select('SELECT count(*) from (
  52. SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
  53. if($this->option('editor')){
  54. $wbw = $wbw->where('editor_id',$this->option('editor'));
  55. $wbwCount = DB::select('SELECT count(*) from (
  56. SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
  57. [$this->option('editor')]);
  58. }
  59. $wbw = $wbw->groupBy(['wbw_word','type','editor_id'])->cursor();
  60. $bar = $this->output->createProgressBar($wbwCount[0]->count);
  61. $count = 0;
  62. foreach ($wbw as $key => $value) {
  63. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  64. ->where('type',$value->type)
  65. ->where('editor_id',$value->editor_id)
  66. ->orderBy('updated_at','desc')
  67. ->value('data');
  68. RedisClusters::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}",$data);
  69. $bar->advance();
  70. $count++;
  71. if($count%1000 === 0){
  72. if(\App\Tools\Tools::isStop()){
  73. return 0;
  74. }
  75. }
  76. }
  77. $bar->finish();
  78. }
  79. if($this->option('view')==='all' ||
  80. $this->option('view')==='community'
  81. ){
  82. $this->info('社区通用');
  83. /**
  84. * 社区数据算法
  85. * 多的优先
  86. */
  87. $wbw = WbwAnalysis::select(['wbw_word','type']);
  88. $count = DB::select('SELECT count(*) from (
  89. SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
  90. $wbw = $wbw->groupBy(['wbw_word','type'])->cursor();
  91. $bar = $this->output->createProgressBar($count[0]->count);
  92. foreach ($wbw as $key => $value) {
  93. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  94. ->where('type',$value->type)
  95. ->selectRaw('data,count(*)')
  96. ->groupBy("data")
  97. ->orderBy("count", "desc")
  98. ->first();
  99. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0",$data->data);
  100. $bar->advance();
  101. }
  102. $bar->finish();
  103. }
  104. return 0;
  105. }
  106. }