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; } $courseCounterCache = new CourseCounterCache(); $courseCache = new CourseCache(); foreach ($courses as $course) { $counter = $courseCounterCache->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(); $courseCache->rebuild($course->id); } } $this->redis->sRem($key, ...$courseIds); } protected function getCacheKey() { $syncer = new CourseCounterSyncer(); return $syncer->getSyncKey(); } }