1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 19:44:02 +08:00

减少model中的事件操作

This commit is contained in:
xiaochong0302 2020-04-19 19:04:04 +08:00
parent 93b0071c87
commit 576be9b61e
24 changed files with 446 additions and 92 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Caches;
use App\Repos\Chapter as ChapterRepo;
class ChapterCounter extends Counter
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "chapter_counter:{$id}";
}
public function getContent($id = null)
{
$chapterRepo = new ChapterRepo();
$chapter = $chapterRepo->findById($id);
return [
'user_count' => $chapter->user_count,
'lesson_count' => $chapter->lesson_count,
'comment_count' => $chapter->comment_count,
'agree_count' => $chapter->agree_count,
'oppose_count' => $chapter->oppose_count,
];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Caches;
use App\Repos\Course as CourseRepo;
class CourseCounter extends Counter
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "course_counter:{$id}";
}
public function getContent($id = null)
{
$courseRepo = new CourseRepo();
$course = $courseRepo->findById($id);
return [
'user_count' => $course->user_count,
'lesson_count' => $course->lesson_count,
'comment_count' => $course->comment_count,
'consult_count' => $course->consult_count,
'review_count' => $course->review_count,
'favorite_count' => $course->favorite_count,
];
}
}

View File

@ -5,6 +5,7 @@ namespace App\Caches;
use App\Models\Package as PackageModel;
use App\Repos\Course as CourseRepo;
use App\Repos\Package as PackageRepo;
use Phalcon\Mvc\Model\Resultset;
class CoursePackageList extends Cache
{
@ -27,19 +28,19 @@ class CoursePackageList extends Cache
$packages = $courseRepo->findPackages($id);
if ($packages->count() == 0) {
return [];
}
return $this->handleContent($packages);
}
/**
* @param PackageModel[] $packages
* @param Resultset|PackageModel[] $packages
* @return array
*/
protected function handleContent($packages)
{
if ($packages->count() == 0) {
return [];
}
$result = [];
foreach ($packages as $package) {
@ -64,6 +65,10 @@ class CoursePackageList extends Cache
$courses = $packageRepo->findCourses($packageId);
if ($courses->count() == 0) {
return [];
}
$result = [];
$baseUrl = kg_ci_base_url();

View File

@ -7,7 +7,6 @@ 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 Phalcon\Mvc\Model\Resultset;
class RebuildChapterCacheTask extends Task
{
@ -41,9 +40,6 @@ class RebuildChapterCacheTask extends Task
$chapterRepo = new ChapterRepo();
/**
* @var Resultset $chapters
*/
$chapters = $chapterRepo->findByIds($chapterIds);
if ($chapters->count() == 0) {
@ -51,24 +47,32 @@ class RebuildChapterCacheTask extends Task
}
$chapterCache = new ChapterCache();
$counterCache = 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->like_count = $chapterRepo->countLikes($chapter->id);
$chapter->agree_count = $chapterRepo->countAgrees($chapter->id);
$chapter->oppose_count = $chapterRepo->countOpposes($chapter->id);
$chapter->update();
$chapterCache->rebuild($chapter->id);
$counterCache->rebuild($chapter->id);
}
$this->redis->sRem($key, ...$chapterIds);
}
protected function getCacheKey()
{
$syncer = new ChapterCacheSyncer();
return $syncer->getCacheKey();
return $syncer->getSyncKey();
}
}

View File

@ -5,10 +5,8 @@ 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\Models\Course as CourseModel;
use App\Repos\Course as CourseRepo;
use App\Services\CourseCacheSyncer;
use Phalcon\Mvc\Model\Resultset;
class RebuildCourseCacheTask extends Task
{
@ -42,9 +40,6 @@ class RebuildCourseCacheTask extends Task
$courseRepo = new CourseRepo();
/**
* @var Resultset|CourseModel[] $courses
*/
$courses = $courseRepo->findByIds($courseIds);
if ($courses->count() == 0) {
@ -52,18 +47,22 @@ class RebuildCourseCacheTask extends Task
}
$courseCache = new CourseCache();
$counterCache = 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);
$course->update();
$courseCache->rebuild($course->id);
$counterCache->rebuild($course->id);
}
@ -74,7 +73,7 @@ class RebuildCourseCacheTask extends Task
{
$syncer = new CourseCacheSyncer();
return $syncer->getCacheKey();
return $syncer->getSyncKey();
}
}

View File

@ -2,6 +2,9 @@
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\Models\Category as CategoryModel;
use App\Repos\Category as CategoryRepo;
use App\Validators\Category as CategoryValidator;
@ -86,6 +89,7 @@ class Category extends Service
$category->update();
$this->updateCategoryStats($category);
$this->rebuildCategoryCache($category);
return $category;
}
@ -122,6 +126,7 @@ class Category extends Service
$category->update($data);
$this->updateCategoryStats($category);
$this->rebuildCategoryCache($category);
return $category;
}
@ -139,6 +144,7 @@ class Category extends Service
$category->update();
$this->updateCategoryStats($category);
$this->rebuildCategoryCache($category);
return $category;
}
@ -152,6 +158,7 @@ class Category extends Service
$category->update();
$this->updateCategoryStats($category);
$this->rebuildCategoryCache($category);
return $category;
}
@ -169,6 +176,21 @@ class Category extends Service
$category->update();
}
protected function rebuildCategoryCache(CategoryModel $category)
{
$itemCache = new CategoryCache();
$itemCache->rebuild($category->id);
$listCache = new CategoryListCache();
$listCache->rebuild();
$treeListCache = new CategoryTreeListCache();
$treeListCache->rebuild();
}
protected function enableChildCategories($parentId)
{
$categoryRepo = new CategoryRepo();

View File

@ -2,6 +2,7 @@
namespace App\Http\Admin\Services;
use App\Caches\CourseChapterList as CourseChapterListCache;
use App\Models\Chapter as ChapterModel;
use App\Models\Course as CourseModel;
use App\Repos\Chapter as ChapterRepo;
@ -59,6 +60,7 @@ class Chapter extends Service
$this->updateChapterStats($chapter);
$this->updateCourseStats($chapter);
$this->rebuildChapterCache($chapter);
return $chapter;
}
@ -100,6 +102,7 @@ class Chapter extends Service
$this->updateChapterStats($chapter);
$this->updateCourseStats($chapter);
$this->rebuildChapterCache($chapter);
return $chapter;
}
@ -118,6 +121,7 @@ class Chapter extends Service
$this->updateChapterStats($chapter);
$this->updateCourseStats($chapter);
$this->rebuildChapterCache($chapter);
return $chapter;
}
@ -132,6 +136,7 @@ class Chapter extends Service
$this->updateChapterStats($chapter);
$this->updateCourseStats($chapter);
$this->rebuildChapterCache($chapter);
return $chapter;
}
@ -168,6 +173,13 @@ class Chapter extends Service
}
}
protected function rebuildChapterCache(ChapterModel $chapter)
{
$cache = new CourseChapterListCache();
$cache->rebuild($chapter->course_id);
}
protected function findOrFail($id)
{
$validator = new ChapterValidator();

View File

@ -3,6 +3,9 @@
namespace App\Http\Admin\Services;
use App\Builders\CourseList as CourseListBuilder;
use App\Caches\CourseCategoryList as CourseCategoryListCache;
use App\Caches\CourseRelatedList as CourseRelatedListCache;
use App\Caches\CourseTeacherList as CourseTeacherListCache;
use App\Library\Paginator\Query as PagerQuery;
use App\Models\Course as CourseModel;
use App\Models\CourseCategory as CourseCategoryModel;
@ -348,6 +351,10 @@ class Course extends Service
}
}
}
$cache = new CourseTeacherListCache();
$cache->rebuild();
}
protected function saveCategories(CourseModel $course, $categoryIds)
@ -388,6 +395,17 @@ class Course extends Service
}
}
}
$categoryId = $newCategoryIds[0] ?? 0;
if ($categoryId) {
$course->category_id = $categoryId;
$course->update();
}
$cache = new CourseCategoryListCache();
$cache->rebuild($course->id);
}
protected function saveRelatedCourses(CourseModel $course, $courseIds)
@ -449,6 +467,10 @@ class Course extends Service
}
}
}
$cache = new CourseRelatedListCache();
$cache->rebuild($course->id);
}
protected function handleCourses($pager)

View File

@ -92,6 +92,8 @@ class Nav extends Service
$this->updateNavStats($nav);
$this->rebuildNavCache();
return $nav;
}
@ -140,6 +142,8 @@ class Nav extends Service
$this->updateNavStats($nav);
$this->rebuildNavCache();
return $nav;
}
@ -157,6 +161,8 @@ class Nav extends Service
$this->updateNavStats($nav);
$this->rebuildNavCache();
return $nav;
}
@ -170,6 +176,8 @@ class Nav extends Service
$this->updateNavStats($nav);
$this->rebuildNavCache();
return $nav;
}
@ -182,10 +190,16 @@ class Nav extends Service
}
$childCount = $navRepo->countChildNavs($nav->id);
$nav->child_count = $childCount;
$nav->update();
$nav->child_count = $childCount;
$nav->update();
}
protected function rebuildNavCache()
{
$cache = new NavTreeListCache();
$cache->rebuild();
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Admin\Services;
use App\Caches\SlideList as SlideListCache;
use App\Library\Paginator\Query as PagerQuery;
use App\Models\Slide as SlideModel;
use App\Repos\Slide as SlideRepo;
@ -59,6 +60,8 @@ class Slide extends Service
$slide->create($data);
$this->rebuildSlideCache();
return $slide;
}
@ -106,6 +109,8 @@ class Slide extends Service
$slide->update($data);
$this->rebuildSlideCache();
return $slide;
}
@ -117,6 +122,8 @@ class Slide extends Service
$slide->update();
$this->rebuildSlideCache();
return $slide;
}
@ -128,9 +135,18 @@ class Slide extends Service
$slide->update();
$this->rebuildSlideCache();
return $slide;
}
protected function rebuildSlideCache()
{
$cache = new SlideListCache();
$cache->rebuild();
}
protected function findOrFail($id)
{
$validator = new SlideValidator();

View File

@ -53,7 +53,7 @@ function kg_uniqid($prefix = '', $more = false)
*/
function kg_json_encode($data)
{
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION;
return json_encode($data, $options);
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Listeners;
use App\Caches\ChapterCounter as CacheChapterCounter;
use App\Models\User as ChapterModel;
use Phalcon\Events\Event;
class ChapterCounter extends Listener
{
protected $counter;
public function __construct()
{
$this->counter = new CacheChapterCounter();
}
public function incrUserCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hIncrBy($chapter->id, 'user_count');
}
public function decrUserCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hDecrBy($chapter->id, 'user_count');
}
public function incrCommentCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hIncrBy($chapter->id, 'comment_count');
}
public function decrCommentCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hDecrBy($chapter->id, 'comment_count');
}
public function incrAgreeCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hIncrBy($chapter->id, 'agree_count');
}
public function decrAgreeCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hDecrBy($chapter->id, 'agree_count');
}
public function incrOpposeCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hIncrBy($chapter->id, 'oppose_count');
}
public function decrOpposeCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hDecrBy($chapter->id, 'oppose_count');
}
public function incrLessonCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hIncrBy($chapter->id, 'lesson_count');
}
public function decrLessonCount(Event $event, $source, ChapterModel $chapter)
{
$this->counter->hDecrBy($chapter->id, 'lesson_count');
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace App\Listeners;
use App\Caches\CourseCounter as CacheCourseCounter;
use App\Models\User as CourseModel;
use Phalcon\Events\Event;
class CourseCounter extends Listener
{
protected $counter;
public function __construct()
{
$this->counter = new CacheCourseCounter();
}
public function incrUserCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'user_count');
}
public function decrUserCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'user_count');
}
public function incrCommentCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'comment_count');
}
public function decrCommentCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'comment_count');
}
public function incrConsultCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'consult_count');
}
public function decrConsultCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'consult_count');
}
public function incrReviewCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'review_count');
}
public function decrReviewCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'review_count');
}
public function incrFavoriteCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'favorite_count');
}
public function decrFavoriteCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'favorite_count');
}
public function incrLessonCount(Event $event, $source, CourseModel $course)
{
$this->counter->hIncrBy($course->id, 'lesson_count');
}
public function decrLessonCount(Event $event, $source, CourseModel $course)
{
$this->counter->hDecrBy($course->id, 'lesson_count');
}
}

View File

@ -2,9 +2,6 @@
namespace App\Models;
use App\Caches\Category as CategoryCache;
use App\Caches\CategoryList as CategoryListCache;
use App\Caches\CategoryTreeList as CategoryTreeListCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Category extends Model
@ -121,29 +118,4 @@ class Category extends Model
$this->update_time = time();
}
public function afterCreate()
{
$this->rebuildCache();
}
public function afterUpdate()
{
$this->rebuildCache();
}
public function rebuildCache()
{
$itemCache = new CategoryCache();
$itemCache->rebuild($this->id);
$listCache = new CategoryListCache();
$listCache->rebuild();
$treeListCache = new CategoryTreeListCache();
$treeListCache->rebuild();
}
}

View File

@ -218,7 +218,7 @@ class Chapter extends Model
{
$this->update_time = time();
if (!empty($this->attrs)) {
if (is_array($this->attrs) && !empty($this->attrs)) {
$this->attrs = kg_json_encode($this->attrs);
}
}

View File

@ -65,7 +65,7 @@ class ChapterVod extends Model
{
$this->create_time = time();
if (!empty($this->file_transcode)) {
if (is_array($this->file_transcode) && !empty($this->file_transcode)) {
$this->file_transcode = kg_json_encode($this->file_transcode);
}
}
@ -74,7 +74,7 @@ class ChapterVod extends Model
{
$this->update_time = time();
if (!empty($this->file_transcode)) {
if (is_array($this->file_transcode) && !empty($this->file_transcode)) {
$this->file_transcode = kg_json_encode($this->file_transcode);
}
}

View File

@ -270,7 +270,7 @@ class Course extends Model
{
$this->update_time = time();
if (!empty($this->attrs)) {
if (is_array($this->attrs) && !empty($this->attrs)) {
$this->attrs = kg_json_encode($this->attrs);
}
}

View File

@ -2,7 +2,6 @@
namespace App\Models;
use App\Caches\NavTreeList as NavTreeListCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Nav extends Model
@ -145,23 +144,6 @@ class Nav extends Model
$this->update_time = time();
}
public function afterCreate()
{
$this->rebuildCache();
}
public function afterUpdate()
{
$this->rebuildCache();
}
public function rebuildCache()
{
$cache = new NavTreeListCache();
$cache->rebuild();
}
public static function positionTypes()
{
return [

View File

@ -152,7 +152,7 @@ class Order extends Model
$this->sn = date('YmdHis') . rand(1000, 9999);
$this->create_time = time();
if (!empty($this->item_info)) {
if (is_array($this->item_info) && !empty($this->item_info)) {
$this->item_info = kg_json_encode($this->item_info);
} else {
$this->item_info = '';
@ -163,7 +163,7 @@ class Order extends Model
{
$this->update_time = time();
if (!empty($this->item_info)) {
if (is_array($this->item_info) && !empty($this->item_info)) {
$this->item_info = kg_json_encode($this->item_info);
}
}

View File

@ -2,7 +2,6 @@
namespace App\Models;
use App\Caches\SlideList as SlideListCache;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Slide extends Model
@ -119,23 +118,6 @@ class Slide extends Model
$this->update_time = time();
}
public function afterCreate()
{
$this->rebuildCache();
}
public function afterUpdate()
{
$this->rebuildCache();
}
public function rebuildCache()
{
$cache = new SlideListCache();
$cache->rebuild();
}
public static function targetTypes()
{
return [

View File

@ -0,0 +1,46 @@
<?php
namespace App\Services;
use App\Library\Cache\Backend\Redis as RedisCache;
class ChapterCacheSyncer 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($chapterId)
{
$key = $this->getSyncKey();
$this->redis->sAdd($key, $chapterId);
$this->redis->expire($key, $this->lifetime);
}
public function getSyncKey()
{
return 'chapter_cache_sync';
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Services;
use App\Library\Cache\Backend\Redis as RedisCache;
class CourseCacheSyncer 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($courseId)
{
$key = $this->getSyncKey();
$this->redis->sAdd($key, $courseId);
$this->redis->expire($key, $this->lifetime);
}
public function getSyncKey()
{
return 'course_cache_sync';
}
}

View File

@ -40,7 +40,11 @@ class ChapterList extends Service
return $this->handleChapters($chapters);
}
protected function handleChapters(Resultset $chapters)
/**
* @param Resultset $chapters
* @return array
*/
protected function handleChapters($chapters)
{
if ($chapters->count() == 0) {
return [];

View File

@ -2,6 +2,7 @@
namespace App\Services\Frontend\Course;
use App\Models\Package as PackageModel;
use App\Repos\Course as CourseRepo;
use App\Repos\Package as PackageRepo;
use App\Services\Frontend\CourseTrait;
@ -25,7 +26,7 @@ class PackageList extends Service
}
/**
* @param Resultset $packages
* @param Resultset|PackageModel[] $packages
* @return array
*/
protected function handlePackages($packages)
@ -58,6 +59,10 @@ class PackageList extends Service
$courses = $packageRepo->findCourses($packageId);
if ($courses->count() == 0) {
return [];
}
$result = [];
$baseUrl = kg_ci_base_url();