This commit is contained in:
Default
2025-04-09 02:13:20 +08:00
parent 466abd8cd6
commit 064a5d4a92
13 changed files with 319 additions and 408 deletions

View File

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

View File

@@ -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] ?? [];
}
}

View File

@@ -18,7 +18,7 @@ abstract class MongoModel
/**
* Undocumented variable
*
* @var GanRaoMaModel
* @var static
*/
static public $obj;

View File

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

View File

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