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(); $hour = date('H'); $recount = $this->checkEnableRecount(); foreach ($consults as $consult) { if ($recount && $hour % 3 == 0) { $consult->agree_count = $consultRepo->countAgrees($consult->id); $consult->oppose_count = $consultRepo->countOpposes($consult->id); $consult->update(); $counterCache->rebuild($consult->id); } else { $counter = $counterCache->get($consult->id); if ($counter) { $consult->agree_count = $counter['agree_count']; $consult->oppose_count = $counter['oppose_count']; $consult->update(); } } } $this->redis->sRem($key, ...$consultIds); } protected function getCacheKey() { $syncer = new ConsultCounterSyncer(); return $syncer->getSyncKey(); } protected function checkEnableRecount() { $config = $this->getDI()->get('config'); return $config['recount_consult'] ?? false; } }