1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-03 23:46:49 +08:00

去除过度设计的缓存

This commit is contained in:
xiaochong0302 2025-01-05 20:51:54 +08:00
parent 09723a9d34
commit 9d210deedf
21 changed files with 39 additions and 510 deletions

View File

@ -1,36 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Repos\Help as HelpRepo;
class Help extends Cache
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "help:{$id}";
}
public function getContent($id = null)
{
$helpRepo = new HelpRepo();
$help = $helpRepo->findById($id);
return $help ?: null;
}
}

View File

@ -1,97 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\Category as CategoryModel;
use App\Models\Help as HelpModel;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\ResultsetInterface;
class HelpList extends Cache
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return 'help_list';
}
public function getContent($id = null)
{
$categories = $this->findCategories();
if ($categories->count() == 0) {
return [];
}
$result = [];
foreach ($categories as $category) {
$item = [];
$item['category'] = [
'id' => $category->id,
'name' => $category->name,
];
$item['helps'] = [];
$helps = $this->findHelps($category->id);
if ($helps->count() > 0) {
foreach ($helps as $help) {
$item['helps'][] = [
'id' => $help->id,
'title' => $help->title,
];
}
}
$result[] = $item;
}
return $result;
}
/**
* @return ResultsetInterface|Resultset|CategoryModel[]
*/
protected function findCategories()
{
return CategoryModel::query()
->where('type = :type:', ['type' => CategoryModel::TYPE_HELP])
->andWhere('level = 1')
->andWhere('published = 1')
->andWhere('deleted = 0')
->orderBy('priority ASC')
->execute();
}
/**
* @param int $categoryId
* @return ResultsetInterface|Resultset|CategoryModel[]
*/
protected function findHelps($categoryId)
{
return HelpModel::query()
->where('category_id = :category_id:', ['category_id' => $categoryId])
->andWhere('published = 1')
->andWhere('deleted = 0')
->orderBy('priority ASC')
->execute();
}
}

View File

@ -1,34 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\Answer as AnswerModel;
class MaxAnswerId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return 'max_answer_id';
}
public function getContent($id = null)
{
$answer = AnswerModel::findFirst(['order' => 'id DESC']);
return $answer->id ?? 0;
}
}

View File

@ -1,34 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\Help as HelpModel;
class MaxHelpId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return 'max_help_id';
}
public function getContent($id = null)
{
$help = HelpModel::findFirst(['order' => 'id DESC']);
return $help->id ?? 0;
}
}

View File

@ -1,34 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\Page as PageModel;
class MaxPageId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return 'max_page_id';
}
public function getContent($id = null)
{
$page = PageModel::findFirst(['order' => 'id DESC']);
return $page->id ?? 0;
}
}

View File

@ -1,34 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\Upload as UploadModel;
class MaxUploadId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return 'max_upload_id';
}
public function getContent($id = null)
{
$upload = UploadModel::findFirst(['order' => 'id DESC']);
return $upload->id ?? 0;
}
}

View File

@ -1,36 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Repos\Page as PageRepo;
class Page extends Cache
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "page:{$id}";
}
public function getContent($id = null)
{
$pageRepo = new PageRepo();
$page = $pageRepo->findById($id);
return $page ?: null;
}
}

View File

@ -8,8 +8,6 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Builders\HelpList as HelpListBuilder; use App\Builders\HelpList as HelpListBuilder;
use App\Caches\Help as HelpCache;
use App\Caches\HelpList as HelpListCache;
use App\Models\Category as CategoryModel; use App\Models\Category as CategoryModel;
use App\Models\Help as HelpModel; use App\Models\Help as HelpModel;
use App\Repos\Category as CategoryRepo; use App\Repos\Category as CategoryRepo;
@ -76,9 +74,6 @@ class Help extends Service
$help->create($data); $help->create($data);
$this->rebuildHelpCache($help);
$this->rebuildHelpListCache();
return $help; return $help;
} }
@ -119,9 +114,6 @@ class Help extends Service
$help->update($data); $help->update($data);
$this->rebuildHelpCache($help);
$this->rebuildHelpListCache();
return $help; return $help;
} }
@ -133,9 +125,6 @@ class Help extends Service
$help->update(); $help->update();
$this->rebuildHelpCache($help);
$this->rebuildHelpListCache();
return $help; return $help;
} }
@ -147,9 +136,6 @@ class Help extends Service
$help->update(); $help->update();
$this->rebuildHelpCache($help);
$this->rebuildHelpListCache();
return $help; return $help;
} }
@ -160,20 +146,6 @@ class Help extends Service
return $validator->checkHelp($id); return $validator->checkHelp($id);
} }
protected function rebuildHelpCache(HelpModel $help)
{
$cache = new HelpCache();
$cache->rebuild($help->id);
}
protected function rebuildHelpListCache()
{
$cache = new HelpListCache();
$cache->rebuild();
}
/** /**
* @param Resultset $helps * @param Resultset $helps
* @return array|object * @return array|object

View File

@ -7,7 +7,6 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Caches\Page as PageCache;
use App\Library\Paginator\Query as PagerQuery; use App\Library\Paginator\Query as PagerQuery;
use App\Models\Page as PageModel; use App\Models\Page as PageModel;
use App\Repos\Page as PageRepo; use App\Repos\Page as PageRepo;
@ -53,8 +52,6 @@ class Page extends Service
$page->create($data); $page->create($data);
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -96,8 +93,6 @@ class Page extends Service
$page->update($data); $page->update($data);
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -109,8 +104,6 @@ class Page extends Service
$page->update(); $page->update();
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -122,8 +115,6 @@ class Page extends Service
$page->update(); $page->update();
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -134,11 +125,4 @@ class Page extends Service
return $validator->checkPage($id); return $validator->checkPage($id);
} }
protected function rebuildPageCache(PageModel $page)
{
$cache = new PageCache();
$cache->rebuild($page->id);
}
} }

View File

@ -7,7 +7,6 @@
namespace App\Models; namespace App\Models;
use App\Caches\MaxAnswerId as MaxAnswerIdCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete; use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Answer extends Model class Answer extends Model
@ -166,13 +165,6 @@ class Answer extends Model
$this->update_time = time(); $this->update_time = time();
} }
public function afterCreate()
{
$cache = new MaxAnswerIdCache();
$cache->rebuild();
}
public static function publishTypes() public static function publishTypes()
{ {
return [ return [

View File

@ -7,7 +7,6 @@
namespace App\Models; namespace App\Models;
use App\Caches\MaxHelpId as MaxHelpIdCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete; use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Help extends Model class Help extends Model
@ -117,11 +116,4 @@ class Help extends Model
$this->update_time = time(); $this->update_time = time();
} }
public function afterCreate()
{
$cache = new MaxHelpIdCache();
$cache->rebuild();
}
} }

View File

@ -7,7 +7,6 @@
namespace App\Models; namespace App\Models;
use App\Caches\MaxPageId as MaxPageIdCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete; use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Page extends Model class Page extends Model
@ -110,11 +109,4 @@ class Page extends Model
$this->update_time = time(); $this->update_time = time();
} }
public function afterCreate()
{
$cache = new MaxPageIdCache();
$cache->rebuild();
}
} }

View File

@ -7,7 +7,6 @@
namespace App\Models; namespace App\Models;
use App\Caches\MaxUploadId as MaxUploadIdCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete; use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Upload extends Model class Upload extends Model
@ -122,11 +121,4 @@ class Upload extends Model
$this->update_time = time(); $this->update_time = time();
} }
public function afterCreate()
{
$cache = new MaxUploadIdCache();
$cache->rebuild();
}
} }

View File

@ -7,7 +7,9 @@
namespace App\Services\Logic\Help; namespace App\Services\Logic\Help;
use App\Caches\HelpList as HelpListCache; use App\Caches\CategoryList as CategoryListCache;
use App\Models\Category as CategoryModel;
use App\Repos\Help as HelpRepo;
use App\Services\Logic\Service as LogicService; use App\Services\Logic\Service as LogicService;
class HelpList extends LogicService class HelpList extends LogicService
@ -15,9 +17,43 @@ class HelpList extends LogicService
public function handle() public function handle()
{ {
$cache = new HelpListCache(); $cache = new CategoryListCache();
return $cache->get(); $categories = $cache->get(CategoryModel::TYPE_HELP);
$helpRepo = new HelpRepo();
$helps = $helpRepo->findAll([
'published' => 1,
'deleted' => 0,
]);
$result = [];
foreach ($categories as $category) {
$item = [];
$item['category'] = [
'id' => $category['id'],
'name' => $category['name'],
];
$item['helps'] = [];
if ($helps->count() > 0) {
foreach ($helps as $help) {
$item['helps'][] = [
'id' => $help->id,
'title' => $help->title,
];
}
}
$result[] = $item;
}
return $result;
} }
} }

View File

@ -19,11 +19,4 @@ trait HelpTrait
return $validator->checkHelp($id); return $validator->checkHelp($id);
} }
public function checkHelpCache($id)
{
$validator = new HelpValidator();
return $validator->checkHelpCache($id);
}
} }

View File

@ -19,11 +19,4 @@ trait PageTrait
return $validator->checkPage($id); return $validator->checkPage($id);
} }
public function checkPageCache($id)
{
$validator = new PageValidator();
return $validator->checkPageCache($id);
}
} }

View File

@ -7,11 +7,9 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxAnswerId as MaxAnswerIdCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Models\Answer as AnswerModel; use App\Models\Answer as AnswerModel;
use App\Models\Question as QuestionModel; use App\Models\Question as QuestionModel;
use App\Models\Reason as ReasonModel;
use App\Models\User as UserModel; use App\Models\User as UserModel;
use App\Repos\Answer as AnswerRepo; use App\Repos\Answer as AnswerRepo;
use App\Repos\Question as QuestionRepo; use App\Repos\Question as QuestionRepo;
@ -22,8 +20,6 @@ class Answer extends Validator
public function checkAnswer($id) public function checkAnswer($id)
{ {
$this->checkId($id);
$answerRepo = new AnswerRepo(); $answerRepo = new AnswerRepo();
$answer = $answerRepo->findById($id); $answer = $answerRepo->findById($id);
@ -35,19 +31,6 @@ class Answer extends Validator
return $answer; return $answer;
} }
public function checkId($id)
{
$id = intval($id);
$maxIdCache = new MaxAnswerIdCache();
$maxId = $maxIdCache->get();
if ($id < 1 || $id > $maxId) {
throw new BadRequestException('answer.not_found');
}
}
public function checkQuestion($id) public function checkQuestion($id)
{ {
$validator = new Question(); $validator = new Question();

View File

@ -7,36 +7,13 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\Help as HelpCache;
use App\Caches\MaxHelpId as MaxHelpIdCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Models\Help as HelpModel;
use App\Repos\Help as HelpRepo; use App\Repos\Help as HelpRepo;
use App\Services\EditorStorage as EditorStorageService; use App\Services\EditorStorage as EditorStorageService;
class Help extends Validator class Help extends Validator
{ {
/**
* @param int $id
* @return HelpModel
* @throws BadRequestException
*/
public function checkHelpCache($id)
{
$this->checkId($id);
$helpCache = new HelpCache();
$help = $helpCache->get($id);
if (!$help) {
throw new BadRequestException('help.not_found');
}
return $help;
}
public function checkHelp($id) public function checkHelp($id)
{ {
$helpRepo = new HelpRepo(); $helpRepo = new HelpRepo();
@ -50,19 +27,6 @@ class Help extends Validator
return $help; return $help;
} }
public function checkId($id)
{
$id = intval($id);
$maxIdCache = new MaxHelpIdCache();
$maxId = $maxIdCache->get();
if ($id < 1 || $id > $maxId) {
throw new BadRequestException('help.not_found');
}
}
public function checkCategory($id) public function checkCategory($id)
{ {
$validator = new Category(); $validator = new Category();

View File

@ -7,37 +7,14 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxPageId as MaxPageIdCache;
use App\Caches\Page as PageCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Validators\Common as CommonValidator; use App\Library\Validators\Common as CommonValidator;
use App\Models\Page as PageModel;
use App\Repos\Page as PageRepo; use App\Repos\Page as PageRepo;
use App\Services\EditorStorage as EditorStorageService; use App\Services\EditorStorage as EditorStorageService;
class Page extends Validator class Page extends Validator
{ {
/**
* @param int $id
* @return PageModel
* @throws BadRequestException
*/
public function checkPageCache($id)
{
$this->checkId($id);
$pageCache = new PageCache();
$page = $pageCache->get($id);
if (!$page) {
throw new BadRequestException('page.not_found');
}
return $page;
}
public function checkPage($id) public function checkPage($id)
{ {
$pageRepo = new PageRepo(); $pageRepo = new PageRepo();
@ -55,19 +32,6 @@ class Page extends Validator
return $page; return $page;
} }
public function checkId($id)
{
$id = intval($id);
$maxIdCache = new MaxPageIdCache();
$maxId = $maxIdCache->get();
if ($id < 1 || $id > $maxId) {
throw new BadRequestException('page.not_found');
}
}
public function checkTitle($title) public function checkTitle($title)
{ {
$value = $this->filter->sanitize($title, ['trim', 'string']); $value = $this->filter->sanitize($title, ['trim', 'string']);

View File

@ -33,13 +33,6 @@ class Resource extends Validator
return $validator->checkCourse($id); return $validator->checkCourse($id);
} }
public function checkChapter($id)
{
$validator = new Chapter();
return $validator->checkChapter($id);
}
public function checkUpload($id) public function checkUpload($id)
{ {
$validator = new Upload(); $validator = new Upload();

View File

@ -7,7 +7,6 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxUploadId as MaxUploadIdCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Upload as UploadRepo; use App\Repos\Upload as UploadRepo;
@ -16,8 +15,6 @@ class Upload extends Validator
public function checkUpload($id) public function checkUpload($id)
{ {
$this->checkId($id);
$uploadRepo = new UploadRepo(); $uploadRepo = new UploadRepo();
$upload = $uploadRepo->findById($id); $upload = $uploadRepo->findById($id);
@ -29,19 +26,6 @@ class Upload extends Validator
return $upload; return $upload;
} }
public function checkId($id)
{
$id = intval($id);
$maxIdCache = new MaxUploadIdCache();
$maxId = $maxIdCache->get();
if ($id < 1 || $id > $maxId) {
throw new BadRequestException('upload.not_found');
}
}
public function checkName($name) public function checkName($name)
{ {
$value = $this->filter->sanitize($name, ['trim', 'string']); $value = $this->filter->sanitize($name, ['trim', 'string']);