From 064a5d4a92598560c6f07cf8fac96e2164e0c0cb Mon Sep 17 00:00:00 2001 From: Default Date: Wed, 9 Apr 2025 02:13:20 +0800 Subject: [PATCH] debug --- code/app/admin/controller/Novel.php | 4 +- code/app/api/controller/Statis.php | 4 +- code/app/home/controller/Index.php | 59 ++- code/app/home/controller/Novel.php | 60 +-- code/app/home/controller/User.php | 22 +- code/app/model/CategoryModel.php | 31 ++ code/app/model/ChapterModel.php | 56 +-- code/app/model/MongoModel.php | 2 +- code/app/model/NovelClicksModel.php | 2 +- code/app/model/NovelModel.php | 475 ++++++------------ .../task/crawler/zw630/page/ChapterPage.php | 4 +- .../app/task/crawler/zw630/page/NovelPage.php | 4 +- code/extend/db/NovelManage.php | 4 +- 13 files changed, 319 insertions(+), 408 deletions(-) diff --git a/code/app/admin/controller/Novel.php b/code/app/admin/controller/Novel.php index 925fe8f..ec18b26 100644 --- a/code/app/admin/controller/Novel.php +++ b/code/app/admin/controller/Novel.php @@ -42,7 +42,7 @@ class Novel extends BaseController $this->validate($arrData, $arrRule); $arrKeywords = explode(',', $arrData['keywords']); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $NovelModel->updateByKeywords($arrKeywords, $arrData['n_level']); return $this->success(); } @@ -61,7 +61,7 @@ class Novel extends BaseController try { - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $Col = $NovelModel->getCol(); diff --git a/code/app/api/controller/Statis.php b/code/app/api/controller/Statis.php index ef3c8d6..6b5a244 100644 --- a/code/app/api/controller/Statis.php +++ b/code/app/api/controller/Statis.php @@ -31,7 +31,7 @@ class Statis extends BaseController return Response::create('非法请求!')->code(400); } - $NovelClicksModel = new NovelClicksModel; + $NovelClicksModel = NovelClicksModel::getInstance(); try { // for ($intNId = 1; $intNId <= 20; $intNId++) { @@ -58,7 +58,7 @@ class Statis extends BaseController // print_r($arrCategory); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $NovelModel->updateByKeywords(["夜", "宝"], 9); exit; return Response::create(); diff --git a/code/app/home/controller/Index.php b/code/app/home/controller/Index.php index 94c0d44..6b1e4c9 100644 --- a/code/app/home/controller/Index.php +++ b/code/app/home/controller/Index.php @@ -22,25 +22,33 @@ class Index extends BaseController */ public function index(Request $Request) { - + // 获取路由附加的参数 $boolIsMobile = $Request->param('boolIsMobile'); + $intTTL = 24 * 3600; - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); // 推荐等级为9的连载的 随机15个小说 - $strKey = $Request->DomainModel->d_domain . ':H5:'; - $arrRecommendNovel = $NovelModel->getHighLevelNovelOnCache($strKey, 8); - $arrNewsNovel = $NovelModel->getSexNovelOnCache($strKey, 8, 0); + $strKey = sprintf('%s:H5:INDEX:TUIJIAN:%s', $Request->DomainModel->d_domain, 8); + $arrRecommendNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 8); + + $strKey = sprintf('%s:H5:INDEX:ZUIJIAN:SEX:%s:%s', $Request->DomainModel->d_domain, 0, 8); + $arrNewsNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 8, 0, NULL, ['og_novel_update_time' => -1]); + + $strKey = sprintf('%s:H5:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'daily', 0); $arrDayRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'daily'); + $strKey = sprintf('%s:H5:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'weekly', 0); $arrWeekRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'weekly'); + $strKey = sprintf('%s:H5:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'monthly', 0); $arrMoonRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'monthly'); $strStatus = '已完结'; + $strKey = sprintf('%s:H5:INDEX:RANK:%s:%s', $Request->DomainModel->d_domain, 'total', 'finished'); $arrEndNovelRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'total', $strStatus); // Title 模板 @@ -88,40 +96,57 @@ class Index extends BaseController // 获取路由附加的参数 $boolIsMobile = $Request->param('boolIsMobile'); - $NovelModel = new NovelModel; + $intTTL = 24 * 3600; + + $NovelModel = NovelModel::getInstance(); // 推荐等级为9的连载的 随机15个小说 - $arrRecommendSerializationNovel = $NovelModel->getHighLevelNovelOnCache($Request->DomainModel->d_domain, 13); + $strKey = sprintf('%s:PC:INDEX:TUIJIAN:%s', $Request->DomainModel->d_domain, 13); + + $arrRecommendSerializationNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 13, 0, NULL); + // 推荐等级为9的完结的 随机12个小说 - $arrRecommendEndNovel = $NovelModel->getFinishedNovelOnCache($Request->DomainModel->d_domain, 12); + $strKey = sprintf('%s:PC:INDEX:TUIJIAN:WANJIE:%s', $Request->DomainModel->d_domain, 12); + $arrRecommendEndNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 12, 0, "已完结"); + // 男生频道,最新的 随机5个小说 - $arrBoyNewsNovel = $NovelModel->getSexNovelOnCache($Request->DomainModel->d_domain, 5, 1); + $strKey = sprintf('%s:PC:INDEX:ZUIJIAN:SEX:%s:%s', $Request->DomainModel->d_domain, 1, 5); + $arrBoyNewsNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 5, 1, NULL, ['og_novel_update_time' => -1]); + // 男生频道,周排行榜 随机10个小说 - $arrBoyWeekRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 1, 'weekly'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'weekly', 1); + $arrBoyWeekRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 1, 'weekly'); // 男生频道,月排行榜 随机10个小说 - $arrBoyMoonRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 1, 'monthly'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'monthly', 1); + $arrBoyMoonRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 1, 'monthly'); // 男生频道,总排行榜 随机10个小说 - $arrBoyTotalRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 1, 'total'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'total', 1); + $arrBoyTotalRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 1, 'total'); // 女生频道,最新的 随机5个小说 - $arrGirlNewsNovel = $NovelModel->getSexNovelOnCache($Request->DomainModel->d_domain, 5, 2); + $strKey = sprintf('%s:PC:INDEX:ZUIJIAN:SEX:%s:%s', $Request->DomainModel->d_domain, 2, 5); + $arrGirlNewsNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 5, 2, NULL, ['og_novel_update_time' => -1]); // 女生频道,周排行榜 随机10个小说 - $arrGirlWeekRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 2, 'weekly'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'weekly', 2); + $arrGirlWeekRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 2, 'weekly'); // 女生频道,月排行榜 随机10个小说 - $arrGirlMoonRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 2, 'monthly'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'monthly', 2); + $arrGirlMoonRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 2, 'monthly'); // 女生频道,总排行榜 随机10个小说 - $arrBGirlTotalRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 2, 'total'); + $strKey = sprintf('%s:PC:INDEX:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'total', 2); + $arrBGirlTotalRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 2, 'total'); // 最近更新-随机50 - $arrNewsUpdateNovel = $NovelModel->getSexNovelOnCache($Request->DomainModel->d_domain, 50); + $strKey = sprintf('%s:PC:INDEX:ZUIJIAN:SEX:%s:%s', $Request->DomainModel->d_domain, 0, 50); + $arrNewsUpdateNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 50, 0, NULL, ['og_novel_update_time' => -1]); // Title 模板 ConverterMovel::setVal([ diff --git a/code/app/home/controller/Novel.php b/code/app/home/controller/Novel.php index 66f98c6..b5fcaa8 100644 --- a/code/app/home/controller/Novel.php +++ b/code/app/home/controller/Novel.php @@ -31,17 +31,20 @@ class Novel extends BaseController $boolIsMobile = $Request->param('boolIsMobile'); // 模拟数据-日排行榜 随机10个小说 - $NovelModel = new NovelModel; - $strKey = $Request->DomainModel->d_domain . ':RANK:'; + $NovelModel = NovelModel::getInstance(); + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'daily', 0); $arrDayRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'daily'); // 模拟数据-周排行榜 随机10个小说 + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'weekly', 0); $arrWeekRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'weekly'); // 模拟数据-月排行榜 随机10个小说 + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'monthly', 0); $arrMoonRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'monthly'); // 模拟数据-总排行榜 随机10个小说 + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s', $Request->DomainModel->d_domain, 'total', 0); $arrTotalRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'total'); // 模拟数据-新书榜 随机10个小说,n_id倒叙 @@ -55,6 +58,7 @@ class Novel extends BaseController // 模拟数据-完结榜 随机10个小说 $strStatus = '已完结'; + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s:%s', $Request->DomainModel->d_domain, 'total', 0, 'finished'); $arrEdnNovelRankNovel = $NovelModel->getRankNovelOnCache($strKey, 10, 0, 'total', $strStatus); // Title 模板 @@ -154,7 +158,7 @@ class Novel extends BaseController $strKey = sprintf("Novel:Library:%s:%s:%s:%s", $intGender, $strSearchCategory, $strSearchStatus, $intPage); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $arrPage = $NovelModel->getCustomPage01($strKey, $intPage, $intLimit, $intLifeTime, $intGender, $strSearchStatus); $strTemplate = ''; @@ -213,7 +217,7 @@ class Novel extends BaseController $boolIsMobile = $Request->param('boolIsMobile'); $strNovelChannel = $Request->param('strChannel'); // 区分男生/女生频道 - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $intType = ['boys' => 1, 'girls' => 2][$strNovelChannel]; $intLifeTime = 24 * 3600; @@ -358,15 +362,13 @@ class Novel extends BaseController public function getKanNovelInfo(Request $Request) { - // $arrNovel['og_novel_book_name'] = kan 小说 的 名字 - $intNId = $Request->param('n_id'); $intForgeId = $Request->param('n_forge_id'); $boolIsMobile = $Request->param('boolIsMobile'); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); // 小说详情 $arrNovel = $NovelModel->getNovelByNId($intNId); @@ -382,8 +384,8 @@ class Novel extends BaseController $arrNovel['category_pinyin'] = CategoryModel::getPinYinByZh($arrNovel['og_novel_category']); // 最后章节 - $ChapterModel = new ChapterModel; - $arrLatestChapter = $ChapterModel->getLatestChapter($intNId); + $ChapterModel = ChapterModel::getInstance(); + $arrLatestChapter = $ChapterModel->getLatestChapterByNId($intNId); $strFocus = ''; // 要点 @@ -397,19 +399,19 @@ class Novel extends BaseController } // 最近五个章节 - $arrChapter = $ChapterModel->getChapterAll([ + $arrChapter = $ChapterModel->findMany([ 'n_id' => $intNId, 'c_page' => 0, ], 10, ['c_sort_num' => -1]); // 点击数 - $NovelClicksModel = new NovelClicksModel; + $NovelClicksModel = NovelClicksModel::getInstance(); $arrNovelClicks = $NovelClicksModel->getStats($intNId); // 推荐小说 $arrRelateveNId = array_column($arrNovel['n_related_novel'], 'n_id'); - $arrRelateveNovel = $NovelModel->getNovel(['n_id' => [ + $arrRelateveNovel = $NovelModel->findMany(['n_id' => [ '$in' => $arrRelateveNId, ]], 10); @@ -468,7 +470,7 @@ class Novel extends BaseController $boolIsMobile = $Request->param('boolIsMobile'); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); // 小说详情 $arrNovel = $NovelModel->getNovelByNId($intNId); @@ -477,8 +479,8 @@ class Novel extends BaseController $arrNovel['category_pinyin'] = CategoryModel::getPinYinByZh($arrNovel['og_novel_category']); // 最后章节 - $ChapterModel = new ChapterModel; - $arrLatestChapter = $ChapterModel->getLatestChapter($intNId); + $ChapterModel = ChapterModel::getInstance(); + $arrLatestChapter = $ChapterModel->getLatestChapterByNId($intNId); $strFocus = ''; // 要点 @@ -492,19 +494,19 @@ class Novel extends BaseController } // 最近五个章节 - $arrChapter = $ChapterModel->getChapterAll([ + $arrChapter = $ChapterModel->findMany([ 'n_id' => $intNId, 'c_page' => 0, ], 10, ['c_sort_num' => -1]); // 点击数 - $NovelClicksModel = new NovelClicksModel; + $NovelClicksModel = NovelClicksModel::getInstance(); $arrNovelClicks = $NovelClicksModel->getStats($intNId); // 推荐小说 $arrRelateveNId = array_column($arrNovel['n_related_novel'], 'n_id'); - $arrRelateveNovel = $NovelModel->getNovel(['n_id' => [ + $arrRelateveNovel = $NovelModel->findMany(['n_id' => [ '$in' => $arrRelateveNId, ]], 10); @@ -559,12 +561,12 @@ class Novel extends BaseController $intNId = $Request->param('n_id'); // 小说详情 - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $arrNovel = $NovelModel->getNovelByNId($intNId); // 最新章节 - $ChapterModel = new ChapterModel; - $arrChapter = $ChapterModel->getLatestChapter($intNId); + $ChapterModel = ChapterModel::getInstance(); + $arrChapter = $ChapterModel->getLatestChapterByNId($intNId); // 上一章节和下一章节 $arrPreChapter = $arrNextChapter = []; @@ -649,15 +651,15 @@ class Novel extends BaseController $boolIsMobile = $Request->param('boolIsMobile'); // 小说详情 - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $intNId = $intNovelId; $arrNovel = $NovelModel->getNovelByNId($intNId); // 章节 - $ChapterModel = new ChapterModel; + $ChapterModel = ChapterModel::getInstance(); $arrFilter = ['n_id' => $intNId, 'c_sort_num' => $intChapterSort, 'c_page' => $intChapterPage]; - $arrChapter = $ChapterModel->getChapter($arrFilter); + $arrChapter = $ChapterModel->findOne($arrFilter); // 上一章节和下一章节 $arrPreChapter = $arrNextChapter = []; @@ -723,11 +725,11 @@ class Novel extends BaseController $boolIsMobile = $Request->param('boolIsMobile'); // 小说详情 - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $intNId = $intNovelId; $arrNovel = $NovelModel->getNovelByNId($intNId); - $ChapterModel = new ChapterModel; + $ChapterModel = ChapterModel::getInstance(); $strKey = sprintf('Nid:%s:Chapter-Page:%s:Order:%s', $intNId, $intPage, $strOrder); @@ -741,7 +743,7 @@ class Novel extends BaseController $arrSort = ['c_sort_num' => -1]; } - $arrPage = $ChapterModel->getCommonPageOnCache($strKey, $arrFilter, $intPage, $intLimit, $intLifeTime, $arrSort); + $arrPage = $ChapterModel->findPaginatedWithCache($arrFilter, $intPage, $intLimit, $arrSort, $strKey, $intLifeTime); $intButtomNum = 5; if ($boolIsMobile == false) { @@ -816,7 +818,7 @@ class Novel extends BaseController return response('请输入搜索的关键字', 400); } - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $arrFilter = [ '$or' => [ @@ -827,7 +829,7 @@ class Novel extends BaseController ] ]; - $arrPage = $NovelModel->getCommonPageOnCache($arrFilter, $intPage, $intLimit, [], $strKey); + $arrPage = $NovelModel->findPaginatedWithCache($arrFilter, $intPage, $intLimit, [], $strKey); $intButtomNum = 10; diff --git a/code/app/home/controller/User.php b/code/app/home/controller/User.php index 177974b..1a6cfff 100644 --- a/code/app/home/controller/User.php +++ b/code/app/home/controller/User.php @@ -17,12 +17,16 @@ class User extends BaseController // 获取路由附加的参数 $boolIsMobile = $Request->param('boolIsMobile'); - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); // 推荐等级为9的完结 随机15个小说 - $arrRecommendEndNovel = $NovelModel->getFinishedNovelOnCache($Request->DomainModel->d_domain, 8); + $intTTL = 24 * 3600; + $strKey = sprintf('%s:ALL:USER:TUIJIAN:WANJIE:%s', $Request->DomainModel->d_domain, 8); - $arrMoonRankNovel = $NovelModel->getRankNovelOnCache($Request->DomainModel->d_domain, 5, 2, 'monthly'); + $arrRecommendEndNovel = $NovelModel->getCommonNovelOnCache($strKey, $intTTL, 8, 0, "已完结"); + + $strKey = sprintf('%s:PC:NOVEL:RANK:%s:SEX:%s:%s', $Request->DomainModel->d_domain, 'monthly', 0); + $arrMoonRankNovel = $NovelModel->getRankNovelOnCache($strKey, 5, 0, 'monthly'); View::assign([ 'arrRecommendEndNovel' => $arrRecommendEndNovel, @@ -68,9 +72,9 @@ class User extends BaseController } } - - + + public function getPcIndex(Request $Request) { @@ -109,7 +113,7 @@ class User extends BaseController ['id' => 10, 'name' => '小说3', 'author' => '作者3'], ['id' => 11, 'name' => '小说3', 'author' => '作者3'], ['id' => 12, 'name' => '小说3', 'author' => '作者3'], - + ]; // 模拟数据-男生频道,最新的 随机5个小说 @@ -161,7 +165,7 @@ class User extends BaseController ]; - + // 模拟数据-男生频道,最新的 随机5个小说 $arrGirlNewsNovel = [ ['id' => 1, 'name' => '小说1', 'author' => '作者1'], @@ -248,7 +252,7 @@ class User extends BaseController 'arrGirlMoonRankNovel' => $arrGirlMoonRankNovel, 'arrBGirlTotalRankNovel' => $arrBGirlTotalRankNovel, 'arrAdBanner' => [], - + ]); if ($boolIsMobile) { @@ -258,7 +262,7 @@ class User extends BaseController } } - + public function test() { diff --git a/code/app/model/CategoryModel.php b/code/app/model/CategoryModel.php index 1164f1f..74caf0a 100644 --- a/code/app/model/CategoryModel.php +++ b/code/app/model/CategoryModel.php @@ -82,6 +82,37 @@ class CategoryModel extends MongoModel return self::$arrCategory; } + /** + * Undocumented function + * + * @param integer $intType + * @param boolean $boolOnlyValue + * @return array + */ + static public function getCategoryByType(int $intType, bool $boolOnlyValue = true): array + { + $arrCategory = []; + switch ($intType) { + case 1: + if ($boolOnlyValue) { + $arrCategory = array_values(CategoryModel::getManCategory()); + } else { + $arrCategory = CategoryModel::getManCategory(); + } + + + break; + case 2: + if ($boolOnlyValue) { + $arrCategory = array_values(CategoryModel::getWomenCategory()); + } else { + $arrCategory = CategoryModel::getWomenCategory(); + } + break; + } + return $arrCategory; + } + /** * get a category of man's novels * diff --git a/code/app/model/ChapterModel.php b/code/app/model/ChapterModel.php index 1dd6e96..eca5877 100644 --- a/code/app/model/ChapterModel.php +++ b/code/app/model/ChapterModel.php @@ -34,21 +34,21 @@ class ChapterModel extends MongoModel /** * Undocumented function * - * @param string $strKey * @param array $arrFilter * @param integer $intPage * @param integer $intLimit - * @param integer $intLifeTime * @param array $arrSort + * @param string $strKey + * @param integer $intLifeTime * @return array */ - public function getCommonPageOnCache( - string $strKey, + public function findPaginatedWithCache( array $arrFilter, int $intPage = 1, int $intLimit = 20, + array $arrSort = ['c_sort_num' => 1], + string $strKey = '', int $intLifeTime = 24 * 3600, - array $arrSort = ['c_sort_num' => 1] ): array { try { @@ -61,7 +61,6 @@ class ChapterModel extends MongoModel $arrOptions = array_merge($arrOptions, [ 'sort' => $arrSort, - // 'limit' => $intCount * 3, 'skip' => ($intPage - 1) * $intLimit, 'limit' => $intLimit, ]); @@ -101,17 +100,20 @@ class ChapterModel extends MongoModel * @param array $arrFilter * @return null|array */ - public function getChapter(array $arrFilter): null|array + public function findOne(array $arrFilter, array $arrOptions = [], bool $boolReadContent = true): null|array { - - $arrOptions = self::$arrChapterOptions; + if (empty($arrOptions)) { + $arrOptions = self::$arrChapterOptions; + } $arrChapter = $this->getOne($arrFilter, $arrOptions); if (!empty($arrChapter)) { applyToKeys($arrChapter, ['created_at', 'updated_at'], 'formatMongoDate'); - $arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']); + if ($boolReadContent) { + $arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']); + } } return $arrChapter; @@ -123,7 +125,7 @@ class ChapterModel extends MongoModel * @param integer $intNId * @return null|array */ - public function getLatestChapter(int $intNId, bool $boolReadContent = true): null|array + public function getLatestChapterByNId(int $intNId, bool $boolReadContent = true): null|array { $arrFilter = ['n_id' => $intNId, 'c_page' => 0]; @@ -131,32 +133,30 @@ class ChapterModel extends MongoModel $arrOptions['sort'] = ['c_sort_num' => -1]; - $arrChapter = $this->getOne($arrFilter, $arrOptions); - - if (!empty($arrChapter)) { - applyToKeys($arrChapter, ['created_at', 'updated_at'], 'formatMongoDate'); - $arrChapter['content'] = ''; - if ($boolReadContent) { - $arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']); - } - } - - return $arrChapter ?? []; + return $this->findOne($arrFilter, $arrOptions, $boolReadContent); } /** * Undocumented function * - * @param integer $intNId + * @param array $arrFilter + * @param integer $intCount + * @param array $arrSort + * @param array $arrOptions + * @param boolean $boolReadContent * @return null|array */ - public function getChapterAll(array $arrFilter, int $intCount = 10, array $arrSort = ['c_sort_num' => -1], bool $boolReadContent = false): null|array + public function findMany(array $arrFilter, int $intCount = 0, array $arrSort = ['c_sort_num' => -1], array $arrOptions = [], bool $boolReadContent = false): null|array { - $arrOptions = self::$arrChapterOptions; + if (empty($arrOptions)) { + $arrOptions = self::$arrChapterOptions; + } $arrOptions['sort'] = $arrSort; - $arrOptions['limit'] = $intCount; + if ($intCount > 0) { + $arrOptions['limit'] = $intCount; + } $Cursor = $this->getCol()->find($arrFilter, $arrOptions); @@ -206,7 +206,7 @@ class ChapterModel extends MongoModel 'c_page' => -1, ]; - $arrChapterAll = $this->getChapterAll($arrFilter, 1, $arrSort); + $arrChapterAll = $this->findMany($arrFilter, 1, $arrSort); return $arrChapterAll[0] ?? []; } @@ -243,7 +243,7 @@ class ChapterModel extends MongoModel 'c_page' => 1, ]; - $arrChapterAll = $this->getChapterAll($arrFilter, 1, $arrSort); + $arrChapterAll = $this->findMany($arrFilter, 1, $arrSort); return $arrChapterAll[0] ?? []; } } diff --git a/code/app/model/MongoModel.php b/code/app/model/MongoModel.php index a5c0f17..ac21ca5 100644 --- a/code/app/model/MongoModel.php +++ b/code/app/model/MongoModel.php @@ -18,7 +18,7 @@ abstract class MongoModel /** * Undocumented variable * - * @var GanRaoMaModel + * @var static */ static public $obj; diff --git a/code/app/model/NovelClicksModel.php b/code/app/model/NovelClicksModel.php index a07f804..5e5c1be 100644 --- a/code/app/model/NovelClicksModel.php +++ b/code/app/model/NovelClicksModel.php @@ -24,7 +24,7 @@ class NovelClicksModel extends MongoModel public function addStatis(int $intNId) { - $NovelModel = new NovelModel; + $NovelModel = NovelModel::getInstance(); $Novel = $NovelModel->getCol()->findOne([ "n_id" => $intNId, ]); diff --git a/code/app/model/NovelModel.php b/code/app/model/NovelModel.php index cd5eacb..727a8be 100644 --- a/code/app/model/NovelModel.php +++ b/code/app/model/NovelModel.php @@ -20,7 +20,7 @@ class NovelModel extends MongoModel */ protected $strCollection = 'novel'; - static public $arrNovelOptions = [ + static public $arrOptions = [ 'typeMap' => [ 'root' => 'array', 'document' => 'array', @@ -35,7 +35,6 @@ class NovelModel extends MongoModel 'n_source_id' => 0, 'n_page_title' => 0, 'og_description' => 0, - 'og_novel_latest_chapter_name' => 0, 'og_image' => 0, 'og_novel_latest_chapter_url' => 0, 'og_novel_read_url' => 0, @@ -80,13 +79,19 @@ class NovelModel extends MongoModel * @param integer $intNId * @return null|array */ - public function getNovel(array $arrFilter, int $intCount = 10, array $arrSort = []): null|array + public function findMany(array $arrFilter, int $intCount = 10, array $arrSort = [], array $arrOptions = []): null|array { - $arrOptions = self::$arrNovelOptions; + if (empty($arrOptions)) { + $arrOptions = self::$arrOptions; + } - $arrOptions['sort'] = $arrSort; + if ($intCount > 0) { + $arrOptions['limit'] = $intCount; + } - $arrOptions['limit'] = $intCount; + if (empty($arrSort)) { + $arrOptions['sort'] = $arrSort; + } $arrFilter['n_have_chapter'] = 1; @@ -102,6 +107,64 @@ class NovelModel extends MongoModel } + /** + * 查询多条数据并缓存 + * + * @param array $arrFilter + * @param integer $intCount + * @param array $arrSort + * @param array $arrOptions + * @param string $strKey + * @param integer $intLifeTime + * @return null|array + */ + public function findManyWithCache( + array $arrFilter, + int $intCount = 0, + array $arrSort = [], + array $arrOptions = [], + string $strKey = '', + int $intLifeTime = 24 * 3600, + ): null|array { + try { + + $arrResult = $this->checkCacheByKey($strKey); + if ($arrResult !== NULL) { + return $arrResult; + } + + $arrResult = $this->findMany($arrFilter, $intCount, $arrSort, $arrOptions); + + $this->setCacheByKey($strKey, $arrResult, $intLifeTime); + + return $arrResult; + } catch (\Exception $e) { + return []; + } + } + + + /** + * find one novel + * + * @param array $arrFilter + * @param array $arrOptions + * @return null|array + */ + public function findOne(array $arrFilter, array $arrOptions = []): null|array + { + if (empty($arrOptions)) { + $arrOptions = self::$arrOptions; + } + + $arrNovel = $this->getOne($arrFilter, $arrOptions); + + if (!empty($arrNovel)) { + applyToKeys($arrNovel, ['created_at', 'updated_at'], 'formatMongoDate'); + } + + return $arrNovel; + } /** * 根据小说ID查询指定小说 @@ -112,12 +175,7 @@ class NovelModel extends MongoModel public function getNovelByNId(int $intNId): null|array { $arrFilter = ['n_id' => $intNId, 'n_have_chapter' => 1]; - $arrOptions = self::$arrNovelOptions; - $arrNovel = $this->getOne($arrFilter, $arrOptions); - applyToKeys($arrNovel, ['created_at', 'updated_at'], 'formatMongoDate'); - - - return $arrNovel; + return $this->findOne($arrFilter); } @@ -125,12 +183,12 @@ class NovelModel extends MongoModel * 查询符合条件的随机多条小说 * * @param string $strKey - * @param integer $intCount - * @param int $intLifeTime * @param array $arrFilter - * @return void + * @param integer $intCount + * @param integer $intLifeTime + * @return array */ - public function getRandNovel(string $strKey, $intCount = 10, int $intLifeTime = 24 * 3600, array $arrFilter = []): array + public function getRandNovelWithCache(array $arrFilter = [], int $intCount = 10, string $strKey = '', int $intLifeTime = 24 * 3600,): array { try { @@ -141,14 +199,69 @@ class NovelModel extends MongoModel $arrFilter['n_have_chapter'] = 1; - $pipeline = [ + $arrPipeline = [ ['$match' => $arrFilter], ['$sample' => ['size' => $intCount]] ]; - $Novel = $this->getCol()->aggregate($pipeline); + $Novel = $this->getCol()->aggregate($arrPipeline); - $arrResult = iterator_to_array($Novel); + $arrResult = iterator_to_array($Novel); + + $this->setCacheByKey($strKey, $arrResult, $intLifeTime); + + return $arrResult; + } catch (\Exception $e) { + return []; + } + } + + /** + * 通用小说分页 + * + * @param array $arrFilter + * @param integer $intPage + * @param integer $intLimit + * @param array $arrSort + * @param string $strKey + * @param int $intLifeTime + * @return array + */ + public function findPaginatedWithCache( + array $arrFilter, + int $intPage = 1, + int $intLimit = 20, + array $arrSort = ['n_level' => -1], + string $strKey = '', + int $intLifeTime = 24 * 3600, + ): array { + try { + $arrFilter['n_have_chapter'] = 1; + + $arrResult = $this->checkCacheByKey($strKey); + if ($arrResult !== NULL) { + return $arrResult; + } + $arrOptions = self::$arrOptions; + $arrOptions['sort'] = $arrSort; + $arrOptions['skip'] = ($intPage - 1) * $intLimit; + $arrOptions['limit'] = $intLimit; + + $Cursor = $this->getCol()->find($arrFilter, $arrOptions); + + $intTotal = $this->getCol()->countDocuments($arrFilter); + + $arrResult = iterator_to_array($Cursor); + + shuffle($arrResult); + + $arrResult = [ + 'data' => $arrResult, + 'total' => $intTotal, + 'page' => $intPage, + 'limit' => $intLimit, + 'pages' => ceil($intTotal / $intLimit) + ]; $this->setCacheByKey($strKey, $arrResult, $intLifeTime); @@ -196,70 +309,50 @@ class NovelModel extends MongoModel if ($strStatus !== NULL) { $arrFilter['og_novel_status'] = $strStatus; } - return $this->getCommonPageOnCache($arrFilter, $intPage, $intLimit, $arrSort, $strKey, $intLifeTime,); + return $this->findPaginatedWithCache($arrFilter, $intPage, $intLimit, $arrSort, $strKey, $intLifeTime,); } /** - * 通用小说分页 + * Undocumented function * * @param array $arrFilter - * @param integer $intPage - * @param integer $intLimit + * @param integer $intCount * @param array $arrSort * @param string $strKey - * @param int $intLifeTime + * @param integer $intLifeTime * @return array */ - public function getCommonPageOnCache( + public function findManyShuffledWithCache( array $arrFilter, - int $intPage = 1, - int $intLimit = 20, - array $arrSort = ['n_level' => -1], + int $intCount = 10, + array $arrSort = [], string $strKey = '', int $intLifeTime = 24 * 3600, ): array { try { - $arrFilter['n_have_chapter'] = 1; - $arrResult = $this->checkCacheByKey($strKey); + $arrResult = $this->checkCacheByKey($strKey); if ($arrResult !== NULL) { - return $arrResult; + return $arrResult; } - $arrOptions = [ - 'sort' => $arrSort, - 'skip' => ($intPage - 1) * $intLimit, - 'limit' => $intLimit, - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'og_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_latest_chapter_name' => 1, - 'og_novel_update_time' => 1, - 'og_novel_status' => 1, - ], - ]; + $arrFilter['n_have_chapter'] = 1; + $arrOptions = self::$arrOptions; + + if (!empty($arrSort)) { + $arrOptions['sort'] = $arrSort; + } + + $arrOptions['limit'] = $intCount * 3; $Cursor = $this->getCol()->find($arrFilter, $arrOptions); - $intTotal = $this->getCol()->countDocuments($arrFilter); - $arrResult = iterator_to_array($Cursor); shuffle($arrResult); - $arrResult = [ - 'data' => $arrResult, - 'total' => $intTotal, - 'page' => $intPage, - 'limit' => $intLimit, - 'pages' => ceil($intTotal / $intLimit) - ]; + $arrResult = array_slice($arrResult, 0, $intCount); $this->setCacheByKey($strKey, $arrResult, $intLifeTime); @@ -269,7 +362,6 @@ class NovelModel extends MongoModel } } - /** * Undocumented function * @@ -291,249 +383,22 @@ class NovelModel extends MongoModel ): array { try { - $arrResult = $this->checkCacheByKey($strKey); - if ($arrResult !== NULL) { - return $arrResult; - } + $arrFilter = []; - $arrFilter = ['n_have_chapter' => 1,]; - - switch ($intType) { - case 1: - $arrCategory = array_values(CategoryModel::getManCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - break; - case 2: - $arrCategory = array_values(CategoryModel::getWomenCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - break; + if (in_array($intType, [1, 2])) { + $arrFilter['og_novel_category'] = CategoryModel::getCategoryByType($intType); } if ($strStatus !== NULL) { $arrFilter['og_novel_status'] = $strStatus; } - $arrOptions = [ - 'sort' => $arrSort, - 'limit' => $intCount * 3, - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'og_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_latest_chapter_name' => 1, - 'og_novel_update_time' => 1, - 'og_novel_status' => 1, - ], - ]; - - $Cursor = $this->getCol()->find($arrFilter, $arrOptions); - $arrResult = iterator_to_array($Cursor); - - shuffle($arrResult); - - $arrResult = array_slice($arrResult, 0, $intCount); - - $this->setCacheByKey($strKey, $arrResult, $intLifeTime); - - return $arrResult; + return $this->findManyShuffledWithCache($arrFilter, $intCount, $arrSort, $strKey, $intLifeTime); } catch (\Exception $e) { return []; } } - - /** - * Undocumented function - * - * @param string $strSign - * @param integer $intCount - * @param integer $intType - * @param integer $intLevel - * @return array - */ - public function getHighLevelNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0, $strStatus = NULL): array - { - try { - $strKey = sprintf('%s:PC:HOME:FIRST', $strSign); - - $intLifeTime = 24 * 3600; - - $arrResult = $this->checkCacheByKey($strKey); - - if ($arrResult !== NULL) { - return $arrResult; - } - - $arrFilter = ['n_have_chapter' => 1,]; - switch ($intType) { - case 1: - $arrCategory = array_values(CategoryModel::getManCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - break; - case 2: - $arrCategory = array_values(CategoryModel::getWomenCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - break; - } - - if ($strStatus !== NULL) { - $arrFilter['og_novel_status'] = $strStatus; - } - - $arrOptions = [ - 'sort' => ['n_level' => -1], - 'limit' => $intCount * 3, - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'n_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_status' => 1, - ], - ]; - - - $Cursor = $this->getCol()->find($arrFilter, $arrOptions); - $arrResult = iterator_to_array($Cursor); - - shuffle($arrResult); - - $arrResult = array_slice($arrResult, 0, $intCount); - - $this->setCacheByKey($strKey, $arrResult, $intLifeTime); - - return $arrResult; - } catch (\Exception $e) { - return []; - } - } - - - /** - * Undocumented function - * - * @param string $strSign - * @param integer $intCount - * @return array - */ - public function getFinishedNovelOnCache(string $strSign, int $intCount = 10): array - { - try { - $strKey = sprintf('%s:PC:HOME:FINISHED', $strSign); - - $intLifeTime = 24 * 3600; - - $arrResult = $this->checkCacheByKey($strKey); - if ($arrResult !== NULL) { - return $arrResult; - } - $arrFilter = [ - 'og_novel_status' => "已完结", - 'n_have_chapter' => 1, - ]; - - $arrOptions = [ - 'sort' => ['og_novel_update_time' => -1], - 'limit' => $intCount * 3, - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'og_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_status' => 1, - ], - ]; - - $Cursor = $this->getCol()->find($arrFilter, $arrOptions); - $arrResult = iterator_to_array($Cursor); - - shuffle($arrResult); - - $arrResult = array_slice($arrResult, 0, $intCount); - - $this->setCacheByKey($strKey, $arrResult, $intLifeTime); - - return $arrResult; - } catch (\Exception $e) { - return []; - } - } - - /** - * Undocumented function - * - * @param string $strSign - * @param integer $intCount - * @param integer $intType - * @param integer $intLevel - * @return array - */ - public function getSexNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0): array - { - try { - $strKey = sprintf('%s:PC:HOME:SIX:%s', $strSign, $intType); - - $intLifeTime = 24 * 3600; - - $arrResult = $this->checkCacheByKey($strKey); - if ($arrResult !== NULL) { - return $arrResult; - } - - $arrFilter = ['n_have_chapter' => 1,]; - - if ($intType == 1) { - $arrCategory = array_values(CategoryModel::getManCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - } elseif ($intType == 2) { - $arrCategory = array_values(CategoryModel::getWomenCategory()); - $arrFilter['og_novel_category'] = ['$in' => $arrCategory]; - } - - $arrOptions = [ - 'sort' => ['og_novel_update_time' => -1], - 'limit' => $intCount * 3, - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'og_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_update_time' => 1, - 'og_novel_latest_chapter_name' => 1, - 'og_novel_status' => 1, - ], - ]; - - $Cursor = $this->getCol()->find($arrFilter, $arrOptions); - $arrResult = iterator_to_array($Cursor); - - shuffle($arrResult); - - $arrResult = array_slice($arrResult, 0, $intCount); - - $this->setCacheByKey($strKey, $arrResult, $intLifeTime); - - return $arrResult; - } catch (\Exception $e) { - return []; - } - } - - - /** * Undocumented function * @@ -547,11 +412,9 @@ class NovelModel extends MongoModel * * total 总 * @return array */ - public function getRankNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0, $strDateType = 'daily', $strStatus = NULL): array + public function getRankNovelOnCache(string $strKey, int $intCount = 10, int $intType = 0, $strDateType = 'daily', $strStatus = NULL): array { try { - $strKey = sprintf('%s:PC:HOME:SIX:%s:TYPE:%s', $strSign, $intType, $strDateType); - $intLifeTime = 24 * 3600; $arrResult = $this->checkCacheByKey($strKey); @@ -566,15 +429,10 @@ class NovelModel extends MongoModel $arrFilter['og_novel_status'] = $strStatus; } - if ($intType == 1) { - $arrCategory = array_values(CategoryModel::getManCategory()); - $arrFilter['n_category'] = ['$in' => $arrCategory]; - } elseif ($intType == 2) { - $arrCategory = array_values(CategoryModel::getWomenCategory()); - $arrFilter['n_category'] = ['$in' => $arrCategory]; + if (in_array($intType, [1, 2])) { + $arrFilter['og_novel_category'] = CategoryModel::getCategoryByType($intType); } - $arrOptions = [ 'sort' => ['nc_clicks' => -1], 'limit' => $intCount * 3, @@ -583,7 +441,7 @@ class NovelModel extends MongoModel ], ]; - $NovelClicksModel = new NovelClicksModel; + $NovelClicksModel = NovelClicksModel::getInstance(); $Cursor = $NovelClicksModel->getCol()->find($arrFilter, $arrOptions); @@ -592,20 +450,11 @@ class NovelModel extends MongoModel $arrNId[] = $Doc['n_id']; } + $arrFilter = ['n_id' => ['$in' => $arrNId]]; + $arrOptions = self::$arrOptions; $Cursor = $this->getCol()->find( - ['n_id' => ['$in' => $arrNId]], - [ - 'projection' => [ - 'n_id' => 1, - 'og_novel_book_name' => 1, - 'og_novel_pin_yin' => 1, - 'n_cover_path' => 1, - 'n_novel_author' => 1, - 'og_novel_category' => 1, - 'og_description' => 1, - 'og_novel_status' => 1, - ], - ] + $arrFilter, + $arrOptions ); $arrResult = iterator_to_array($Cursor); diff --git a/code/app/task/crawler/zw630/page/ChapterPage.php b/code/app/task/crawler/zw630/page/ChapterPage.php index 72478dd..adfbe97 100644 --- a/code/app/task/crawler/zw630/page/ChapterPage.php +++ b/code/app/task/crawler/zw630/page/ChapterPage.php @@ -122,8 +122,8 @@ class ChapterPage extends BasePage 'n_id' => $intNId, ]; - $ChapterModel = new ChapterModel; - $arrLasteChapter = $ChapterModel->getLatestChapter($intNId, false); + $ChapterModel = ChapterModel::getInstance(); + $arrLasteChapter = $ChapterModel->getLatestChapterByNId($intNId, false); if (!empty($arrLasteChapter)) { $arrUpdate['$set']['n_have_chapter'] = 1; diff --git a/code/app/task/crawler/zw630/page/NovelPage.php b/code/app/task/crawler/zw630/page/NovelPage.php index 882ffa1..8608ac4 100644 --- a/code/app/task/crawler/zw630/page/NovelPage.php +++ b/code/app/task/crawler/zw630/page/NovelPage.php @@ -226,7 +226,7 @@ class NovelPage extends BasePage */ protected $arrChapters = NULL; - public function getLatestChaptersList(): array + public function getLatestChapterByNIdsList(): array { if ($this->arrLatestChapters === NULL) { $this->arrLatestChapters = []; @@ -286,7 +286,7 @@ class NovelPage extends BasePage { $this->arrNextPageArgs = []; if ($this->arrData['page'] == 0) { - $this->getLatestChaptersList(); + $this->getLatestChapterByNIdsList(); $this->getChapters(); $arrLatestChapterFirstBlock = count($this->arrLatestChapters) > 0 ? $this->arrLatestChapters[0] : null; diff --git a/code/extend/db/NovelManage.php b/code/extend/db/NovelManage.php index 0120566..df48fae 100644 --- a/code/extend/db/NovelManage.php +++ b/code/extend/db/NovelManage.php @@ -45,7 +45,7 @@ class NovelManage extends Command $ChapterCol = $Db->selectCollection('chapter'); - $ChapterModel = new ChapterModel; + $ChapterModel = ChapterModel::getInstance(); $intBatchSize = 1000; @@ -82,7 +82,7 @@ class NovelManage extends Command $arrUpdateData['category_sex_zh'] = CategoryModel::checkSexZh($arrDoc['og_novel_category']); } - $arrLasteChapter = $ChapterModel->getLatestChapter($arrDoc['n_id'], false); + $arrLasteChapter = $ChapterModel->getLatestChapterByNId($arrDoc['n_id'], false); if (!empty($arrLasteChapter)) { $arrUpdateData['n_have_chapter'] = 1;