diff --git a/app/Caches/Chapter.php b/app/Caches/Chapter.php index 4166fc96..2b8cb436 100644 --- a/app/Caches/Chapter.php +++ b/app/Caches/Chapter.php @@ -25,11 +25,7 @@ class Chapter extends Cache $chapter = $chapterRepo->findById($id); - if (!$chapter) { - return new \stdClass(); - } - - return $chapter; + return $chapter ?: null; } } diff --git a/app/Caches/ChapterCounter.php b/app/Caches/ChapterCounter.php index c3f840c9..3d928275 100644 --- a/app/Caches/ChapterCounter.php +++ b/app/Caches/ChapterCounter.php @@ -7,7 +7,7 @@ use App\Repos\Chapter as ChapterRepo; class ChapterCounter extends Counter { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/Course.php b/app/Caches/Course.php index f353b063..b29c555f 100644 --- a/app/Caches/Course.php +++ b/app/Caches/Course.php @@ -25,11 +25,9 @@ class Course extends Cache $course = $courseRepo->findById($id); - $course->cover = kg_ci_img_url($course->cover); + if (!$course) return null; - if (!$course) { - return new \stdClass(); - } + $course->cover = kg_ci_img_url($course->cover); return $course; } diff --git a/app/Caches/CourseCounter.php b/app/Caches/CourseCounter.php index bed56542..8270ff3f 100644 --- a/app/Caches/CourseCounter.php +++ b/app/Caches/CourseCounter.php @@ -7,7 +7,7 @@ use App\Repos\Course as CourseRepo; class CourseCounter extends Counter { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CoursePackageList.php b/app/Caches/CoursePackageList.php index 2f59296b..f3574e5f 100644 --- a/app/Caches/CoursePackageList.php +++ b/app/Caches/CoursePackageList.php @@ -4,8 +4,6 @@ namespace App\Caches; use App\Models\Package as PackageModel; use App\Repos\Course as CourseRepo; -use App\Repos\Package as PackageRepo; -use Phalcon\Mvc\Model\Resultset; class CoursePackageList extends Cache { @@ -28,68 +26,27 @@ class CoursePackageList extends Cache $packages = $courseRepo->findPackages($id); - return $this->handleContent($packages); - } - - /** - * @param Resultset|PackageModel[] $packages - * @return array - */ - protected function handleContent($packages) - { if ($packages->count() == 0) { return []; } + return $this->handleContent($packages); + } + + /** + * @param PackageModel[] $packages + * @return array + */ + protected function handleContent($packages) + { $result = []; foreach ($packages as $package) { - - $courses = $this->getPackageCourses($package->id); - $result[] = [ 'id' => $package->id, 'title' => $package->title, 'market_price' => $package->market_price, 'vip_price' => $package->vip_price, - 'courses' => $courses, - ]; - } - - return $result; - } - - protected function getPackageCourses($packageId) - { - $packageRepo = new PackageRepo(); - - $courses = $packageRepo->findCourses($packageId); - - if ($courses->count() == 0) { - return []; - } - - $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' => $course->market_price, - 'vip_price' => $course->vip_price, - '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/MaxCategoryId.php b/app/Caches/MaxCategoryId.php new file mode 100644 index 00000000..dfc4fa4f --- /dev/null +++ b/app/Caches/MaxCategoryId.php @@ -0,0 +1,29 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'max_category_id'; + } + + public function getContent($id = null) + { + $category = CategoryModel::findFirst(['order' => 'id DESC']); + + return $category->id ?? 0; + } + +} diff --git a/app/Caches/MaxChapterId.php b/app/Caches/MaxChapterId.php index 4e2167a8..e751dfe8 100644 --- a/app/Caches/MaxChapterId.php +++ b/app/Caches/MaxChapterId.php @@ -23,7 +23,7 @@ class MaxChapterId extends Cache { $chapter = ChapterModel::findFirst(['order' => 'id DESC']); - return $chapter->id; + return $chapter->id ?? 0; } } diff --git a/app/Caches/MaxCourseId.php b/app/Caches/MaxCourseId.php index bbcc41eb..6f8b350c 100644 --- a/app/Caches/MaxCourseId.php +++ b/app/Caches/MaxCourseId.php @@ -23,7 +23,7 @@ class MaxCourseId extends Cache { $course = CourseModel::findFirst(['order' => 'id DESC']); - return $course->id; + return $course->id ?? 0; } } diff --git a/app/Caches/MaxPackageId.php b/app/Caches/MaxPackageId.php new file mode 100644 index 00000000..b5252a4c --- /dev/null +++ b/app/Caches/MaxPackageId.php @@ -0,0 +1,29 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'max_package_id'; + } + + public function getContent($id = null) + { + $package = PackageModel::findFirst(['order' => 'id DESC']); + + return $package->id ?? 0; + } + +} diff --git a/app/Caches/MaxTopicId.php b/app/Caches/MaxTopicId.php new file mode 100644 index 00000000..3c739d7a --- /dev/null +++ b/app/Caches/MaxTopicId.php @@ -0,0 +1,29 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'max_topic_id'; + } + + public function getContent($id = null) + { + $topic = TopicModel::findFirst(['order' => 'id DESC']); + + return $topic->id ?? 0; + } + +} diff --git a/app/Caches/MaxUserId.php b/app/Caches/MaxUserId.php index 2724c06c..7109382b 100644 --- a/app/Caches/MaxUserId.php +++ b/app/Caches/MaxUserId.php @@ -21,9 +21,9 @@ class MaxUserId extends Cache public function getContent($id = null) { - $course = UserModel::findFirst(['order' => 'id DESC']); + $user = UserModel::findFirst(['order' => 'id DESC']); - return $course->id; + return $user->id ?? 0; } } diff --git a/app/Caches/Package.php b/app/Caches/Package.php new file mode 100644 index 00000000..512e9ccc --- /dev/null +++ b/app/Caches/Package.php @@ -0,0 +1,31 @@ +lifetime; + } + + public function getKey($id = null) + { + return "package:{$id}"; + } + + public function getContent($id = null) + { + $packageRepo = new PackageRepo(); + + $package = $packageRepo->findById($id); + + return $package ?: null; + } + +} diff --git a/app/Caches/PackageCourseList.php b/app/Caches/PackageCourseList.php new file mode 100644 index 00000000..c78f7b6a --- /dev/null +++ b/app/Caches/PackageCourseList.php @@ -0,0 +1,57 @@ +lifetime; + } + + public function getKey($id = null) + { + return "package_course_list:{$id}"; + } + + public function getContent($id = null) + { + $packageRepo = new PackageRepo(); + + $courses = $packageRepo->findCourses($id); + + if ($courses->count() == 0) { + return []; + } + + return $this->handleContent($courses); + } + + /** + * @param CourseModel[] $courses + * @return array + */ + public function handleContent($courses) + { + $result = []; + + foreach ($courses as $course) { + $result[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + ]; + } + + return $result; + } + +} diff --git a/app/Caches/Topic.php b/app/Caches/Topic.php new file mode 100644 index 00000000..a77c9d92 --- /dev/null +++ b/app/Caches/Topic.php @@ -0,0 +1,31 @@ +lifetime; + } + + public function getKey($id = null) + { + return "topic:{$id}"; + } + + public function getContent($id = null) + { + $topicRepo = new TopicRepo(); + + $topic = $topicRepo->findById($id); + + return $topic ?: null; + } + +} diff --git a/app/Caches/TopicCourseList.php b/app/Caches/TopicCourseList.php new file mode 100644 index 00000000..b09328b4 --- /dev/null +++ b/app/Caches/TopicCourseList.php @@ -0,0 +1,57 @@ +lifetime; + } + + public function getKey($id = null) + { + return "topic_course_list:{$id}"; + } + + public function getContent($id = null) + { + $topicRepo = new TopicRepo(); + + $courses = $topicRepo->findCourses($id); + + if ($courses->count() == 0) { + return []; + } + + return $this->handleContent($courses); + } + + /** + * @param CourseModel[] $courses + * @return array + */ + public function handleContent($courses) + { + $result = []; + + foreach ($courses as $course) { + $result[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + ]; + } + + return $result; + } + +} diff --git a/app/Caches/User.php b/app/Caches/User.php index dd4be9b1..20e2fa95 100644 --- a/app/Caches/User.php +++ b/app/Caches/User.php @@ -7,7 +7,7 @@ use App\Repos\User as UserRepo; class User extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -25,11 +25,7 @@ class User extends Cache $user = $userRepo->findById($id); - if (!$user) { - return new \stdClass(); - } - - return $user; + return $user ?: null; } } diff --git a/app/Http/Admin/Services/Package.php b/app/Http/Admin/Services/Package.php index e3df0de4..e510fc7c 100644 --- a/app/Http/Admin/Services/Package.php +++ b/app/Http/Admin/Services/Package.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\Package as PackageCache; +use App\Caches\PackageCourseList as PackageCourseListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\CoursePackage as CoursePackageModel; use App\Models\Package as PackageModel; @@ -9,6 +11,7 @@ use App\Repos\Course as CourseRepo; use App\Repos\CoursePackage as CoursePackageRepo; use App\Repos\Package as PackageRepo; use App\Validators\Package as PackageValidator; +use Yansongda\Supports\Collection; class Package extends Service { @@ -50,6 +53,8 @@ class Package extends Service $package->create($data); + $this->rebuildPackageCache($package); + return $package; } @@ -91,6 +96,8 @@ class Package extends Service $this->updateCourseCount($package); + $this->rebuildPackageCache($package); + return $package; } @@ -102,6 +109,8 @@ class Package extends Service $package->update(); + $this->rebuildPackageCache($package); + return $package; } @@ -113,6 +122,8 @@ class Package extends Service $package->update(); + $this->rebuildPackageCache($package); + return $package; } @@ -141,11 +152,10 @@ class Package extends Service $sgtMarketPrice = sprintf('%0.2f', intval($totalMarketPrice * 0.9)); $sgtVipPrice = sprintf('%0.2f', intval($totalVipPrice * 0.8)); - $price = new \stdClass(); - $price->market_price = $sgtMarketPrice; - $price->vip_price = $sgtVipPrice; - - return $price; + return new Collection([ + 'market_price' => $sgtMarketPrice, + 'vip_price' => $sgtVipPrice, + ]); } public function getXmCourses($id) @@ -220,6 +230,17 @@ class Package extends Service $package->update(); } + protected function rebuildPackageCache(PackageModel $package) + { + $cache = new PackageCache(); + + $cache->rebuild($package->id); + + $cache = new PackageCourseListCache(); + + $cache->rebuild($package->id); + } + protected function findOrFail($id) { $validator = new PackageValidator(); diff --git a/app/Http/Admin/Services/Topic.php b/app/Http/Admin/Services/Topic.php index 2c504df8..ac8289bf 100644 --- a/app/Http/Admin/Services/Topic.php +++ b/app/Http/Admin/Services/Topic.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\Topic as TopicCache; +use App\Caches\TopicCourseList as TopicCourseListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\CourseTopic as CourseTopicModel; use App\Models\Topic as TopicModel; @@ -49,6 +51,8 @@ class Topic extends Service $topic->create($data); + $this->rebuildTopicCache($topic); + return $topic; } @@ -82,6 +86,8 @@ class Topic extends Service $this->updateCourseCount($topic); + $this->rebuildTopicCache($topic); + return $topic; } @@ -93,6 +99,8 @@ class Topic extends Service $topic->update(); + $this->rebuildTopicCache($topic); + return $topic; } @@ -104,6 +112,8 @@ class Topic extends Service $topic->update(); + $this->rebuildTopicCache($topic); + return $topic; } @@ -179,6 +189,17 @@ class Topic extends Service $topic->update(); } + protected function rebuildTopicCache(TopicModel $topic) + { + $cache = new TopicCache(); + + $cache->rebuild($topic->id); + + $cache = new TopicCourseListCache(); + + $cache->rebuild($topic->id); + } + protected function findOrFail($id) { $validator = new TopicValidator(); diff --git a/app/Http/Admin/Views/course/edit_basic.volt b/app/Http/Admin/Views/course/edit_basic.volt index 3556b3d9..49cbe2de 100644 --- a/app/Http/Admin/Views/course/edit_basic.volt +++ b/app/Http/Admin/Views/course/edit_basic.volt @@ -11,11 +11,11 @@