1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 19:44:02 +08:00
course-tencent-cloud/app/Console/Tasks/SyncCommentCounterTask.php
2020-07-14 21:13:37 +08:00

90 lines
1.9 KiB
PHP

<?php
namespace App\Console\Tasks;
use App\Caches\CommentCounter as CommentCounterCache;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Repos\Comment as CommentRepo;
use App\Services\Syncer\CommentCounter as CommentCounterSyncer;
class SyncCommentCounterTask extends Task
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
public function mainAction()
{
$this->cache = $this->getDI()->get('cache');
$this->redis = $this->cache->getRedis();
$this->rebuild();
}
protected function rebuild()
{
$key = $this->getCacheKey();
$commentIds = $this->redis->sRandMember($key, 500);
if (!$commentIds) return;
$commentRepo = new CommentRepo();
$comments = $commentRepo->findByIds($commentIds);
if ($comments->count() == 0) {
return;
}
$counterCache = new CommentCounterCache();
$allowRecount = $this->allowRecount();
foreach ($comments as $comment) {
if ($allowRecount) {
$comment->reply_count = $commentRepo->countReplies($comment->id);
$comment->like_count = $commentRepo->countLikes($comment->id);
$comment->update();
$counterCache->rebuild($comment->id);
} else {
$counter = $counterCache->get($comment->id);
if ($counter) {
$comment->reply_count = $counter['reply_count'];
$comment->like_count = $counter['like_count'];
$comment->update();
}
}
}
$this->redis->sRem($key, ...$commentIds);
}
protected function getCacheKey()
{
$syncer = new CommentCounterSyncer();
return $syncer->getSyncKey();
}
protected function allowRecount()
{
return date('H') == 5;
}
}