mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 20:00:27 +08:00
62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Tasks;
|
|
|
|
use App\Models\Question as QuestionModel;
|
|
use App\Repos\Question as QuestionRepo;
|
|
use App\Services\Search\QuestionDocument;
|
|
use App\Services\Search\QuestionSearcher;
|
|
use App\Services\Sync\QuestionIndex as QuestionIndexSync;
|
|
|
|
class SyncQuestionIndexTask extends Task
|
|
{
|
|
|
|
public function mainAction()
|
|
{
|
|
$redis = $this->getRedis();
|
|
|
|
$key = $this->getSyncKey();
|
|
|
|
$questionIds = $redis->sRandMember($key, 1000);
|
|
|
|
if (!$questionIds) return;
|
|
|
|
$questionRepo = new QuestionRepo();
|
|
|
|
$questions = $questionRepo->findByIds($questionIds);
|
|
|
|
if ($questions->count() == 0) return;
|
|
|
|
$document = new QuestionDocument();
|
|
|
|
$handler = new QuestionSearcher();
|
|
|
|
$index = $handler->getXS()->getIndex();
|
|
|
|
$index->openBuffer();
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
$doc = $document->setDocument($question);
|
|
|
|
if ($question->published == QuestionModel::PUBLISH_APPROVED) {
|
|
$index->update($doc);
|
|
} else {
|
|
$index->del($question->id);
|
|
}
|
|
}
|
|
|
|
$index->closeBuffer();
|
|
|
|
$redis->sRem($key, ...$questionIds);
|
|
}
|
|
|
|
protected function getSyncKey()
|
|
{
|
|
$sync = new QuestionIndexSync();
|
|
|
|
return $sync->getSyncKey();
|
|
}
|
|
|
|
}
|