1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 19:44:02 +08:00
course-tencent-cloud/app/Console/Tasks/SyncUserIndexTask.php
2020-09-17 21:29:29 +08:00

63 lines
1.2 KiB
PHP

<?php
namespace App\Console\Tasks;
use App\Repos\User as UserRepo;
use App\Services\Search\UserDocument;
use App\Services\Search\UserSearcher;
use App\Services\Sync\UserIndex as UserIndexSync;
class SyncUserIndexTask extends Task
{
public function mainAction()
{
$cache = $this->getCache();
$redis = $this->getRedis();
$key = $this->getSyncKey();
$userIds = $redis->sRandMember($key, 1000);
if (!$userIds) return;
$userRepo = new UserRepo();
$users = $userRepo->findByIds($userIds);
if ($users->count() == 0) return;
$document = new UserDocument();
$handler = new UserSearcher();
$index = $handler->getXS()->getIndex();
$index->openBuffer();
foreach ($users as $user) {
$doc = $document->setDocument($user);
if ($user->deleted == 0) {
$index->update($doc);
} else {
$index->del($user->id);
}
}
$index->closeBuffer();
$redis->sRem($key, ...$userIds);
}
protected function getSyncKey()
{
$sync = new UserIndexSync();
return $sync->getSyncKey();
}
}