1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-17 21:56:13 +08:00

增强缓存

This commit is contained in:
xiaochong0302 2020-04-23 19:52:46 +08:00
parent 1d8af72d29
commit 530ec92736
45 changed files with 1074 additions and 229 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Caches;
use App\Repos\Comment as CommentRepo;
class CommentCounter extends Counter
{
protected $lifetime = 1 * 86400;
public function getLifetime()
{
return $this->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,
];
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Caches;
use App\Repos\Consult as ConsultRepo;
class ConsultCounter extends Counter
{
protected $lifetime = 1 * 86400;
public function getLifetime()
{
return $this->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,
];
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Caches;
use App\Repos\Review as ReviewRepo;
class ReviewCounter extends Counter
{
protected $lifetime = 1 * 86400;
public function getLifetime()
{
return $this->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,
];
}
}

View File

@ -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();
}

View File

@ -0,0 +1,74 @@
<?php
namespace App\Console\Tasks;
use App\Caches\CommentCounter as CommentCounterCache;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Repos\Comment as CommentRepo;
use App\Services\CommentCounterSyncer;
class SyncCommentCounterTask extends Task
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
public function mainAction()
{
$this->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();
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace App\Console\Tasks;
use App\Caches\ConsultCounter as ConsultCounterCache;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Repos\Consult as ConsultRepo;
use App\Services\ConsultCounterSyncer;
class SyncConsultCounterTask extends Task
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
public function mainAction()
{
$this->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();
}
}

View File

@ -8,7 +8,7 @@ use App\Searchers\CourseDocument;
use App\Searchers\CourseSearch;
use App\Services\CourseIndexSyncer;
class RebuildCourseIndexTask extends Task
class SyncCourseIndexTask extends Task
{
/**

View File

@ -0,0 +1,73 @@
<?php
namespace App\Console\Tasks;
use App\Caches\ReviewCounter as ReviewCounterCache;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Repos\Review as ReviewRepo;
use App\Services\ReviewCounterSyncer;
class SyncReviewCounterTask extends Task
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
public function mainAction()
{
$this->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();
}
}

View File

@ -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();
}

View File

@ -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)

View File

@ -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)

View File

@ -85,7 +85,7 @@ class ChapterContent extends Service
$vod->update([
'file_id' => $fileId,
'file_attrs' => '',
'file_transcode' => '',
]);
/**

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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();

View File

@ -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);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Listeners;
use App\Caches\CommentCounter as CacheCommentCounter;
use App\Models\Comment as CommentModel;
use App\Services\Syncer\CommentCounter as CommentCounterSyncer;
use Phalcon\Events\Event;
class CommentCounter extends Listener
{
protected $counter;
public function __construct()
{
$this->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);
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\Listeners;
use App\Caches\ConsultCounter as CacheConsultCounter;
use App\Models\Consult as ConsultModel;
use App\Services\Syncer\ConsultCounter as ConsultCounterSyncer;
use Phalcon\Events\Event;
class ConsultCounter extends Listener
{
protected $counter;
public function __construct()
{
$this->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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\Listeners;
use App\Caches\ReviewCounter as CacheReviewCounter;
use App\Models\Review as ReviewModel;
use App\Services\Syncer\ReviewCounter as ReviewCounterSyncer;
use Phalcon\Events\Event;
class ReviewCounter extends Listener
{
protected $counter;
public function __construct()
{
$this->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);
}
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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')) {

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -1,10 +1,11 @@
<?php
namespace App\Services;
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class ChapterCacheSyncer extends Service
class ChapterCounter extends Service
{
/**
@ -40,7 +41,7 @@ class ChapterCacheSyncer extends Service
public function getSyncKey()
{
return 'chapter_cache_sync';
return 'chapter_counter_sync';
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class CommentCounter extends Service
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
/**
* @var int
*/
protected $lifetime = 86400;
public function __construct()
{
$this->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';
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class ConsultCounter extends Service
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
/**
* @var int
*/
protected $lifetime = 86400;
public function __construct()
{
$this->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';
}
}

View File

@ -1,10 +1,11 @@
<?php
namespace App\Services;
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class CourseCacheSyncer extends Service
class CourseCounter extends Service
{
/**
@ -40,7 +41,7 @@ class CourseCacheSyncer extends Service
public function getSyncKey()
{
return 'course_cache_sync';
return 'course_counter_sync';
}
}

View File

@ -1,10 +1,11 @@
<?php
namespace App\Services;
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class CourseIndexSyncer extends Service
class CourseIndex extends Service
{
/**

View File

@ -1,14 +1,14 @@
<?php
namespace App\Services;
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Models\Learning;
use App\Models\Learning as LearningModel;
use App\Repos\Chapter as ChapterRepo;
use App\Services\Service;
use App\Traits\Client as ClientTrait;
class LearningSyncer extends Service
class Learning extends Service
{
use ClientTrait;

View File

@ -0,0 +1,47 @@
<?php
namespace App\Services\Syncer;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Service;
class ReviewCounter extends Service
{
/**
* @var RedisCache
*/
protected $cache;
/**
* @var \Redis
*/
protected $redis;
/**
* @var int
*/
protected $lifetime = 86400;
public function __construct()
{
$this->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';
}
}

View File

@ -1,11 +1,21 @@
<?php
use App\Listeners\ChapterCounter;
use App\Listeners\CommentCounter;
use App\Listeners\ConsultCounter;
use App\Listeners\CourseCounter;
use App\Listeners\Pay;
use App\Listeners\Profiler;
use App\Listeners\ReviewCounter;
use App\Listeners\UserDailyCounter;
return [
'db' => Profiler::class,
'pay' => Pay::class,
'courseCounter' => CourseCounter::class,
'chapterCounter' => ChapterCounter::class,
'commentCounter' => CommentCounter::class,
'consultCounter' => ConsultCounter::class,
'reviewCounter' => ReviewCounter::class,
'userDailyCounter' => UserDailyCounter::class,
];

View File

@ -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);