瀏覽代碼

话题使用数

wzq 5 年之前
父節點
當前提交
696fa6cf05

+ 64 - 0
app/Console/Commands/TopicUseCount.php

@@ -0,0 +1,64 @@
+<?php
+namespace App\Console\Commands;
+
+
+use App\Models\Topic;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class TopicUseCount extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:create_bean';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新话题使用数';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(Topic $topic)
+    {
+        parent::__construct();
+        $this->topic = $topic;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始更新话题使用数");
+
+        $key = 'topic.just_use_count';
+        $ids = Redis::ZRANGE($key, 0 , -1, 'WITHSCORES');
+        foreach($ids as $topicId => $val){
+            try{
+                $value = Redis::ZSCORE($key, $topicId);
+                Redis::ZREM ($key, $topicId);
+                $this->topic->where('post_id', $topicId)->increment('use_bean', $value);
+                Log::debug('更新更新话题使用数成功'.$topicId.'-'.$value);
+            }catch (\Exception $exception){
+                Log::error('更新更新话题使用数失败'.$topicId.'-'.$val.$exception->getMessage());
+            }
+            usleep(10000);
+        }
+
+
+        $this->line("更新内容生成U米结束");
+
+    }
+}

+ 82 - 0
app/Console/Commands/UpdateTopicUseCount.php

@@ -0,0 +1,82 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 11:50
+ */
+namespace App\Console\Commands;
+
+
+use App\Models\Post;
+use App\Models\PostCollect;
+use App\Models\PostDislike;
+use App\Models\PostLike;
+use App\Models\Topic;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class UpdateTopicUseCount extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:topic_use_count';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新话题使用数';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(Post $post,
+                                Topic $topic)
+    {
+        parent::__construct();
+        $this->post = $post;
+        $this->topic = $topic;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始更新话题使用数");
+        $topic = [];
+
+        $bar = $this->output->createProgressBar($this->post->withTrashed()->count());
+        $this->post->withTrashed()->chunk(100, function($posts) use ($bar, &$topic){
+            foreach($posts as $post) {
+                $topicIds = explode(',', $post->topic_ids);
+                foreach($topicIds as $id){
+                    if(isset($topic[$id])){
+                        $topic[$id] += 1;
+                    }else{
+                        $topic[$id] = 1;
+                    }
+                }
+                $bar->advance();
+            }
+
+            usleep(100000);
+        });
+        foreach($topic as $id => $useCount){
+            $this->topic->where('id', $id)->update(['use_count' => $useCount]);
+        }
+        $bar->finish();
+        $this->line("\n更新话题使用数结束");
+
+
+    }
+}

+ 4 - 0
app/Console/Kernel.php

@@ -10,9 +10,11 @@ use App\Console\Commands\PostCollectBean;
 use App\Console\Commands\PostCreateBean;
 use App\Console\Commands\PostStatistics;
 use App\Console\Commands\PostYesterday;
+use App\Console\Commands\TopicUseCount;
 use App\Console\Commands\UpdatePostInfo;
 use App\Console\Commands\UpdatePostStatus;
 use App\Console\Commands\UpdateReplyCount;
+use App\Console\Commands\UpdateTopicUseCount;
 use App\Console\Commands\VirusAdd;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@@ -35,6 +37,8 @@ class Kernel extends ConsoleKernel
         UpdatePostInfo::class,
         UpdatePostStatus::class,
         UpdateReplyCount::class,
+        UpdateTopicUseCount::class,
+        TopicUseCount::class,
         CommunityMemberStatistics::class,
         MusicImport::class
     ];

+ 5 - 0
app/Repositories/Post/PostRepository.php

@@ -179,6 +179,7 @@ class PostRepository
             Redis::zadd('post_trigger_type', 0, $post->id);
             foreach ($topicIdsArray as $id) {
                 Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
+                Redis::zincrby('topic.just_use_count', 1, $id);
             }
 
             $virus = $this->behavior->where('behavior_identification', 'publish')->first();
@@ -1125,6 +1126,10 @@ class PostRepository
 
             Redis::HSET('post_info_'.$request['id'], 'topic_ids', $topicIds);
 
+            foreach ($topicIdsArray as $id) {
+                Redis::zincrby('topic.just_use_count', 1, $id);
+            }
+
             return Response::create();
 
         } catch (QueryException $exception) {