visuddhinanda 2 năm trước cách đây
mục cha
commit
8f50d51d16
1 tập tin đã thay đổi với 106 bổ sung0 xóa
  1. 106 0
      app/Console/Commands/CacheWbwPreference.php

+ 106 - 0
app/Console/Commands/CacheWbwPreference.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Cache;
+use App\Models\WbwAnalysis;
+use Illuminate\Support\Facades\DB;
+
+class CacheWbwPreference extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'cache:wbw.preference {--editor=} {--view=all}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '逐词解析的首选项预热';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $prefix = 'wbw-preference';
+        if($this->option('view')==='all' ||
+           $this->option('view')==='my'){
+            $this->info('个人数据');
+            /**
+             * 个人数据算法
+             * 最新优先
+             */
+            $wbw = WbwAnalysis::select(['wbw_word','type','editor_id']);
+            $count = DB::select('SELECT count(*) from (
+                SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
+            if($this->option('editor')){
+                $wbw = $wbw->where('editor_id',$this->option('editor'));
+                $count = DB::select('SELECT count(*) from (
+                    SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
+                    [$this->option('editor')]);
+            }
+            $wbw = $wbw->groupBy(['wbw_word','type','editor_id'])->cursor();
+
+            $bar = $this->output->createProgressBar($count[0]->count);
+            foreach ($wbw as $key => $value) {
+                $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
+                                    ->where('type',$value->type)
+                                    ->where('editor_id',$value->editor_id)
+                                    ->orderBy('updated_at','desc')
+                                    ->value('data');
+                Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}",$data);
+                $bar->advance();
+            }
+            $bar->finish();
+        }
+
+        if($this->option('view')==='all' ||
+           $this->option('view')==='community'
+           ){
+            $this->info('社区通用');
+            /**
+             * 社区数据算法
+             * 多的优先
+             */
+            $wbw = WbwAnalysis::select(['wbw_word','type']);
+            $count = DB::select('SELECT count(*) from (
+                SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
+            $wbw = $wbw->groupBy(['wbw_word','type'])->cursor();
+
+            $bar = $this->output->createProgressBar($count[0]->count);
+            foreach ($wbw as $key => $value) {
+                $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
+                                    ->where('type',$value->type)
+                                    ->selectRaw('data,count(*)')
+                                    ->groupBy("data")
+                                    ->orderBy("count", "desc")
+                                    ->first();
+
+                Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0",$data->data);
+                $bar->advance();
+            }
+            $bar->finish();
+        }
+
+        return 0;
+    }
+}