Bladeren bron

删除内容回复新缓存

wzq 5 jaren geleden
bovenliggende
commit
788e76a35d
3 gewijzigde bestanden met toevoegingen van 76 en 1 verwijderingen
  1. 62 0
      app/Console/Commands/DelPostNewReply.php
  2. 2 0
      app/Console/Kernel.php
  3. 12 1
      app/Transformers/Post/CommentTransformer.php

+ 62 - 0
app/Console/Commands/DelPostNewReply.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 10:59
+ */
+namespace App\Console\Commands;
+
+
+use App\Models\PostComment;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class DelPostNewReply extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:del_post_new_reply';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '删除内容新回复缓存';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostComment $postComment)
+    {
+        parent::__construct();
+        $this->postComment = $postComment;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始删除内容新回复缓存");
+
+        $max = $this->postComment->max('id');
+        $bar = $this->output->createProgressBar($max);
+        for($i=1;$i<=$max;$i++){
+            Redis::DEL('post_new_reply_'.$i);
+            $bar->advance();
+            usleep(10000);
+        }
+        $bar->finish();
+        $this->line("\n删除内容新回复缓存结束");
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -7,6 +7,7 @@ use App\Console\Commands\BehaviorRecord;
 use App\Console\Commands\CalcPostWeight;
 use App\Console\Commands\ContentFeedCreate;
 use App\Console\Commands\ContentFeedDelete;
+use App\Console\Commands\DelPostNewReply;
 use App\Console\Commands\ExcellentResidents;
 use App\Console\Commands\RankingList;
 use Illuminate\Console\Scheduling\Schedule;
@@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
         ContentFeedCreate::class,
         ContentFeedDelete::class,
         ExcellentResidents::class,
+        DelPostNewReply::class,
         RankingList::class
     ];
 

+ 12 - 1
app/Transformers/Post/CommentTransformer.php

@@ -29,6 +29,7 @@ class CommentTransformer extends TransformerAbstract
             }
         }else{
             $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
+            $redisReply = [];
             foreach($replies as $val){
                 $userComment = $this->userInfo($val->uid);
                 $replyUsername = '';
@@ -45,8 +46,17 @@ class CommentTransformer extends TransformerAbstract
                     'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
                     'is_delete' => $val->is_delete,
                 ];
+                $redisReply[] = [
+                    'uid' => $val->uid,
+                    'username' => $userComment['username'],
+                    'avatar' => $userComment['avatar'],
+                    'reply_username' => $replyUsername,
+                    'content' => $val->is_delete?'该评论已被删除':$val->content,
+                    'created_at' => $val->created_at,
+                    'is_delete' => $val->is_delete,
+                ];
             }
-            Redis::SET($replyKey, json_encode($reply));
+            Redis::SET($replyKey, json_encode($redisReply));
             Redis::EXPIRE($replyKey, 604800);
         }
         $user = $this->userInfo($postComment['uid']);
@@ -59,6 +69,7 @@ class CommentTransformer extends TransformerAbstract
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'reply_count' => $postComment['reply_count'],
             'reply' => $reply,
+            'replys' => $reply,
             'is_delete' => $postComment['is_delete'],
         ];
     }