1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-25 12:09:09 +08:00

优化相关文章和问题

This commit is contained in:
xiaochong0302 2023-12-12 17:18:24 +08:00
parent 3ae0d0df82
commit 7a8d8ba342
5 changed files with 38 additions and 14 deletions

View File

@ -13,7 +13,7 @@ use App\Repos\Article as ArticleRepo;
class TaggedArticleList extends Cache class TaggedArticleList extends Cache
{ {
protected $limit = 5; protected $limit = 15;
protected $lifetime = 3600; protected $lifetime = 3600;

View File

@ -13,7 +13,7 @@ use App\Repos\Question as QuestionRepo;
class TaggedQuestionList extends Cache class TaggedQuestionList extends Cache
{ {
protected $limit = 5; protected $limit = 15;
protected $lifetime = 3600; protected $lifetime = 3600;

View File

@ -35,7 +35,7 @@
<style> <style>
html { html {
height: 95%; height: 100%;
} }
body { body {
@ -72,7 +72,7 @@
</script> </script>
<script> <script>
jquery('body').buoyant({ $('body').buoyant({
elementClass: 'circles', elementClass: 'circles',
numberOfItems: 20, numberOfItems: 20,
minRadius: 5, minRadius: 5,

View File

@ -18,21 +18,33 @@ class RelatedArticleList extends LogicService
public function handle($id) public function handle($id)
{ {
$limit = $this->request->getQuery('limit', 'int', 5);
$article = $this->checkArticle($id); $article = $this->checkArticle($id);
if (empty($article->tags)) return []; if (empty($article->tags)) return [];
$tagIds = kg_array_column($article->tags, 'id'); $tagIds = kg_array_column($article->tags, 'id');
$randKey = array_rand($tagIds); $tagId = kg_array_rand($tagIds);
$tagId = $tagIds[$randKey];
$cache = new TaggedArticleListCache(); $cache = new TaggedArticleListCache();
$result = $cache->get($tagId); $articles = $cache->get($tagId);
return $result ?: []; if (empty($articles)) return [];
foreach ($articles as $key => $article) {
if ($article['id'] == $id) {
unset($articles[$key]);
}
}
if ($limit < count($articles)) {
$articles = array_slice($articles, $limit);
}
return $articles;
} }
} }

View File

@ -18,21 +18,33 @@ class RelatedQuestionList extends LogicService
public function handle($id) public function handle($id)
{ {
$limit = $this->request->getQuery('limit', 'int', 5);
$question = $this->checkQuestion($id); $question = $this->checkQuestion($id);
if (empty($question->tags)) return []; if (empty($question->tags)) return [];
$tagIds = kg_array_column($question->tags, 'id'); $tagIds = kg_array_column($question->tags, 'id');
$randKey = array_rand($tagIds); $tagId = kg_array_rand($tagIds);
$tagId = $tagIds[$randKey];
$cache = new TaggedQuestionListCache(); $cache = new TaggedQuestionListCache();
$result = $cache->get($tagId); $questions = $cache->get($tagId);
return $result ?: []; if (empty($questions)) return [];
foreach ($questions as $key => $question) {
if ($question['id'] == $id) {
unset($questions[$key]);
}
}
if ($limit < count($questions)) {
$questions = array_slice($questions, $limit);
}
return $questions;
} }
} }