diff --git a/app/Caches/CommentCounter.php b/app/Caches/CommentCounter.php new file mode 100644 index 00000000..4afed9ac --- /dev/null +++ b/app/Caches/CommentCounter.php @@ -0,0 +1,37 @@ +lifetime; + } + + public function getKey($id = null) + { + return "comment_counter:{$id}"; + } + + public function getContent($id = null) + { + $commentRepo = new CommentRepo(); + + $comment = $commentRepo->findById($id); + + if (!$comment) return null; + + return [ + 'reply_count' => $comment->reply_count, + 'agree_count' => $comment->agree_count, + 'oppose_count' => $comment->oppose_count, + ]; + } + +} diff --git a/app/Caches/ConsultCounter.php b/app/Caches/ConsultCounter.php new file mode 100644 index 00000000..6d0f4570 --- /dev/null +++ b/app/Caches/ConsultCounter.php @@ -0,0 +1,36 @@ +lifetime; + } + + public function getKey($id = null) + { + return "consult_counter:{$id}"; + } + + public function getContent($id = null) + { + $consultRepo = new ConsultRepo(); + + $consult = $consultRepo->findById($id); + + if (!$consult) return null; + + return [ + 'agree_count' => $consult->agree_count, + 'oppose_count' => $consult->oppose_count, + ]; + } + +} diff --git a/app/Caches/ReviewCounter.php b/app/Caches/ReviewCounter.php new file mode 100644 index 00000000..24cd5811 --- /dev/null +++ b/app/Caches/ReviewCounter.php @@ -0,0 +1,36 @@ +lifetime; + } + + public function getKey($id = null) + { + return "review_counter:{$id}"; + } + + public function getContent($id = null) + { + $reviewRepo = new ReviewRepo(); + + $review = $reviewRepo->findById($id); + + if (!$review) return null; + + return [ + 'agree_count' => $review->agree_count, + 'oppose_count' => $review->oppose_count, + ]; + } + +} diff --git a/app/Console/Tasks/RebuildChapterCacheTask.php b/app/Console/Tasks/SyncChapterCounterTask.php similarity index 56% rename from app/Console/Tasks/RebuildChapterCacheTask.php rename to app/Console/Tasks/SyncChapterCounterTask.php index 4f648df0..b3dcabd2 100644 --- a/app/Console/Tasks/RebuildChapterCacheTask.php +++ b/app/Console/Tasks/SyncChapterCounterTask.php @@ -2,13 +2,12 @@ namespace App\Console\Tasks; -use App\Caches\Chapter as ChapterCache; use App\Caches\ChapterCounter as ChapterCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Chapter as ChapterRepo; -use App\Services\ChapterCacheSyncer; +use App\Services\ChapterCounterSyncer; -class RebuildChapterCacheTask extends Task +class SyncChapterCounterTask extends Task { /** @@ -46,23 +45,22 @@ class RebuildChapterCacheTask extends Task return; } - $chapterCache = new ChapterCache(); - - $counterCache = new ChapterCounterCache(); + $cache = new ChapterCounterCache(); foreach ($chapters as $chapter) { - $chapter->user_count = $chapterRepo->countUsers($chapter->id); - $chapter->lesson_count = $chapterRepo->countLessons($chapter->id); - $chapter->comment_count = $chapterRepo->countComments($chapter->id); - $chapter->agree_count = $chapterRepo->countAgrees($chapter->id); - $chapter->oppose_count = $chapterRepo->countOpposes($chapter->id); + $counter = $cache->get($chapter->id); - $chapter->update(); + if ($counter) { - $chapterCache->rebuild($chapter->id); + $chapter->user_count = $counter['user_count']; + $chapter->lesson_count = $counter['lesson_count']; + $chapter->comment_count = $counter['comment_count']; + $chapter->agree_count = $counter['agree_count']; + $chapter->oppose_count = $counter['oppose_count']; - $counterCache->rebuild($chapter->id); + $chapter->update(); + } } $this->redis->sRem($key, ...$chapterIds); @@ -70,7 +68,7 @@ class RebuildChapterCacheTask extends Task protected function getCacheKey() { - $syncer = new ChapterCacheSyncer(); + $syncer = new ChapterCounterSyncer(); return $syncer->getSyncKey(); } diff --git a/app/Console/Tasks/SyncCommentCounterTask.php b/app/Console/Tasks/SyncCommentCounterTask.php new file mode 100644 index 00000000..762d91fa --- /dev/null +++ b/app/Console/Tasks/SyncCommentCounterTask.php @@ -0,0 +1,74 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + + $this->rebuild(); + } + + protected function rebuild() + { + $key = $this->getCacheKey(); + + $commentIds = $this->redis->sRandMember($key, 500); + + if (!$commentIds) return; + + $commentRepo = new CommentRepo(); + + $comments = $commentRepo->findByIds($commentIds); + + if ($comments->count() == 0) { + return; + } + + $cache = new CommentCounterCache(); + + foreach ($comments as $comment) { + + $counter = $cache->get($comment->id); + + if ($counter) { + + $comment->reply_count = $counter['reply_count']; + $comment->agree_count = $counter['agree_count']; + $comment->oppose_count = $counter['oppose_count']; + + $comment->update(); + } + } + + $this->redis->sRem($key, ...$commentIds); + } + + protected function getCacheKey() + { + $syncer = new CommentCounterSyncer(); + + return $syncer->getSyncKey(); + } + +} diff --git a/app/Console/Tasks/SyncConsultCounterTask.php b/app/Console/Tasks/SyncConsultCounterTask.php new file mode 100644 index 00000000..e19a6ada --- /dev/null +++ b/app/Console/Tasks/SyncConsultCounterTask.php @@ -0,0 +1,73 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + + $this->rebuild(); + } + + protected function rebuild() + { + $key = $this->getCacheKey(); + + $consultIds = $this->redis->sRandMember($key, 500); + + if (!$consultIds) return; + + $consultRepo = new ConsultRepo(); + + $consults = $consultRepo->findByIds($consultIds); + + if ($consults->count() == 0) { + return; + } + + $cache = new ConsultCounterCache(); + + foreach ($consults as $consult) { + + $counter = $cache->get($consult->id); + + if ($counter) { + + $consult->agree_count = $counter['agree_count']; + $consult->oppose_count = $counter['oppose_count']; + + $consult->update(); + } + } + + $this->redis->sRem($key, ...$consultIds); + } + + protected function getCacheKey() + { + $syncer = new ConsultCounterSyncer(); + + return $syncer->getSyncKey(); + } + +} diff --git a/app/Console/Tasks/RebuildCourseIndexTask.php b/app/Console/Tasks/SyncCourseIndexTask.php similarity index 97% rename from app/Console/Tasks/RebuildCourseIndexTask.php rename to app/Console/Tasks/SyncCourseIndexTask.php index 64ba22df..85dc1771 100644 --- a/app/Console/Tasks/RebuildCourseIndexTask.php +++ b/app/Console/Tasks/SyncCourseIndexTask.php @@ -8,7 +8,7 @@ use App\Searchers\CourseDocument; use App\Searchers\CourseSearch; use App\Services\CourseIndexSyncer; -class RebuildCourseIndexTask extends Task +class SyncCourseIndexTask extends Task { /** diff --git a/app/Console/Tasks/SyncReviewCounterTask.php b/app/Console/Tasks/SyncReviewCounterTask.php new file mode 100644 index 00000000..f371fbdd --- /dev/null +++ b/app/Console/Tasks/SyncReviewCounterTask.php @@ -0,0 +1,73 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + + $this->rebuild(); + } + + protected function rebuild() + { + $key = $this->getCacheKey(); + + $reviewIds = $this->redis->sRandMember($key, 500); + + if (!$reviewIds) return; + + $reviewRepo = new ReviewRepo(); + + $reviews = $reviewRepo->findByIds($reviewIds); + + if ($reviews->count() == 0) { + return; + } + + $cache = new ReviewCounterCache(); + + foreach ($reviews as $review) { + + $counter = $cache->get($review->id); + + if ($counter) { + + $review->agree_count = $counter['agree_count']; + $review->oppose_count = $counter['oppose_count']; + + $review->update(); + } + } + + $this->redis->sRem($key, ...$reviewIds); + } + + protected function getCacheKey() + { + $syncer = new ReviewCounterSyncer(); + + return $syncer->getSyncKey(); + } + +} diff --git a/app/Console/Tasks/RebuildCourseCacheTask.php b/app/Console/Tasks/syncCourseCounterTask.php similarity index 54% rename from app/Console/Tasks/RebuildCourseCacheTask.php rename to app/Console/Tasks/syncCourseCounterTask.php index f78a3c8f..be8609ff 100644 --- a/app/Console/Tasks/RebuildCourseCacheTask.php +++ b/app/Console/Tasks/syncCourseCounterTask.php @@ -2,13 +2,12 @@ namespace App\Console\Tasks; -use App\Caches\Course as CourseCache; use App\Caches\CourseCounter as CourseCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Course as CourseRepo; -use App\Services\CourseCacheSyncer; +use App\Services\CourseCounterSyncer; -class RebuildCourseCacheTask extends Task +class syncCourseCounterTask extends Task { /** @@ -46,24 +45,23 @@ class RebuildCourseCacheTask extends Task return; } - $courseCache = new CourseCache(); - - $counterCache = new CourseCounterCache(); + $cache = new CourseCounterCache(); foreach ($courses as $course) { - $course->user_count = $courseRepo->countUsers($course->id); - $course->lesson_count = $courseRepo->countLessons($course->id); - $course->comment_count = $courseRepo->countComments($course->id); - $course->consult_count = $courseRepo->countConsults($course->id); - $course->review_count = $courseRepo->countReviews($course->id); - $course->favorite_count = $courseRepo->countFavorites($course->id); + $counter = $cache->get($course->id); - $course->update(); + if ($counter) { - $courseCache->rebuild($course->id); + $course->user_count = $counter['user_count']; + $course->lesson_count = $counter['lesson_count']; + $course->comment_count = $counter['comment_count']; + $course->consult_count = $counter['consult_count']; + $course->review_count = $counter['review_count']; + $course->favorite_count = $counter['favorite_count']; - $counterCache->rebuild($course->id); + $course->update(); + } } $this->redis->sRem($key, ...$courseIds); @@ -71,7 +69,7 @@ class RebuildCourseCacheTask extends Task protected function getCacheKey() { - $syncer = new CourseCacheSyncer(); + $syncer = new CourseCounterSyncer(); return $syncer->getSyncKey(); } diff --git a/app/Http/Admin/Services/Category.php b/app/Http/Admin/Services/Category.php index 193e2874..8f4f71d5 100644 --- a/app/Http/Admin/Services/Category.php +++ b/app/Http/Admin/Services/Category.php @@ -5,7 +5,6 @@ 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; @@ -90,6 +89,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; @@ -127,6 +127,7 @@ class Category extends Service $category->update($data); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; @@ -145,6 +146,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; @@ -159,6 +161,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; @@ -192,10 +195,6 @@ class Category extends Service $cache = new CategoryTreeListCache(); $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 673fba5c..7598d036 100644 --- a/app/Http/Admin/Services/Chapter.php +++ b/app/Http/Admin/Services/Chapter.php @@ -4,7 +4,6 @@ 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; @@ -64,7 +63,9 @@ class Chapter extends Service $chapter->create($data); $this->updateChapterStats($chapter); + $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; @@ -106,7 +107,9 @@ class Chapter extends Service $chapter->update($data); $this->updateChapterStats($chapter); + $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; @@ -125,7 +128,9 @@ class Chapter extends Service $chapter->update(); $this->updateChapterStats($chapter); + $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; @@ -140,7 +145,9 @@ class Chapter extends Service $chapter->update(); $this->updateChapterStats($chapter); + $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; @@ -187,10 +194,6 @@ class Chapter extends Service $cache = new CourseChapterListCache(); $cache->rebuild($chapter->course_id); - - $cache = new MaxChapterIdCache(); - - $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/ChapterContent.php b/app/Http/Admin/Services/ChapterContent.php index 94f03c76..3e86dddb 100644 --- a/app/Http/Admin/Services/ChapterContent.php +++ b/app/Http/Admin/Services/ChapterContent.php @@ -85,7 +85,7 @@ class ChapterContent extends Service $vod->update([ 'file_id' => $fileId, - 'file_attrs' => '', + 'file_transcode' => '', ]); /** diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index 8881c642..53d19ab9 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -7,7 +7,6 @@ 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; @@ -20,7 +19,9 @@ use App\Repos\CourseCategory as CourseCategoryRepo; use App\Repos\CourseRelated as CourseRelatedRepo; use App\Repos\CourseUser as CourseUserRepo; use App\Repos\User as UserRepo; +use App\Services\Syncer\CourseIndex as CourseIndexSyncer; use App\Validators\Course as CourseValidator; +use Yansongda\Supports\Collection; class Course extends Service { @@ -71,6 +72,8 @@ class Course extends Service $this->rebuildCourseCache($course); + $this->rebuildCourseIndex($course); + return $course; } @@ -141,6 +144,10 @@ class Course extends Service $course->update($data); + $this->rebuildCourseCache($course); + + $this->rebuildCourseIndex($course); + return $course; } @@ -154,6 +161,8 @@ class Course extends Service $this->rebuildCourseCache($course); + $this->rebuildCourseIndex($course); + return $course; } @@ -167,6 +176,8 @@ class Course extends Service $this->rebuildCourseCache($course); + $this->rebuildCourseIndex($course); + return $course; } @@ -174,14 +185,14 @@ class Course extends Service { $options = CourseModel::studyExpiryOptions(); - return kg_array_object($options); + return new Collection($options); } public function getRefundExpiryOptions() { $options = CourseModel::refundExpiryOptions(); - return kg_array_object($options); + return new Collection($options); } public function getXmCategories($id) @@ -323,10 +334,13 @@ class Course extends Service $cache = new CourseCache(); $cache->rebuild($course->id); + } - $cache = new MaxCourseIdCache(); + protected function rebuildCourseIndex(CourseModel $course) + { + $syncer = new CourseIndexSyncer(); - $cache->rebuild(); + $syncer->addItem($course->id); } protected function saveTeachers(CourseModel $course, $teacherIds) diff --git a/app/Http/Admin/Services/Help.php b/app/Http/Admin/Services/Help.php index 1ee61d43..6202c567 100644 --- a/app/Http/Admin/Services/Help.php +++ b/app/Http/Admin/Services/Help.php @@ -3,7 +3,6 @@ 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; @@ -113,10 +112,6 @@ class Help extends Service $cache = new HelpCache(); $cache->rebuild($help->id); - - $cache = new MaxHelpIdCache(); - - $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Package.php b/app/Http/Admin/Services/Package.php index ac34aa96..e510fc7c 100644 --- a/app/Http/Admin/Services/Package.php +++ b/app/Http/Admin/Services/Package.php @@ -2,7 +2,6 @@ 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; @@ -240,10 +239,6 @@ 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 9e53a344..91f33750 100644 --- a/app/Http/Admin/Services/Page.php +++ b/app/Http/Admin/Services/Page.php @@ -2,7 +2,6 @@ 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; @@ -115,10 +114,6 @@ class Page extends Service $cache = new PageCache(); $cache->rebuild($help->id); - - $cache = new MaxPageIdCache(); - - $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Topic.php b/app/Http/Admin/Services/Topic.php index bd4719fc..ac8289bf 100644 --- a/app/Http/Admin/Services/Topic.php +++ b/app/Http/Admin/Services/Topic.php @@ -2,7 +2,6 @@ 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; @@ -199,10 +198,6 @@ 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/Services/User.php b/app/Http/Admin/Services/User.php index 764bae04..3215d3bf 100644 --- a/app/Http/Admin/Services/User.php +++ b/app/Http/Admin/Services/User.php @@ -3,8 +3,10 @@ namespace App\Http\Admin\Services; use App\Builders\UserList as UserListBuilder; +use App\Caches\User as UserCache; use App\Library\Paginator\Query as PaginateQuery; use App\Models\Account as AccountModel; +use App\Models\User as UserModel; use App\Repos\Account as AccountRepo; use App\Repos\Role as RoleRepo; use App\Repos\User as UserRepo; @@ -85,6 +87,8 @@ class User extends Service $this->updateAdminUserCount($adminRole); } + $this->rebuildUserCache($user); + return $user; } @@ -155,6 +159,8 @@ class User extends Service $this->updateAdminUserCount($user->admin_role); } + $this->rebuildUserCache($user); + return $user; } @@ -200,6 +206,13 @@ class User extends Service return $validator->checkUser($id); } + protected function rebuildUserCache(UserModel $user) + { + $cache = new UserCache(); + + $cache->rebuild($user->id); + } + protected function updateAdminUserCount($roleId) { $roleRepo = new RoleRepo(); diff --git a/app/Listeners/ChapterCounter.php b/app/Listeners/ChapterCounter.php index f9b91d9f..0148e727 100644 --- a/app/Listeners/ChapterCounter.php +++ b/app/Listeners/ChapterCounter.php @@ -3,7 +3,8 @@ namespace App\Listeners; use App\Caches\ChapterCounter as CacheChapterCounter; -use App\Models\User as ChapterModel; +use App\Models\Chapter as ChapterModel; +use App\Services\Syncer\ChapterCounter as ChapterCounterSyncer; use Phalcon\Events\Event; class ChapterCounter extends Listener @@ -19,51 +20,78 @@ class ChapterCounter extends Listener public function incrUserCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hIncrBy($chapter->id, 'user_count'); + + $this->syncChapterCounter($chapter); } public function decrUserCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hDecrBy($chapter->id, 'user_count'); + + $this->syncChapterCounter($chapter); } public function incrCommentCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hIncrBy($chapter->id, 'comment_count'); + + $this->syncChapterCounter($chapter); } public function decrCommentCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hDecrBy($chapter->id, 'comment_count'); + + $this->syncChapterCounter($chapter); } public function incrAgreeCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hIncrBy($chapter->id, 'agree_count'); + + $this->syncChapterCounter($chapter); } public function decrAgreeCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hDecrBy($chapter->id, 'agree_count'); + + $this->syncChapterCounter($chapter); } public function incrOpposeCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hIncrBy($chapter->id, 'oppose_count'); + + $this->syncChapterCounter($chapter); } public function decrOpposeCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hDecrBy($chapter->id, 'oppose_count'); + + $this->syncChapterCounter($chapter); } public function incrLessonCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hIncrBy($chapter->id, 'lesson_count'); + + $this->syncChapterCounter($chapter); } public function decrLessonCount(Event $event, $source, ChapterModel $chapter) { $this->counter->hDecrBy($chapter->id, 'lesson_count'); + + $this->syncChapterCounter($chapter); + } + + protected function syncChapterCounter(ChapterModel $chapter) + { + $syncer = new ChapterCounterSyncer(); + + $syncer->addItem($chapter->id); } } \ No newline at end of file diff --git a/app/Listeners/CommentCounter.php b/app/Listeners/CommentCounter.php new file mode 100644 index 00000000..de4cb319 --- /dev/null +++ b/app/Listeners/CommentCounter.php @@ -0,0 +1,69 @@ +counter = new CacheCommentCounter(); + } + + public function incrReplyCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hIncrBy($comment->id, 'reply_count'); + + $this->syncCommentCounter($comment); + } + + public function decrReplyCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hDecrBy($comment->id, 'reply_count'); + + $this->syncCommentCounter($comment); + } + + public function incrAgreeCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hIncrBy($comment->id, 'agree_count'); + + $this->syncCommentCounter($comment); + } + + public function decrAgreeCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hDecrBy($comment->id, 'agree_count'); + + $this->syncCommentCounter($comment); + } + + public function incrOpposeCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hIncrBy($comment->id, 'oppose_count'); + + $this->syncCommentCounter($comment); + } + + public function decrOpposeCount(Event $event, $source, CommentModel $comment) + { + $this->counter->hDecrBy($comment->id, 'oppose_count'); + + $this->syncCommentCounter($comment); + } + + protected function syncCommentCounter(CommentModel $comment) + { + $syncer = new CommentCounterSyncer(); + + $syncer->addItem($comment->id); + } + +} \ No newline at end of file diff --git a/app/Listeners/ConsultCounter.php b/app/Listeners/ConsultCounter.php new file mode 100644 index 00000000..c8223722 --- /dev/null +++ b/app/Listeners/ConsultCounter.php @@ -0,0 +1,55 @@ +counter = new CacheConsultCounter(); + } + + public function incrAgreeCount(Event $event, $source, ConsultModel $consult) + { + $this->counter->hIncrBy($consult->id, 'agree_count'); + + $this->syncConsultCounter($consult); + } + + public function decrAgreeCount(Event $event, $source, ConsultModel $consult) + { + $this->counter->hDecrBy($consult->id, 'agree_count'); + + $this->syncConsultCounter($consult); + } + + public function incrOpposeCount(Event $event, $source, ConsultModel $consult) + { + $this->counter->hIncrBy($consult->id, 'oppose_count'); + + $this->syncConsultCounter($consult); + } + + public function decrOpposeCount(Event $event, $source, ConsultModel $consult) + { + $this->counter->hDecrBy($consult->id, 'oppose_count'); + + $this->syncConsultCounter($consult); + } + + protected function syncConsultCounter(ConsultModel $consult) + { + $syncer = new ConsultCounterSyncer(); + + $syncer->addItem($consult->id); + } + +} \ No newline at end of file diff --git a/app/Listeners/CourseCounter.php b/app/Listeners/CourseCounter.php index fd44517d..f4d1951b 100644 --- a/app/Listeners/CourseCounter.php +++ b/app/Listeners/CourseCounter.php @@ -3,7 +3,9 @@ namespace App\Listeners; use App\Caches\CourseCounter as CacheCourseCounter; -use App\Models\User as CourseModel; +use App\Models\Course as CourseModel; +use App\Services\Syncer\CourseCounter as CourseCounterSyncer; +use App\Services\Syncer\CourseIndex as CourseIndexSyncer; use Phalcon\Events\Event; class CourseCounter extends Listener @@ -19,61 +21,103 @@ class CourseCounter extends Listener public function incrUserCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'user_count'); + + $this->syncCourseCounter($course); + + $this->syncCourseIndex($course); } public function decrUserCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'user_count'); + + $this->syncCourseCounter($course); + + $this->syncCourseIndex($course); } public function incrCommentCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'comment_count'); + + $this->syncCourseCounter($course); } public function decrCommentCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'comment_count'); + + $this->syncCourseCounter($course); } public function incrConsultCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'consult_count'); + + $this->syncCourseCounter($course); } public function decrConsultCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'consult_count'); + + $this->syncCourseCounter($course); } public function incrReviewCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'review_count'); + + $this->syncCourseCounter($course); } public function decrReviewCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'review_count'); + + $this->syncCourseCounter($course); } public function incrFavoriteCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'favorite_count'); + + $this->syncCourseCounter($course); } public function decrFavoriteCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'favorite_count'); + + $this->syncCourseCounter($course); } public function incrLessonCount(Event $event, $source, CourseModel $course) { $this->counter->hIncrBy($course->id, 'lesson_count'); + + $this->syncCourseCounter($course); } public function decrLessonCount(Event $event, $source, CourseModel $course) { $this->counter->hDecrBy($course->id, 'lesson_count'); + + $this->syncCourseCounter($course); + } + + protected function syncCourseCounter(CourseModel $course) + { + $syncer = new CourseCounterSyncer(); + + $syncer->addItem($course->id); + } + + protected function syncCourseIndex(CourseModel $course) + { + $syncer = new CourseIndexSyncer(); + + $syncer->addItem($course->id); } } \ No newline at end of file diff --git a/app/Listeners/ReviewCounter.php b/app/Listeners/ReviewCounter.php new file mode 100644 index 00000000..9cd15510 --- /dev/null +++ b/app/Listeners/ReviewCounter.php @@ -0,0 +1,55 @@ +counter = new CacheReviewCounter(); + } + + public function incrAgreeCount(Event $event, $source, ReviewModel $review) + { + $this->counter->hIncrBy($review->id, 'agree_count'); + + $this->syncReviewCounter($review); + } + + public function decrAgreeCount(Event $event, $source, ReviewModel $review) + { + $this->counter->hDecrBy($review->id, 'agree_count'); + + $this->syncReviewCounter($review); + } + + public function incrOpposeCount(Event $event, $source, ReviewModel $review) + { + $this->counter->hIncrBy($review->id, 'oppose_count'); + + $this->syncReviewCounter($review); + } + + public function decrOpposeCount(Event $event, $source, ReviewModel $review) + { + $this->counter->hDecrBy($review->id, 'oppose_count'); + + $this->syncReviewCounter($review); + } + + protected function syncReviewCounter(ReviewModel $review) + { + $syncer = new ReviewCounterSyncer(); + + $syncer->addItem($review->id); + } + +} \ No newline at end of file diff --git a/app/Models/ChapterVod.php b/app/Models/ChapterVod.php index 12fc5d4b..6c40e18b 100644 --- a/app/Models/ChapterVod.php +++ b/app/Models/ChapterVod.php @@ -81,31 +81,24 @@ class ChapterVod extends Model public function afterFetch() { - if (!empty($this->file_transcode)) { - $this->file_transcode = json_decode($this->file_transcode, true); - } else { - $this->getFileTranscode($this->file_id); + if (!empty($this->file_id)) { + if (!empty($this->file_transcode)) { + $this->file_transcode = json_decode($this->file_transcode, true); + } else { + $this->file_transcode = $this->getFileTranscode($this->file_id); + } } } protected function getFileTranscode($fileId) { - if (!$fileId) return []; - $vodService = new VodService(); $transcode = $vodService->getFileTranscode($fileId); if ($transcode && empty($this->file_transcode)) { - $this->file_transcode = $transcode; - $this->update(); - - /** - * afterUpdate事件触发序列化,故重设属性 - */ - $this->file_transcode = $transcode; } return $transcode; diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 277e9bda..693bba9c 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -127,11 +127,26 @@ class Comment extends Model public function beforeCreate() { $this->create_time = time(); + + if (is_array($this->mentions) && !empty($this->mentions)) { + $this->mentions = kg_json_encode($this->mentions); + } } public function beforeUpdate() { $this->update_time = time(); + + if (is_array($this->mentions) && !empty($this->mentions)) { + $this->mentions = kg_json_encode($this->mentions); + } + } + + public function afterFetch() + { + if (!empty($this->mentions)) { + $this->mentions = json_decode($this->mentions, true); + } } } diff --git a/app/Models/Course.php b/app/Models/Course.php index f8ada85c..cf038cdb 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -287,18 +287,11 @@ class Course extends Model public function afterCreate() { - $this->rebuildIndex(); - $cache = new MaxCourseIdCache(); $cache->rebuild(); } - public function afterUpdate() - { - $this->rebuildIndex(); - } - public function afterFetch() { $this->market_price = (float)$this->market_price; @@ -315,13 +308,6 @@ class Course extends Model } } - public function rebuildIndex() - { - $syncer = new CourseIndexSyncer(); - - $syncer->addItem($this->id); - } - public static function getCoverPath($url) { if (Text::startsWith($url, 'http')) { diff --git a/app/Services/Frontend/Chapter/ChapterInfo.php b/app/Services/Frontend/Chapter/ChapterInfo.php index e60fcbfd..af759546 100644 --- a/app/Services/Frontend/Chapter/ChapterInfo.php +++ b/app/Services/Frontend/Chapter/ChapterInfo.php @@ -200,9 +200,7 @@ class ChapterInfo extends Service $courseUser->create(); - $course->user_count += 1; - - $course->update(); + $this->incrCourseUserCount($course); } protected function handleChapterUser(ChapterModel $chapter, UserModel $user) @@ -221,9 +219,17 @@ class ChapterInfo extends Service $chapterUser->create(); - $chapter->user_count += 1; + $this->incrChapterUserCount($chapter); + } - $chapter->update(); + protected function incrCourseUserCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:incrUserCount', $this, $course); + } + + protected function incrChapterUserCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:incrUserCount', $this, $chapter); } } diff --git a/app/Services/Frontend/Chapter/ChapterVote.php b/app/Services/Frontend/Chapter/ChapterVote.php index f55f7f6b..374b06fd 100644 --- a/app/Services/Frontend/Chapter/ChapterVote.php +++ b/app/Services/Frontend/Chapter/ChapterVote.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Chapter; +use App\Models\Chapter as ChapterModel; use App\Models\ChapterVote as ChapterVoteModel; use App\Models\User as UserModel; use App\Repos\ChapterVote as ChapterVoteRepo; @@ -38,7 +39,7 @@ class ChapterVote extends Service $chapterVote->create(); - $chapter->agree_count += 1; + $this->incrAgreeCount($chapter); } else { @@ -46,27 +47,26 @@ class ChapterVote extends Service $chapterVote->type = ChapterVoteModel::TYPE_NONE; - $chapter->agree_count -= 1; + $this->decrAgreeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) { $chapterVote->type = ChapterVoteModel::TYPE_AGREE; - $chapter->agree_count += 1; - $chapter->oppose_count -= 1; + $this->incrAgreeCount($chapter); + + $this->decrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) { $chapterVote->type = ChapterVoteModel::TYPE_AGREE; - $chapter->agree_count += 1; + $this->incrAgreeCount($chapter); } $chapterVote->update(); } - $chapter->update(); - $this->incrUserDailyChapterVoteCount($user); return $chapter; @@ -96,7 +96,7 @@ class ChapterVote extends Service $chapterVote->create(); - $chapter->oppose_count += 1; + $this->incrOpposeCount($chapter); } else { @@ -104,32 +104,51 @@ class ChapterVote extends Service $chapterVote->type = ChapterVoteModel::TYPE_OPPOSE; - $chapter->agree_count -= 1; - $chapter->oppose_count += 1; + $this->decrAgreeCount($chapter); + + $this->incrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) { $chapterVote->type = ChapterVoteModel::TYPE_NONE; - $chapter->oppose_count -= 1; + $this->decrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) { $chapterVote->type = ChapterVoteModel::TYPE_OPPOSE; - $chapter->oppose_count += 1; + $this->incrOpposeCount($chapter); } $chapterVote->update(); } - $chapter->update(); - $this->incrUserDailyChapterVoteCount($user); return $chapter; } + protected function incrAgreeCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:incrAgreeCount', $this, $chapter); + } + + protected function decrAgreeCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:decrAgreeCount', $this, $chapter); + } + + protected function incrOpposeCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:incrOpposeCount', $this, $chapter); + } + + protected function decrOpposeCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:decrOpposeCount', $this, $chapter); + } + protected function incrUserDailyChapterVoteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrChapterVoteCount', $this, $user); diff --git a/app/Services/Frontend/Comment/CommentCreate.php b/app/Services/Frontend/Comment/CommentCreate.php index e5edaf3c..7e7e0202 100644 --- a/app/Services/Frontend/Comment/CommentCreate.php +++ b/app/Services/Frontend/Comment/CommentCreate.php @@ -2,10 +2,12 @@ namespace App\Services\Frontend\Comment; +use App\Models\Chapter as ChapterModel; use App\Models\Comment as CommentModel; +use App\Models\Course as CourseModel; use App\Models\User as UserModel; -use App\Repos\Course as CourseRepo; use App\Services\Frontend\ChapterTrait; +use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; use App\Validators\Comment as CommentValidator; use App\Validators\UserDailyLimit as UserDailyLimitValidator; @@ -13,7 +15,7 @@ use App\Validators\UserDailyLimit as UserDailyLimitValidator; class CommentCreate extends Service { - use ChapterTrait; + use ChapterTrait, CourseTrait; public function createComment() { @@ -27,11 +29,9 @@ class CommentCreate extends Service $validator = new CommentValidator(); - $chapter = $validator->checkChapter($post['chapter_id']); + $chapter = $this->checkChapterCache($post['chapter_id']); - $courseRepo = new CourseRepo(); - - $course = $courseRepo->findById($chapter->course_id); + $course = $this->checkCourseCache($chapter->course_id); $data = []; @@ -54,13 +54,9 @@ class CommentCreate extends Service $comment->create($data); - $chapter->comment_count += 1; + $this->incrChapterCommentCount($chapter); - $chapter->update(); - - $course->comment_count += 1; - - $course->update(); + $this->incrCourseCommentCount($course); $this->incrUserDailyCommentCount($user); } @@ -70,6 +66,16 @@ class CommentCreate extends Service } + protected function incrChapterCommentCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:incrCommentCount', $this, $chapter); + } + + protected function incrCourseCommentCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:incrCommentCount', $this, $course); + } + protected function incrUserDailyCommentCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrCommentCount', $this, $user); diff --git a/app/Services/Frontend/Comment/CommentDelete.php b/app/Services/Frontend/Comment/CommentDelete.php index bed10f11..4f8d9ac2 100644 --- a/app/Services/Frontend/Comment/CommentDelete.php +++ b/app/Services/Frontend/Comment/CommentDelete.php @@ -2,16 +2,18 @@ namespace App\Services\Frontend\Comment; -use App\Repos\Chapter as ChapterRepo; -use App\Repos\Course as CourseRepo; +use App\Models\Chapter as ChapterModel; +use App\Models\Course as CourseModel; +use App\Services\Frontend\ChapterTrait; use App\Services\Frontend\CommentTrait; +use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; use App\Validators\Comment as CommentValidator; class CommentDelete extends Service { - use CommentTrait; + use CommentTrait, ChapterTrait, CourseTrait; public function deleteComment($id) { @@ -27,21 +29,23 @@ class CommentDelete extends Service $comment->update(); - $chapterRepo = new ChapterRepo(); + $chapter = $this->checkChapterCache($comment->chapter_id); - $chapter = $chapterRepo->findById($comment->chapter_id); + $this->decrChapterCommentCount($chapter); - $chapter->comment_count -= 1; + $course = $this->checkCourseCache($comment->course_id); - $chapter->update(); + $this->decrCourseCommentCount($course); + } - $courseRepo = new CourseRepo(); + protected function decrChapterCommentCount(ChapterModel $chapter) + { + $this->eventsManager->fire('chapterCounter:decrCommentCount', $this, $chapter); + } - $course = $courseRepo->findById($comment->course_id); - - $course->comment_count -= 1; - - $course->update(); + protected function decrCourseCommentCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:decrCommentCount', $this, $course); } } diff --git a/app/Services/Frontend/Comment/CommentVote.php b/app/Services/Frontend/Comment/CommentVote.php index d33d5931..4e56ed52 100644 --- a/app/Services/Frontend/Comment/CommentVote.php +++ b/app/Services/Frontend/Comment/CommentVote.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Comment; +use App\Models\Comment as CommentModel; use App\Models\CommentVote as CommentVoteModel; use App\Models\User as UserModel; use App\Repos\CommentVote as CommentVoteRepo; @@ -38,7 +39,7 @@ class CommentVote extends Service $commentVote->create(); - $comment->agree_count += 1; + $this->incrAgreeCount($comment); } else { @@ -46,27 +47,26 @@ class CommentVote extends Service $commentVote->type = CommentVoteModel::TYPE_NONE; - $comment->agree_count -= 1; + $this->decrAgreeCount($comment); } elseif ($commentVote->type == CommentVoteModel::TYPE_OPPOSE) { $commentVote->type = CommentVoteModel::TYPE_AGREE; - $comment->agree_count += 1; - $comment->oppose_count -= 1; + $this->incrAgreeCount($comment); + + $this->decrOpposeCount($comment); } elseif ($commentVote->type == CommentVoteModel::TYPE_NONE) { $commentVote->type = CommentVoteModel::TYPE_AGREE; - $comment->agree_count += 1; + $this->incrAgreeCount($comment); } $commentVote->update(); } - $comment->update(); - $this->incrUserDailyCommentVoteCount($user); return $comment; @@ -96,7 +96,7 @@ class CommentVote extends Service $commentVote->create(); - $comment->oppose_count += 1; + $this->incrOpposeCount($comment); } else { @@ -104,32 +104,51 @@ class CommentVote extends Service $commentVote->type = CommentVoteModel::TYPE_OPPOSE; - $comment->agree_count -= 1; - $comment->oppose_count += 1; + $this->decrAgreeCount($comment); + + $this->incrOpposeCount($comment); } elseif ($commentVote->type == CommentVoteModel::TYPE_OPPOSE) { $commentVote->type = CommentVoteModel::TYPE_NONE; - $comment->oppose_count -= 1; + $this->decrOpposeCount($comment); } elseif ($commentVote->type == CommentVoteModel::TYPE_NONE) { $commentVote->type = CommentVoteModel::TYPE_OPPOSE; - $comment->oppose_count += 1; + $this->incrOpposeCount($comment); } $commentVote->update(); } - $comment->update(); - $this->incrUserDailyCommentVoteCount($user); return $comment; } + protected function incrAgreeCount(CommentModel $comment) + { + $this->eventsManager->fire('commentCounter:incrAgreeCount', $this, $comment); + } + + protected function decrAgreeCount(CommentModel $comment) + { + $this->eventsManager->fire('commentCounter:decrAgreeCount', $this, $comment); + } + + protected function incrOpposeCount(CommentModel $comment) + { + $this->eventsManager->fire('commentCounter:incrOpposeCount', $this, $comment); + } + + protected function decrOpposeCount(CommentModel $comment) + { + $this->eventsManager->fire('commentCounter:decrOpposeCount', $this, $comment); + } + protected function incrUserDailyCommentVoteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrCommentVoteCount', $this, $user); diff --git a/app/Services/Frontend/Consult/ConsultCreate.php b/app/Services/Frontend/Consult/ConsultCreate.php index 62ec39ad..75323b6f 100644 --- a/app/Services/Frontend/Consult/ConsultCreate.php +++ b/app/Services/Frontend/Consult/ConsultCreate.php @@ -3,6 +3,7 @@ namespace App\Services\Frontend\Consult; use App\Models\Consult as ConsultModel; +use App\Models\Course as CourseModel; use App\Models\User as UserModel; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; @@ -20,13 +21,14 @@ class ConsultCreate extends Service $user = $this->getLoginUser(); + $course = $this->checkCourseCache($post['course_id']); + $validator = new UserDailyLimitValidator(); $validator->checkConsultLimit($user); $validator = new ConsultValidator(); - $course = $validator->checkCourse($post['course_id']); $question = $validator->checkQuestion($post['question']); $consult = new ConsultModel(); @@ -37,15 +39,18 @@ class ConsultCreate extends Service $consult->create(); - $course->consult_count += 1; - - $course->update(); + $this->incrCourseConsultCount($course); $this->incrUserDailyConsultCount($user); return $consult; } + protected function incrCourseConsultCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:incrConsultCount', $this, $course); + } + protected function incrUserDailyConsultCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrConsultCount', $this, $user); diff --git a/app/Services/Frontend/Consult/ConsultVote.php b/app/Services/Frontend/Consult/ConsultVote.php index dbeef13b..ddb41f21 100644 --- a/app/Services/Frontend/Consult/ConsultVote.php +++ b/app/Services/Frontend/Consult/ConsultVote.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Consult; +use App\Models\Consult as ConsultModel; use App\Models\ConsultVote as ConsultVoteModel; use App\Models\User as UserModel; use App\Repos\ConsultVote as ConsultVoteRepo; @@ -38,7 +39,7 @@ class ConsultVote extends Service $consultVote->create(); - $consult->agree_count += 1; + $this->incrAgreeCount($consult); } else { @@ -46,27 +47,26 @@ class ConsultVote extends Service $consultVote->type = ConsultVoteModel::TYPE_NONE; - $consult->agree_count -= 1; + $this->decrAgreeCount($consult); } elseif ($consultVote->type == ConsultVoteModel::TYPE_OPPOSE) { $consultVote->type = ConsultVoteModel::TYPE_AGREE; - $consult->agree_count += 1; - $consult->oppose_count -= 1; + $this->incrAgreeCount($consult); + + $this->decrOpposeCount($consult); } elseif ($consultVote->type == ConsultVoteModel::TYPE_NONE) { $consultVote->type = ConsultVoteModel::TYPE_AGREE; - $consult->agree_count += 1; + $this->incrAgreeCount($consult); } $consultVote->update(); } - $consult->update(); - $this->incrUserDailyConsultVoteCount($user); return $consult; @@ -96,7 +96,7 @@ class ConsultVote extends Service $consultVote->create(); - $consult->oppose_count += 1; + $this->incrOpposeCount($consult); } else { @@ -104,32 +104,51 @@ class ConsultVote extends Service $consultVote->type = ConsultVoteModel::TYPE_OPPOSE; - $consult->agree_count -= 1; - $consult->oppose_count += 1; + $this->decrAgreeCount($consult); + + $this->incrOpposeCount($consult); } elseif ($consultVote->type == ConsultVoteModel::TYPE_OPPOSE) { $consultVote->type = ConsultVoteModel::TYPE_NONE; - $consult->oppose_count -= 1; + $this->decrOpposeCount($consult); } elseif ($consultVote->type == ConsultVoteModel::TYPE_NONE) { $consultVote->type = ConsultVoteModel::TYPE_OPPOSE; - $consult->oppose_count += 1; + $this->incrOpposeCount($consult); } $consultVote->update(); } - $consult->update(); - $this->incrUserDailyConsultVoteCount($user); return $consult; } + protected function incrAgreeCount(ConsultModel $consult) + { + $this->eventsManager->fire('consultCounter:incrAgreeCount', $this, $consult); + } + + protected function decrAgreeCount(ConsultModel $consult) + { + $this->eventsManager->fire('consultCounter:decrAgreeCount', $this, $consult); + } + + protected function incrOpposeCount(ConsultModel $consult) + { + $this->eventsManager->fire('consultCounter:incrOpposeCount', $this, $consult); + } + + protected function decrOpposeCount(ConsultModel $consult) + { + $this->eventsManager->fire('consultCounter:decrOpposeCount', $this, $consult); + } + protected function incrUserDailyConsultVoteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrConsultVoteCount', $this, $user); diff --git a/app/Services/Frontend/Course/CourseFavorite.php b/app/Services/Frontend/Course/CourseFavorite.php index d3e6f81e..9d1282f9 100644 --- a/app/Services/Frontend/Course/CourseFavorite.php +++ b/app/Services/Frontend/Course/CourseFavorite.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Course; +use App\Models\Course as CourseModel; use App\Models\CourseFavorite as FavoriteModel; use App\Models\User as UserModel; use App\Repos\CourseFavorite as CourseFavoriteRepo; @@ -14,7 +15,7 @@ class CourseFavorite extends Service use CourseTrait; - public function favorite($id) + public function saveFavorite($id) { $course = $this->checkCourse($id); @@ -37,46 +38,37 @@ class CourseFavorite extends Service $favorite->create(); - $course->favorite_count += 1; + $this->incrCourseFavoriteCount($course); } else { - if ($favorite->deleted == 1) { + if ($favorite->deleted == 0) { + + $favorite->deleted = 1; + + $this->decrCourseFavoriteCount($course); + + } else { $favorite->deleted = 0; - $course->favorite_count += 1; - - $favorite->update(); + $this->incrCourseFavoriteCount($course); } + $favorite->update(); } - $course->update(); - $this->incrUserDailyFavoriteCount($user); } - public function unfavorite($id) + protected function incrCourseFavoriteCount(CourseModel $course) { - $course = $this->checkCourse($id); + $this->eventsManager->fire('courseCounter:incrFavoriteCount', $this, $course); + } - $user = $this->getLoginUser(); - - $favoriteRepo = new CourseFavoriteRepo(); - - $favorite = $favoriteRepo->findCourseFavorite($course->id, $user->id); - - if (!$favorite) return; - - if ($favorite->deleted == 0) { - - $favorite->deleted = 1; - - $course->favorite_count -= 1; - } - - $favorite->update(); + protected function decrCourseFavoriteCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:decrFavoriteCount', $this, $course); } protected function incrUserDailyFavoriteCount(UserModel $user) diff --git a/app/Services/Frontend/Review/ReviewCreate.php b/app/Services/Frontend/Review/ReviewCreate.php index 518552c8..81902321 100644 --- a/app/Services/Frontend/Review/ReviewCreate.php +++ b/app/Services/Frontend/Review/ReviewCreate.php @@ -2,8 +2,10 @@ namespace App\Services\Frontend\Review; +use App\Models\Course as CourseModel; use App\Models\Review as ReviewModel; use App\Models\User as UserModel; +use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; use App\Validators\Review as ReviewValidator; use App\Validators\UserDailyLimit as UserDailyLimitValidator; @@ -11,10 +13,14 @@ use App\Validators\UserDailyLimit as UserDailyLimitValidator; class ReviewCreate extends Service { + use CourseTrait; + public function createReview() { $post = $this->request->getPost(); + $course = $this->checkCourseCache($post['course_id']); + $user = $this->getLoginUser(); $validator = new UserDailyLimitValidator(); @@ -23,7 +29,6 @@ class ReviewCreate extends Service $validator = new ReviewValidator(); - $course = $validator->checkCourse($post['course_id']); $content = $validator->checkContent($post['content']); $rating = $validator->checkRating($post['rating']); @@ -38,13 +43,16 @@ class ReviewCreate extends Service $review->create(); - $course->review_count += 1; - - $course->update(); + $this->incrCourseReviewCount($course); $this->incrUserDailyReviewCount($user); } + protected function incrCourseReviewCount(CourseModel $course) + { + $this->eventsManager->fire('courseCounter:incrReviewCount', $this, $course); + } + protected function incrUserDailyReviewCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrReviewCount', $this, $user); diff --git a/app/Services/Frontend/Review/ReviewVote.php b/app/Services/Frontend/Review/ReviewVote.php index 8fa47246..61c21f9f 100644 --- a/app/Services/Frontend/Review/ReviewVote.php +++ b/app/Services/Frontend/Review/ReviewVote.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Review; +use App\Models\Review as ReviewModel; use App\Models\ReviewVote as ReviewVoteModel; use App\Models\User as UserModel; use App\Repos\ReviewVote as ReviewVoteRepo; @@ -38,7 +39,7 @@ class ReviewVote extends Service $reviewVote->create(); - $review->agree_count += 1; + $this->incrAgreeCount($review); } else { @@ -46,27 +47,26 @@ class ReviewVote extends Service $reviewVote->type = ReviewVoteModel::TYPE_NONE; - $review->agree_count -= 1; + $this->decrAgreeCount($review); } elseif ($reviewVote->type == ReviewVoteModel::TYPE_OPPOSE) { $reviewVote->type = ReviewVoteModel::TYPE_AGREE; - $review->agree_count += 1; - $review->oppose_count -= 1; + $this->incrAgreeCount($review); + + $this->decrOpposeCount($review); } elseif ($reviewVote->type == ReviewVoteModel::TYPE_NONE) { $reviewVote->type = ReviewVoteModel::TYPE_AGREE; - $review->agree_count += 1; + $this->incrAgreeCount($review); } $reviewVote->update(); } - $review->update(); - $this->incrUserDailyReviewVoteCount($user); return $review; @@ -96,7 +96,7 @@ class ReviewVote extends Service $reviewVote->create(); - $review->oppose_count += 1; + $this->incrOpposeCount($review); } else { @@ -104,32 +104,51 @@ class ReviewVote extends Service $reviewVote->type = ReviewVoteModel::TYPE_OPPOSE; - $review->agree_count -= 1; - $review->oppose_count += 1; + $this->decrAgreeCount($review); + + $this->incrOpposeCount($review); } elseif ($reviewVote->type == ReviewVoteModel::TYPE_OPPOSE) { $reviewVote->type = ReviewVoteModel::TYPE_NONE; - $review->oppose_count -= 1; + $this->decrOpposeCount($review); } elseif ($reviewVote->type == ReviewVoteModel::TYPE_NONE) { $reviewVote->type = ReviewVoteModel::TYPE_OPPOSE; - $review->oppose_count += 1; + $this->incrOpposeCount($review); } $reviewVote->update(); } - $review->update(); - $this->incrUserDailyReviewVoteCount($user); return $review; } + protected function incrAgreeCount(ReviewModel $review) + { + $this->eventsManager->fire('reviewCounter:incrAgreeCount', $this, $review); + } + + protected function decrAgreeCount(ReviewModel $review) + { + $this->eventsManager->fire('reviewCounter:decrAgreeCount', $this, $review); + } + + protected function incrOpposeCount(ReviewModel $review) + { + $this->eventsManager->fire('reviewCounter:incrOpposeCount', $this, $review); + } + + protected function decrOpposeCount(ReviewModel $review) + { + $this->eventsManager->fire('reviewCounter:decrOpposeCount', $this, $review); + } + protected function incrUserDailyReviewVoteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrReviewVoteCount', $this, $user); diff --git a/app/Services/ChapterCacheSyncer.php b/app/Services/Syncer/ChapterCounter.php similarity index 82% rename from app/Services/ChapterCacheSyncer.php rename to app/Services/Syncer/ChapterCounter.php index 01f0bfbf..2f5df70f 100644 --- a/app/Services/ChapterCacheSyncer.php +++ b/app/Services/Syncer/ChapterCounter.php @@ -1,10 +1,11 @@ cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + } + + public function addItem($commentId) + { + $key = $this->getSyncKey(); + + $this->redis->sAdd($key, $commentId); + + $this->redis->expire($key, $this->lifetime); + } + + public function getSyncKey() + { + return 'comment_counter_sync'; + } + +} diff --git a/app/Services/Syncer/ConsultCounter.php b/app/Services/Syncer/ConsultCounter.php new file mode 100644 index 00000000..d2f3aae6 --- /dev/null +++ b/app/Services/Syncer/ConsultCounter.php @@ -0,0 +1,47 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + } + + public function addItem($consultId) + { + $key = $this->getSyncKey(); + + $this->redis->sAdd($key, $consultId); + + $this->redis->expire($key, $this->lifetime); + } + + public function getSyncKey() + { + return 'consult_counter_sync'; + } + +} diff --git a/app/Services/CourseCacheSyncer.php b/app/Services/Syncer/CourseCounter.php similarity index 82% rename from app/Services/CourseCacheSyncer.php rename to app/Services/Syncer/CourseCounter.php index 65538a77..85b174e3 100644 --- a/app/Services/CourseCacheSyncer.php +++ b/app/Services/Syncer/CourseCounter.php @@ -1,10 +1,11 @@ cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + } + + public function addItem($reviewId) + { + $key = $this->getSyncKey(); + + $this->redis->sAdd($key, $reviewId); + + $this->redis->expire($key, $this->lifetime); + } + + public function getSyncKey() + { + return 'review_counter_sync'; + } + +} diff --git a/config/events.php b/config/events.php index e1ea86fb..4df77f01 100644 --- a/config/events.php +++ b/config/events.php @@ -1,11 +1,21 @@ Profiler::class, 'pay' => Pay::class, + 'courseCounter' => CourseCounter::class, + 'chapterCounter' => ChapterCounter::class, + 'commentCounter' => CommentCounter::class, + 'consultCounter' => ConsultCounter::class, + 'reviewCounter' => ReviewCounter::class, 'userDailyCounter' => UserDailyCounter::class, ]; \ No newline at end of file diff --git a/scheduler.php b/scheduler.php index 313e2db2..c9a8ca1c 100644 --- a/scheduler.php +++ b/scheduler.php @@ -31,9 +31,24 @@ $scheduler->php($script, $bin, ['--task' => 'close_order', '--action' => 'main'] $scheduler->php($script, $bin, ['--task' => 'refund', '--action' => 'main']) ->hourly(7); -$scheduler->php($script, $bin, ['--task' => 'rebuild_course_index', '--action' => 'main']) +$scheduler->php($script, $bin, ['--task' => 'sync_course_index', '--action' => 'main']) ->hourly(11); +$scheduler->php($script, $bin, ['--task' => 'sync_course_counter', '--action' => 'main']) + ->hourly(13); + +$scheduler->php($script, $bin, ['--task' => 'sync_chapter_counter', '--action' => 'main']) + ->hourly(17); + +$scheduler->php($script, $bin, ['--task' => 'sync_comment_counter', '--action' => 'main']) + ->hourly(19); + +$scheduler->php($script, $bin, ['--task' => 'sync_consult_counter', '--action' => 'main']) + ->hourly(23); + +$scheduler->php($script, $bin, ['--task' => 'sync_review_counter', '--action' => 'main']) + ->hourly(29); + $scheduler->php($script, $bin, ['--task' => 'clean_log', '--action' => 'main']) ->daily(3, 3);