mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-22 11:41:27 +08:00
94 lines
2.0 KiB
PHP
94 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Tasks;
|
|
|
|
use App\Caches\ConsultCounter as ConsultCounterCache;
|
|
use App\Library\Cache\Backend\Redis as RedisCache;
|
|
use App\Repos\Consult as ConsultRepo;
|
|
use App\Services\Syncer\ConsultCounter as ConsultCounterSyncer;
|
|
|
|
class SyncConsultCounterTask 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();
|
|
|
|
$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;
|
|
}
|
|
|
|
}
|