cache = $this->getDI()->get('cache'); $this->redis = $this->cache->getRedis(); $this->rebuild(); } protected function rebuild() { $key = $this->getCacheKey(); $consultIds = $this->redis->sRandMember($key, 500); if (!$consultIds) return; $consultRepo = new ConsultRepo(); $consults = $consultRepo->findByIds($consultIds); if ($consults->count() == 0) { return; } $counterCache = new ConsultCounterCache(); $allowRecount = $this->allowRecount(); foreach ($consults as $consult) { if ($allowRecount) { $consult->like_count = $consultRepo->countLikes($consult->id); $consult->update(); $counterCache->rebuild($consult->id); } else { $counter = $counterCache->get($consult->id); if ($counter) { $consult->like_count = $counter['like_count']; $consult->update(); } } } $this->redis->sRem($key, ...$consultIds); } protected function getCacheKey() { $syncer = new ConsultCounterSyncer(); return $syncer->getSyncKey(); } protected function allowRecount() { return date('H') % 4 == 0; } }