mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
74 lines
1.4 KiB
PHP
74 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Tasks;
|
|
|
|
use App\Caches\ReviewCounter as ReviewCounterCache;
|
|
use App\Library\Cache\Backend\Redis as RedisCache;
|
|
use App\Repos\Review as ReviewRepo;
|
|
use App\Services\Syncer\ReviewCounter as ReviewCounterSyncer;
|
|
|
|
class SyncReviewCounterTask 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();
|
|
|
|
$reviewIds = $this->redis->sRandMember($key, 500);
|
|
|
|
if (!$reviewIds) return;
|
|
|
|
$reviewRepo = new ReviewRepo();
|
|
|
|
$reviews = $reviewRepo->findByIds($reviewIds);
|
|
|
|
if ($reviews->count() == 0) {
|
|
return;
|
|
}
|
|
|
|
$cache = new ReviewCounterCache();
|
|
|
|
foreach ($reviews as $review) {
|
|
|
|
$counter = $cache->get($review->id);
|
|
|
|
if ($counter) {
|
|
|
|
$review->agree_count = $counter['agree_count'];
|
|
$review->oppose_count = $counter['oppose_count'];
|
|
|
|
$review->update();
|
|
}
|
|
}
|
|
|
|
$this->redis->sRem($key, ...$reviewIds);
|
|
}
|
|
|
|
protected function getCacheKey()
|
|
{
|
|
$syncer = new ReviewCounterSyncer();
|
|
|
|
return $syncer->getSyncKey();
|
|
}
|
|
|
|
}
|