From f02d3220481f2d117e659f207869634131cf1dce Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sun, 5 Jan 2025 20:51:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=BF=87=E5=BA=A6=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E7=9A=84=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/Help.php | 36 ----------- app/Caches/HelpList.php | 97 ---------------------------- app/Caches/MaxAnswerId.php | 34 ---------- app/Caches/MaxHelpId.php | 34 ---------- app/Caches/MaxPageId.php | 34 ---------- app/Caches/MaxUploadId.php | 34 ---------- app/Caches/Page.php | 36 ----------- app/Http/Admin/Services/Help.php | 28 -------- app/Http/Admin/Services/Page.php | 16 ----- app/Models/Answer.php | 8 --- app/Models/Help.php | 8 --- app/Models/Page.php | 8 --- app/Models/Upload.php | 8 --- app/Services/Logic/Help/HelpList.php | 42 +++++++++++- app/Services/Logic/HelpTrait.php | 7 -- app/Services/Logic/PageTrait.php | 7 -- app/Validators/Answer.php | 17 ----- app/Validators/Help.php | 36 ----------- app/Validators/Page.php | 36 ----------- app/Validators/Resource.php | 7 -- app/Validators/Upload.php | 16 ----- 21 files changed, 39 insertions(+), 510 deletions(-) delete mode 100644 app/Caches/Help.php delete mode 100644 app/Caches/HelpList.php delete mode 100644 app/Caches/MaxAnswerId.php delete mode 100644 app/Caches/MaxHelpId.php delete mode 100644 app/Caches/MaxPageId.php delete mode 100644 app/Caches/MaxUploadId.php delete mode 100644 app/Caches/Page.php diff --git a/app/Caches/Help.php b/app/Caches/Help.php deleted file mode 100644 index bb16649c..00000000 --- a/app/Caches/Help.php +++ /dev/null @@ -1,36 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return "help:{$id}"; - } - - public function getContent($id = null) - { - $helpRepo = new HelpRepo(); - - $help = $helpRepo->findById($id); - - return $help ?: null; - } - -} diff --git a/app/Caches/HelpList.php b/app/Caches/HelpList.php deleted file mode 100644 index 5c30bea4..00000000 --- a/app/Caches/HelpList.php +++ /dev/null @@ -1,97 +0,0 @@ -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(); - } - -} diff --git a/app/Caches/MaxAnswerId.php b/app/Caches/MaxAnswerId.php deleted file mode 100644 index f10d5f01..00000000 --- a/app/Caches/MaxAnswerId.php +++ /dev/null @@ -1,34 +0,0 @@ -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; - } - -} diff --git a/app/Caches/MaxHelpId.php b/app/Caches/MaxHelpId.php deleted file mode 100644 index 75679247..00000000 --- a/app/Caches/MaxHelpId.php +++ /dev/null @@ -1,34 +0,0 @@ -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; - } - -} diff --git a/app/Caches/MaxPageId.php b/app/Caches/MaxPageId.php deleted file mode 100644 index 004faff2..00000000 --- a/app/Caches/MaxPageId.php +++ /dev/null @@ -1,34 +0,0 @@ -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; - } - -} diff --git a/app/Caches/MaxUploadId.php b/app/Caches/MaxUploadId.php deleted file mode 100644 index 2c623ecd..00000000 --- a/app/Caches/MaxUploadId.php +++ /dev/null @@ -1,34 +0,0 @@ -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; - } - -} diff --git a/app/Caches/Page.php b/app/Caches/Page.php deleted file mode 100644 index 9d2554c7..00000000 --- a/app/Caches/Page.php +++ /dev/null @@ -1,36 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return "page:{$id}"; - } - - public function getContent($id = null) - { - $pageRepo = new PageRepo(); - - $page = $pageRepo->findById($id); - - return $page ?: null; - } - -} diff --git a/app/Http/Admin/Services/Help.php b/app/Http/Admin/Services/Help.php index 4fb16999..0a681b20 100644 --- a/app/Http/Admin/Services/Help.php +++ b/app/Http/Admin/Services/Help.php @@ -8,8 +8,6 @@ namespace App\Http\Admin\Services; 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\Help as HelpModel; use App\Repos\Category as CategoryRepo; @@ -76,9 +74,6 @@ class Help extends Service $help->create($data); - $this->rebuildHelpCache($help); - $this->rebuildHelpListCache(); - return $help; } @@ -119,9 +114,6 @@ class Help extends Service $help->update($data); - $this->rebuildHelpCache($help); - $this->rebuildHelpListCache(); - return $help; } @@ -133,9 +125,6 @@ class Help extends Service $help->update(); - $this->rebuildHelpCache($help); - $this->rebuildHelpListCache(); - return $help; } @@ -147,9 +136,6 @@ class Help extends Service $help->update(); - $this->rebuildHelpCache($help); - $this->rebuildHelpListCache(); - return $help; } @@ -160,20 +146,6 @@ class Help extends Service 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 * @return array|object diff --git a/app/Http/Admin/Services/Page.php b/app/Http/Admin/Services/Page.php index 4684f8bb..3ed047c3 100644 --- a/app/Http/Admin/Services/Page.php +++ b/app/Http/Admin/Services/Page.php @@ -7,7 +7,6 @@ namespace App\Http\Admin\Services; -use App\Caches\Page as PageCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Page as PageModel; use App\Repos\Page as PageRepo; @@ -53,8 +52,6 @@ class Page extends Service $page->create($data); - $this->rebuildPageCache($page); - return $page; } @@ -96,8 +93,6 @@ class Page extends Service $page->update($data); - $this->rebuildPageCache($page); - return $page; } @@ -109,8 +104,6 @@ class Page extends Service $page->update(); - $this->rebuildPageCache($page); - return $page; } @@ -122,8 +115,6 @@ class Page extends Service $page->update(); - $this->rebuildPageCache($page); - return $page; } @@ -134,11 +125,4 @@ class Page extends Service return $validator->checkPage($id); } - protected function rebuildPageCache(PageModel $page) - { - $cache = new PageCache(); - - $cache->rebuild($page->id); - } - } diff --git a/app/Models/Answer.php b/app/Models/Answer.php index 0fd88c51..49e97aad 100644 --- a/app/Models/Answer.php +++ b/app/Models/Answer.php @@ -7,7 +7,6 @@ namespace App\Models; -use App\Caches\MaxAnswerId as MaxAnswerIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Answer extends Model @@ -166,13 +165,6 @@ class Answer extends Model $this->update_time = time(); } - public function afterCreate() - { - $cache = new MaxAnswerIdCache(); - - $cache->rebuild(); - } - public static function publishTypes() { return [ diff --git a/app/Models/Help.php b/app/Models/Help.php index 236c1732..35197a18 100644 --- a/app/Models/Help.php +++ b/app/Models/Help.php @@ -7,7 +7,6 @@ namespace App\Models; -use App\Caches\MaxHelpId as MaxHelpIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Help extends Model @@ -117,11 +116,4 @@ class Help extends Model $this->update_time = time(); } - public function afterCreate() - { - $cache = new MaxHelpIdCache(); - - $cache->rebuild(); - } - } diff --git a/app/Models/Page.php b/app/Models/Page.php index 31ac25d8..4493991e 100644 --- a/app/Models/Page.php +++ b/app/Models/Page.php @@ -7,7 +7,6 @@ namespace App\Models; -use App\Caches\MaxPageId as MaxPageIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Page extends Model @@ -110,11 +109,4 @@ class Page extends Model $this->update_time = time(); } - public function afterCreate() - { - $cache = new MaxPageIdCache(); - - $cache->rebuild(); - } - } diff --git a/app/Models/Upload.php b/app/Models/Upload.php index 33a09b25..98db82ad 100644 --- a/app/Models/Upload.php +++ b/app/Models/Upload.php @@ -7,7 +7,6 @@ namespace App\Models; -use App\Caches\MaxUploadId as MaxUploadIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Upload extends Model @@ -122,11 +121,4 @@ class Upload extends Model $this->update_time = time(); } - public function afterCreate() - { - $cache = new MaxUploadIdCache(); - - $cache->rebuild(); - } - } diff --git a/app/Services/Logic/Help/HelpList.php b/app/Services/Logic/Help/HelpList.php index e9b341aa..f20e6300 100644 --- a/app/Services/Logic/Help/HelpList.php +++ b/app/Services/Logic/Help/HelpList.php @@ -7,7 +7,9 @@ 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; class HelpList extends LogicService @@ -15,9 +17,43 @@ class HelpList extends LogicService 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; } } diff --git a/app/Services/Logic/HelpTrait.php b/app/Services/Logic/HelpTrait.php index da8b73e4..328104f9 100644 --- a/app/Services/Logic/HelpTrait.php +++ b/app/Services/Logic/HelpTrait.php @@ -19,11 +19,4 @@ trait HelpTrait return $validator->checkHelp($id); } - public function checkHelpCache($id) - { - $validator = new HelpValidator(); - - return $validator->checkHelpCache($id); - } - } diff --git a/app/Services/Logic/PageTrait.php b/app/Services/Logic/PageTrait.php index ffc6330e..23b1351d 100644 --- a/app/Services/Logic/PageTrait.php +++ b/app/Services/Logic/PageTrait.php @@ -19,11 +19,4 @@ trait PageTrait return $validator->checkPage($id); } - public function checkPageCache($id) - { - $validator = new PageValidator(); - - return $validator->checkPageCache($id); - } - } diff --git a/app/Validators/Answer.php b/app/Validators/Answer.php index 73cfe297..b71a5dd5 100644 --- a/app/Validators/Answer.php +++ b/app/Validators/Answer.php @@ -7,11 +7,9 @@ namespace App\Validators; -use App\Caches\MaxAnswerId as MaxAnswerIdCache; use App\Exceptions\BadRequest as BadRequestException; use App\Models\Answer as AnswerModel; use App\Models\Question as QuestionModel; -use App\Models\Reason as ReasonModel; use App\Models\User as UserModel; use App\Repos\Answer as AnswerRepo; use App\Repos\Question as QuestionRepo; @@ -22,8 +20,6 @@ class Answer extends Validator public function checkAnswer($id) { - $this->checkId($id); - $answerRepo = new AnswerRepo(); $answer = $answerRepo->findById($id); @@ -35,19 +31,6 @@ class Answer extends Validator 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) { $validator = new Question(); diff --git a/app/Validators/Help.php b/app/Validators/Help.php index a1356f5a..63b715f7 100644 --- a/app/Validators/Help.php +++ b/app/Validators/Help.php @@ -7,36 +7,13 @@ namespace App\Validators; -use App\Caches\Help as HelpCache; -use App\Caches\MaxHelpId as MaxHelpIdCache; use App\Exceptions\BadRequest as BadRequestException; -use App\Models\Help as HelpModel; use App\Repos\Help as HelpRepo; use App\Services\EditorStorage as EditorStorageService; 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) { $helpRepo = new HelpRepo(); @@ -50,19 +27,6 @@ class Help extends Validator 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) { $validator = new Category(); diff --git a/app/Validators/Page.php b/app/Validators/Page.php index 7bf04814..bd9740f0 100644 --- a/app/Validators/Page.php +++ b/app/Validators/Page.php @@ -7,37 +7,14 @@ namespace App\Validators; -use App\Caches\MaxPageId as MaxPageIdCache; -use App\Caches\Page as PageCache; use App\Exceptions\BadRequest as BadRequestException; use App\Library\Validators\Common as CommonValidator; -use App\Models\Page as PageModel; use App\Repos\Page as PageRepo; use App\Services\EditorStorage as EditorStorageService; 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) { $pageRepo = new PageRepo(); @@ -55,19 +32,6 @@ class Page extends Validator 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) { $value = $this->filter->sanitize($title, ['trim', 'string']); diff --git a/app/Validators/Resource.php b/app/Validators/Resource.php index 1dde9981..ee02329c 100644 --- a/app/Validators/Resource.php +++ b/app/Validators/Resource.php @@ -33,13 +33,6 @@ class Resource extends Validator return $validator->checkCourse($id); } - public function checkChapter($id) - { - $validator = new Chapter(); - - return $validator->checkChapter($id); - } - public function checkUpload($id) { $validator = new Upload(); diff --git a/app/Validators/Upload.php b/app/Validators/Upload.php index e7530738..257a9e88 100644 --- a/app/Validators/Upload.php +++ b/app/Validators/Upload.php @@ -7,7 +7,6 @@ namespace App\Validators; -use App\Caches\MaxUploadId as MaxUploadIdCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Upload as UploadRepo; @@ -16,8 +15,6 @@ class Upload extends Validator public function checkUpload($id) { - $this->checkId($id); - $uploadRepo = new UploadRepo(); $upload = $uploadRepo->findById($id); @@ -29,19 +26,6 @@ class Upload extends Validator 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) { $value = $this->filter->sanitize($name, ['trim', 'string']);