1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-18 01:58:25 +08:00
course-tencent-cloud/app/Console/Tasks/RebuildChapterCacheTask.php
2020-03-16 15:33:36 +08:00

74 lines
1.6 KiB
PHP

<?php
namespace App\Console\Tasks;
use App\Caches\Chapter as ChapterCache;
use App\Caches\ChapterCounter as ChapterCounterCache;
use App\Repos\Chapter as ChapterRepo;
use App\Services\ChapterCacheSyncer;
use Phalcon\Mvc\Model\Resultset;
class RebuildChapterCacheTask extends Task
{
/**
* @var \App\Library\Cache\Backend\Redis
*/
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();
$chapterIds = $this->redis->sRandMember($key, 500);
if (!$chapterIds) return;
$chapterRepo = new ChapterRepo();
/**
* @var Resultset $chapters
*/
$chapters = $chapterRepo->findByIds($chapterIds);
if ($chapters->count() == 0) {
return;
}
$chapterCache = new ChapterCache();
$counterCache = new ChapterCounterCache();
foreach ($chapters as $chapter) {
$chapter->user_count = $chapterRepo->countUsers($chapter->id);
$chapter->comment_count = $chapterRepo->countComments($chapter->id);
$chapter->like_count = $chapterRepo->countLikes($chapter->id);
$chapter->update();
$chapterCache->rebuild($chapter->id);
$counterCache->rebuild($chapter->id);
}
}
protected function getCacheKey()
{
$syncer = new ChapterCacheSyncer();
return $syncer->getCacheKey();
}
}