mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 11:58:41 +08:00
62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Tasks;
|
|
|
|
use App\Models\Article as ArticleModel;
|
|
use App\Repos\Article as ArticleRepo;
|
|
use App\Services\Search\ArticleDocument;
|
|
use App\Services\Search\ArticleSearcher;
|
|
use App\Services\Sync\ArticleIndex as ArticleIndexSync;
|
|
|
|
class SyncArticleIndexTask extends Task
|
|
{
|
|
|
|
public function mainAction()
|
|
{
|
|
$redis = $this->getRedis();
|
|
|
|
$key = $this->getSyncKey();
|
|
|
|
$articleIds = $redis->sRandMember($key, 1000);
|
|
|
|
if (!$articleIds) return;
|
|
|
|
$articleRepo = new ArticleRepo();
|
|
|
|
$articles = $articleRepo->findByIds($articleIds);
|
|
|
|
if ($articles->count() == 0) return;
|
|
|
|
$document = new ArticleDocument();
|
|
|
|
$handler = new ArticleSearcher();
|
|
|
|
$index = $handler->getXS()->getIndex();
|
|
|
|
$index->openBuffer();
|
|
|
|
foreach ($articles as $article) {
|
|
|
|
$doc = $document->setDocument($article);
|
|
|
|
if ($article->published == ArticleModel::PUBLISH_APPROVED) {
|
|
$index->update($doc);
|
|
} else {
|
|
$index->del($article->id);
|
|
}
|
|
}
|
|
|
|
$index->closeBuffer();
|
|
|
|
$redis->sRem($key, ...$articleIds);
|
|
}
|
|
|
|
protected function getSyncKey()
|
|
{
|
|
$sync = new ArticleIndexSync();
|
|
|
|
return $sync->getSyncKey();
|
|
}
|
|
|
|
}
|