diff --git a/app/Caches/Chapter.php b/app/Caches/Chapter.php index 2b8cb436..b280704d 100644 --- a/app/Caches/Chapter.php +++ b/app/Caches/Chapter.php @@ -7,7 +7,7 @@ use App\Repos\Chapter as ChapterRepo; class Chapter extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/ChapterCounter.php b/app/Caches/ChapterCounter.php index 3d928275..a07b39a7 100644 --- a/app/Caches/ChapterCounter.php +++ b/app/Caches/ChapterCounter.php @@ -25,6 +25,8 @@ class ChapterCounter extends Counter $chapter = $chapterRepo->findById($id); + if (!$chapter) return null; + return [ 'user_count' => $chapter->user_count, 'lesson_count' => $chapter->lesson_count, diff --git a/app/Caches/Course.php b/app/Caches/Course.php index b29c555f..6071c305 100644 --- a/app/Caches/Course.php +++ b/app/Caches/Course.php @@ -7,7 +7,7 @@ use App\Repos\Course as CourseRepo; class Course extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -25,11 +25,7 @@ class Course extends Cache $course = $courseRepo->findById($id); - if (!$course) return null; - - $course->cover = kg_ci_img_url($course->cover); - - return $course; + return $course ?: null; } } diff --git a/app/Caches/CourseCategoryList.php b/app/Caches/CourseCategoryList.php index fbaf2a0e..65ac5080 100644 --- a/app/Caches/CourseCategoryList.php +++ b/app/Caches/CourseCategoryList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseCategoryList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseChapterList.php b/app/Caches/CourseChapterList.php index 21203677..74f1082d 100644 --- a/app/Caches/CourseChapterList.php +++ b/app/Caches/CourseChapterList.php @@ -9,7 +9,7 @@ use Phalcon\Mvc\Model\Resultset; class CourseChapterList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseCounter.php b/app/Caches/CourseCounter.php index 8270ff3f..793cfc18 100644 --- a/app/Caches/CourseCounter.php +++ b/app/Caches/CourseCounter.php @@ -25,6 +25,8 @@ class CourseCounter extends Counter $course = $courseRepo->findById($id); + if (!$course) return null; + return [ 'user_count' => $course->user_count, 'lesson_count' => $course->lesson_count, diff --git a/app/Caches/CoursePackageList.php b/app/Caches/CoursePackageList.php index f3574e5f..3462825d 100644 --- a/app/Caches/CoursePackageList.php +++ b/app/Caches/CoursePackageList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CoursePackageList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseRelatedList.php b/app/Caches/CourseRelatedList.php index dd85a83c..9c04c5fb 100644 --- a/app/Caches/CourseRelatedList.php +++ b/app/Caches/CourseRelatedList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseRelatedList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -41,27 +41,18 @@ class CourseRelatedList extends Cache { $result = []; - $baseUrl = kg_ci_base_url(); - foreach ($courses as $course) { - $course->cover = $baseUrl . $course->cover; - $result[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'summary' => $course->summary, 'market_price' => (float)$course->market_price, 'vip_price' => (float)$course->vip_price, - 'rating' => (float)$course['rating'], - 'score' => (float)$course['score'], 'model' => $course->model, 'level' => $course->level, 'user_count' => $course->user_count, 'lesson_count' => $course->lesson_count, - 'review_count' => $course->review_count, - 'favorite_count' => $course->favorite_count, ]; } diff --git a/app/Caches/CourseTeacherList.php b/app/Caches/CourseTeacherList.php index 3302a866..051160cf 100644 --- a/app/Caches/CourseTeacherList.php +++ b/app/Caches/CourseTeacherList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseTeacherList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -41,12 +41,7 @@ class CourseTeacherList extends Cache { $result = []; - $baseUrl = kg_ci_base_url(); - foreach ($users as $user) { - - $user->avatar = $baseUrl . $user->avatar; - $result[] = [ 'id' => $user->id, 'name' => $user->name, diff --git a/app/Caches/CourseUser.php b/app/Caches/CourseUser.php index 1fedc9b4..433a9425 100644 --- a/app/Caches/CourseUser.php +++ b/app/Caches/CourseUser.php @@ -7,7 +7,7 @@ use App\Repos\CourseUser as CourseUserRepo; class CourseUser extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -33,11 +33,7 @@ class CourseUser extends Cache $courseUser = $courseUserRepo->findCourseUser($courseId, $userId); - if (!$courseUser) { - return new \stdClass(); - } - - return $courseUser; + return $courseUser ?: null; } } diff --git a/app/Caches/Help.php b/app/Caches/Help.php new file mode 100644 index 00000000..ecea52c5 --- /dev/null +++ b/app/Caches/Help.php @@ -0,0 +1,31 @@ +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/MaxHelpId.php b/app/Caches/MaxHelpId.php new file mode 100644 index 00000000..ae43642d --- /dev/null +++ b/app/Caches/MaxHelpId.php @@ -0,0 +1,29 @@ +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 new file mode 100644 index 00000000..cfed90f3 --- /dev/null +++ b/app/Caches/MaxPageId.php @@ -0,0 +1,29 @@ +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/Page.php b/app/Caches/Page.php new file mode 100644 index 00000000..2c9c31de --- /dev/null +++ b/app/Caches/Page.php @@ -0,0 +1,31 @@ +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/Category.php b/app/Http/Admin/Services/Category.php index faae26c5..193e2874 100644 --- a/app/Http/Admin/Services/Category.php +++ b/app/Http/Admin/Services/Category.php @@ -5,6 +5,7 @@ namespace App\Http\Admin\Services; use App\Caches\Category as CategoryCache; use App\Caches\CategoryList as CategoryListCache; use App\Caches\CategoryTreeList as CategoryTreeListCache; +use App\Caches\MaxCategoryId as MaxCategoryIdCache; use App\Models\Category as CategoryModel; use App\Repos\Category as CategoryRepo; use App\Validators\Category as CategoryValidator; @@ -172,23 +173,29 @@ class Category extends Service } $childCount = $categoryRepo->countChildCategories($category->id); + $category->child_count = $childCount; + $category->update(); } protected function rebuildCategoryCache(CategoryModel $category) { - $itemCache = new CategoryCache(); + $cache = new CategoryCache(); - $itemCache->rebuild($category->id); + $cache->rebuild($category->id); - $listCache = new CategoryListCache(); + $cache = new CategoryListCache(); - $listCache->rebuild(); + $cache->rebuild(); - $treeListCache = new CategoryTreeListCache(); + $cache = new CategoryTreeListCache(); - $treeListCache->rebuild(); + $cache->rebuild(); + + $cache = new MaxCategoryIdCache(); + + $cache->rebuild(); } protected function enableChildCategories($parentId) diff --git a/app/Http/Admin/Services/Chapter.php b/app/Http/Admin/Services/Chapter.php index 98cd2406..673fba5c 100644 --- a/app/Http/Admin/Services/Chapter.php +++ b/app/Http/Admin/Services/Chapter.php @@ -2,7 +2,9 @@ namespace App\Http\Admin\Services; +use App\Caches\Chapter as ChapterCache; use App\Caches\CourseChapterList as CourseChapterListCache; +use App\Caches\MaxChapterId as MaxChapterIdCache; use App\Models\Chapter as ChapterModel; use App\Models\Course as CourseModel; use App\Repos\Chapter as ChapterRepo; @@ -38,7 +40,9 @@ class Chapter extends Service $data = []; - $data['course_id'] = $validator->checkCourseId($post['course_id']); + $course = $validator->checkCourse($post['course_id']); + + $data['course_id'] = $course->id; $data['title'] = $validator->checkTitle($post['title']); $data['summary'] = $validator->checkSummary($post['summary']); $data['free'] = $validator->checkFreeStatus($post['free']); @@ -46,7 +50,8 @@ class Chapter extends Service $chapterRepo = new ChapterRepo(); if (isset($post['parent_id'])) { - $data['parent_id'] = $validator->checkParentId($post['parent_id']); + $parent = $validator->checkParent($post['parent_id']); + $data['parent_id'] = $parent->id; $data['priority'] = $chapterRepo->maxLessonPriority($post['parent_id']); } else { $data['priority'] = $chapterRepo->maxChapterPriority($post['course_id']); @@ -175,9 +180,17 @@ class Chapter extends Service protected function rebuildChapterCache(ChapterModel $chapter) { + $cache = new ChapterCache(); + + $cache->rebuild($chapter->id); + $cache = new CourseChapterListCache(); $cache->rebuild($chapter->course_id); + + $cache = new MaxChapterIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index 12b7c970..8881c642 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -3,9 +3,11 @@ namespace App\Http\Admin\Services; use App\Builders\CourseList as CourseListBuilder; +use App\Caches\Course as CourseCache; use App\Caches\CourseCategoryList as CourseCategoryListCache; use App\Caches\CourseRelatedList as CourseRelatedListCache; use App\Caches\CourseTeacherList as CourseTeacherListCache; +use App\Caches\MaxCourseId as MaxCourseIdCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Course as CourseModel; use App\Models\CourseCategory as CourseCategoryModel; @@ -67,6 +69,8 @@ class Course extends Service $course->create($data); + $this->rebuildCourseCache($course); + return $course; } @@ -148,6 +152,8 @@ class Course extends Service $course->update(); + $this->rebuildCourseCache($course); + return $course; } @@ -159,6 +165,8 @@ class Course extends Service $course->update(); + $this->rebuildCourseCache($course); + return $course; } @@ -310,6 +318,17 @@ class Course extends Service return $validator->checkCourse($id); } + protected function rebuildCourseCache(CourseModel $course) + { + $cache = new CourseCache(); + + $cache->rebuild($course->id); + + $cache = new MaxCourseIdCache(); + + $cache->rebuild(); + } + protected function saveTeachers(CourseModel $course, $teacherIds) { $courseRepo = new CourseRepo(); diff --git a/app/Http/Admin/Services/Help.php b/app/Http/Admin/Services/Help.php index 2ed88cdf..1ee61d43 100644 --- a/app/Http/Admin/Services/Help.php +++ b/app/Http/Admin/Services/Help.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\Help as HelpCache; +use App\Caches\MaxHelpId as MaxHelpIdCache; use App\Models\Help as HelpModel; use App\Repos\Help as HelpRepo; use App\Validators\Help as HelpValidator; @@ -42,6 +44,8 @@ class Help extends Service $help->create($data); + $this->rebuildHelpCache($help); + return $help; } @@ -73,6 +77,8 @@ class Help extends Service $help->update($data); + $this->rebuildHelpCache($help); + return $help; } @@ -84,6 +90,8 @@ class Help extends Service $help->update(); + $this->rebuildHelpCache($help); + return $help; } @@ -95,9 +103,22 @@ class Help extends Service $help->update(); + $this->rebuildHelpCache($help); + return $help; } + protected function rebuildHelpCache(HelpModel $help) + { + $cache = new HelpCache(); + + $cache->rebuild($help->id); + + $cache = new MaxHelpIdCache(); + + $cache->rebuild(); + } + protected function findOrFail($id) { $validator = new HelpValidator(); diff --git a/app/Http/Admin/Services/Nav.php b/app/Http/Admin/Services/Nav.php index bfe3634a..8fcd451d 100644 --- a/app/Http/Admin/Services/Nav.php +++ b/app/Http/Admin/Services/Nav.php @@ -91,7 +91,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -141,7 +140,6 @@ class Nav extends Service $nav->update($data); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -160,7 +158,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -175,7 +172,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; diff --git a/app/Http/Admin/Services/Package.php b/app/Http/Admin/Services/Package.php index e510fc7c..ac34aa96 100644 --- a/app/Http/Admin/Services/Package.php +++ b/app/Http/Admin/Services/Package.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxPackageId as MaxPackageIdCache; use App\Caches\Package as PackageCache; use App\Caches\PackageCourseList as PackageCourseListCache; use App\Library\Paginator\Query as PagerQuery; @@ -239,6 +240,10 @@ class Package extends Service $cache = new PackageCourseListCache(); $cache->rebuild($package->id); + + $cache = new MaxPackageIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Page.php b/app/Http/Admin/Services/Page.php index 434ff177..9e53a344 100644 --- a/app/Http/Admin/Services/Page.php +++ b/app/Http/Admin/Services/Page.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxPageId as MaxPageIdCache; +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; @@ -48,6 +50,8 @@ class Page extends Service $page->create($data); + $this->rebuildPageCache($page); + return $page; } @@ -75,6 +79,8 @@ class Page extends Service $page->update($data); + $this->rebuildPageCache($page); + return $page; } @@ -86,6 +92,8 @@ class Page extends Service $page->update(); + $this->rebuildPageCache($page); + return $page; } @@ -97,9 +105,22 @@ class Page extends Service $page->update(); + $this->rebuildPageCache($page); + return $page; } + protected function rebuildPageCache(PageModel $help) + { + $cache = new PageCache(); + + $cache->rebuild($help->id); + + $cache = new MaxPageIdCache(); + + $cache->rebuild(); + } + protected function findOrFail($id) { $validator = new PageValidator(); diff --git a/app/Http/Admin/Services/Topic.php b/app/Http/Admin/Services/Topic.php index ac8289bf..bd4719fc 100644 --- a/app/Http/Admin/Services/Topic.php +++ b/app/Http/Admin/Services/Topic.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxTopicId as MaxTopicIdCache; use App\Caches\Topic as TopicCache; use App\Caches\TopicCourseList as TopicCourseListCache; use App\Library\Paginator\Query as PagerQuery; @@ -198,6 +199,10 @@ class Topic extends Service $cache = new TopicCourseListCache(); $cache->rebuild($topic->id); + + $cache = new MaxTopicIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Views/course/edit_basic.volt b/app/Http/Admin/Views/course/edit_basic.volt index 49cbe2de..4109537a 100644 --- a/app/Http/Admin/Views/course/edit_basic.volt +++ b/app/Http/Admin/Views/course/edit_basic.volt @@ -11,11 +11,11 @@