1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 11:41:27 +08:00
course-tencent-cloud/app/Console/Tasks/SyncCourseCounterTask.php
2020-05-27 18:25:42 +08:00

106 lines
2.7 KiB
PHP

<?php
namespace App\Console\Tasks;
use App\Caches\Course as CourseCache;
use App\Caches\CourseCounter as CourseCounterCache;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Repos\Course as CourseRepo;
use App\Services\Syncer\CourseCounter as CourseCounterSyncer;
class SyncCourseCounterTask 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();
$courseIds = $this->redis->sRandMember($key, 100);
if (!$courseIds) return;
$courseRepo = new CourseRepo();
$courses = $courseRepo->findByIds($courseIds);
if ($courses->count() == 0) {
return;
}
$counterCache = new CourseCounterCache();
$courseCache = new CourseCache();
$hour = date('H');
$recount = $this->checkEnableRecount();
foreach ($courses as $course) {
if ($recount && $hour % 3 == 0) {
$course->user_count = $courseRepo->countUsers($course->id);
$course->lesson_count = $courseRepo->countLessons($course->id);
$course->comment_count = $courseRepo->countComments($course->id);
$course->consult_count = $courseRepo->countConsults($course->id);
$course->review_count = $courseRepo->countReviews($course->id);
$course->favorite_count = $courseRepo->countFavorites($course->id);
$course->update();
$counterCache->rebuild($course->id);
$courseCache->rebuild($course->id);
} else {
$counter = $counterCache->get($course->id);
if ($counter) {
$course->user_count = $counter['user_count'];
$course->lesson_count = $counter['lesson_count'];
$course->comment_count = $counter['comment_count'];
$course->consult_count = $counter['consult_count'];
$course->review_count = $counter['review_count'];
$course->favorite_count = $counter['favorite_count'];
$course->update();
}
}
}
$this->redis->sRem($key, ...$courseIds);
}
protected function getCacheKey()
{
$syncer = new CourseCounterSyncer();
return $syncer->getSyncKey();
}
protected function checkEnableRecount()
{
$config = $this->getDI()->get('config');
return $config['recount_course'] ?? false;
}
}