优化代码

This commit is contained in:
Default
2025-04-04 00:48:11 +08:00
parent efbbfc3e0f
commit 4b97fe8a46
9 changed files with 469 additions and 379 deletions

View File

@@ -44,17 +44,6 @@ class CategoryModel extends MongoModel
]
];
/**
* Undocumented function
*
* @param string $strName
* @return string
*/
static public function checkSex(string $strName): string
{
return in_array($strName, self::$arrMan) ? 'man' : 'women';
}
static $arrCategoryPinYin = [
1 => 'xuanhuan-xiuzhen', // 玄幻-修真
2 => 'junshi-xiaoshuo', // 军史-小说
@@ -83,24 +72,56 @@ class CategoryModel extends MongoModel
];
/**
* get category of the novels
*
* @return array
*/
static public function getCategory(): array
{
return self::$arrCategory;
}
/**
* get a category of man's novels
*
* @return array
*/
static public function getManCategory(): array
{
return self::$arrMan;
}
/**
* get a category of the woman novels
*
* @return array
*/
static public function getWomenCategory(): array
{
return self::$arrWomen;
}
/**
* get pinyin based on chinese characters
*
* @param string $strZh
* @return string
*/
static public function getPinYinByZh(string $strZh)
{
$intKey = array_search($strZh, self::$arrCategory);
return self::$arrCategoryPinYin[$intKey];
}
/**
* determine the gender of the novel category
*
* @param string $strName
* @return string
*/
static public function checkSex(string $strName): string
{
return in_array($strName, self::$arrMan) ? 'man' : 'women';
}
}

View File

@@ -49,7 +49,7 @@ class ChapterModel extends MongoModel
* @param array $arrSort
* @return array
*/
public function getCommonNovelPageOnCache(
public function getCommonPageOnCache(
string $strKey,
array $arrFilter,
int $intPage = 1,
@@ -149,7 +149,7 @@ class ChapterModel extends MongoModel
$arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']);
}
return $arrChapter;
return $arrChapter??[];
}
/**

View File

@@ -7,6 +7,7 @@ namespace app\model;
use db\mongo\MongoBase;
use ddl\DDLManage;
use MongoDB\Collection;
use think\facade\Cache;
/**
* @mixin think\Model
@@ -77,4 +78,41 @@ abstract class MongoModel
{
return $this->getCol()->findOne($arrFilter, $arrOptions);
}
/**
* Undocumented function
*
* @param string $strKey
* @return array|object|NULL
*/
public function checkCacheByKey(string $strKey): array|object|NULL
{
if (config('view.view_data_cache') == false) {
return NULL;
}
if (!empty($strKey)) {
$arrResult = Cache::get($strKey);
if (!empty($arrResult)) {
return $arrResult;
}
}
return NULL;
}
/**
* Undocumented function
*
* @param string $strKey
* @param mixed $mixedData
* @param integer $intLifeTime
* @return void
*/
public function setCacheByKey(string $strKey, mixed $mixedData, int $intLifeTime)
{
if (!empty($strKey) && !empty($mixedData)) {
Cache::set($strKey, $mixedData, $intLifeTime);
}
}
}

View File

@@ -68,9 +68,8 @@ class NovelModel extends MongoModel
['$set' => ['n_level' => $intLevel]]
);
$intUpdateNum = $UpdateResult->getModifiedCount();
$UpdateResult->getModifiedCount();
} catch (Exception $e) {
return false;
}
@@ -78,15 +77,13 @@ class NovelModel extends MongoModel
}
/**
* Undocumented function
* 直接读库查询大量小说
*
* @param integer $intNId
* @return null|array
*/
public function getNovelAll(array $arrFilter, int $intCount = 10, array $arrSort = []): null|array
public function getNovel(array $arrFilter, int $intCount = 10, array $arrSort = []): null|array
{
// $arrFilter = ['n_id' => $intNId, 'c_page' => 0];
$arrOptions = self::$arrNovelOptions;
$arrOptions['sort'] = $arrSort;
@@ -100,7 +97,6 @@ class NovelModel extends MongoModel
$arrResult = iterator_to_array($Cursor);
foreach ($arrResult as &$arrItem) {
applyToKeys($arrItem, ['created_at', 'updated_at'], 'formatMongoDate');
}
@@ -110,12 +106,12 @@ class NovelModel extends MongoModel
/**
* Undocumented function
* 根据小说ID查询指定小说
*
* @param integer $intNId
* @return array
*/
public function getNovelInfo(int $intNId): null|array
public function getNovelByNId(int $intNId): null|array
{
$arrFilter = ['n_id' => $intNId];
$arrOptions = self::$arrNovelOptions;
@@ -131,7 +127,7 @@ class NovelModel extends MongoModel
/**
* Undocumented function
* 查询符合条件的随机多条小说
*
* @param string $strKey
* @param integer $intCount
@@ -143,42 +139,42 @@ class NovelModel extends MongoModel
{
try {
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$pipeline = [
['$match' => $arrFilter],
['$sample' => ['size' => $intCount]]
];
$Novel = $this->getCol()->aggregate($pipeline);
$arrResult = iterator_to_array($Novel);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
$pipeline = [
['$match' => $arrFilter],
['$sample' => ['size' => $intCount]]
];
$Novel = $this->getCol()->aggregate($pipeline);
$arrResult = iterator_to_array($Novel);
$this->setCacheByKey($strKey, $arrResult, $intLifeTime);
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
/**
* Undocumented function
* 用于Home下的书库分页
*
* @param string $strKey
* @param integer $intPage
* @param integer $intLimit
* @param int $intLifeTime
* @param integer $intCount
* @param integer $intType
* @param string $strStatus
* @param array $arrSort
* @return array
*/
public function getCommonNovelPageOnCache(
public function getCustomPage01(
string $strKey,
int $intPage = 1,
int $intLimit = 20,
@@ -186,71 +182,89 @@ class NovelModel extends MongoModel
int $intType = 0,
$strStatus = NULL,
array $arrSort = ['n_level' => -1]
): array {
$arrFilter = [];
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;
}
return $this->getCommonPageOnCache($arrFilter, $intPage, $intLimit, $arrSort, $strKey, $intLifeTime,);
}
/**
* 通用小说分页
*
* @param array $arrFilter
* @param integer $intPage
* @param integer $intLimit
* @param array $arrSort
* @param string $strKey
* @param int $intLifeTime
* @return array
*/
public function getCommonPageOnCache(
array $arrFilter,
int $intPage = 1,
int $intLimit = 20,
array $arrSort = ['n_level' => -1],
string $strKey = '',
int $intLifeTime = 24 * 3600,
): array {
try {
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [];
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' => $arrSort,
// 'limit' => $intCount * 3,
'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,
],
];
$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)
];
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
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,
],
];
$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);
return $arrResult;
} catch (\Exception $e) {
return [];
@@ -279,56 +293,54 @@ class NovelModel extends MongoModel
): array {
try {
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [];
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' => $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);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
$arrFilter = [];
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' => $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;
} catch (\Exception $e) {
return [];
@@ -352,54 +364,51 @@ class NovelModel extends MongoModel
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [];
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);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
$this->setCacheByKey($strKey, $arrResult, $intLifeTime);
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) {
@@ -422,40 +431,37 @@ class NovelModel extends MongoModel
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [
'og_novel_status' => "已完结",
];
$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);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
$arrFilter = [
'og_novel_status' => "已完结",
];
$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) {
@@ -479,52 +485,49 @@ class NovelModel extends MongoModel
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
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]
];
} else {
$arrFilter = [];
}
$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);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
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]
];
} else {
$arrFilter = [];
}
$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) {
@@ -554,74 +557,69 @@ class NovelModel extends MongoModel
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
$arrResult = $this->checkCacheByKey($strKey);
if ($arrResult !== NULL) {
return $arrResult;
}
$arrFilter = [
'nc_type' => $strDateType,
];
// if (empty($arrResult)) {
if (true) {
$arrFilter = [
'nc_type' => $strDateType,
];
if ($strStatus !== NULL) {
$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];
}
$arrOptions = [
'sort' => ['nc_clicks' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
],
];
$NovelClicksModel = new NovelClicksModel;
$Cursor = $NovelClicksModel->getCol()->find($arrFilter, $arrOptions);
$arrNId = [];
foreach ($Cursor as $Doc) {
$arrNId[] = $Doc['n_id'];
}
$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,
],
]
);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
if ($strStatus !== NULL) {
$arrFilter['og_novel_status'] = $strStatus;
}
return $arrResult;
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];
}
$arrOptions = [
'sort' => ['nc_clicks' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
],
];
$NovelClicksModel = new NovelClicksModel;
$Cursor = $NovelClicksModel->getCol()->find($arrFilter, $arrOptions);
$arrNId = [];
foreach ($Cursor as $Doc) {
$arrNId[] = $Doc['n_id'];
}
$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,
],
]
);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
$this->setCacheByKey($strKey, $arrResult, $intLifeTime);
return $arrResult;
} catch (\Exception $e) {
return [];
}
@@ -643,22 +641,5 @@ class NovelModel extends MongoModel
'strPinYin' => $intPinYin
]);
return (string)$strUrl;
// $Request = request();
// $strFomart = $Request->DomainModel->d_novel_info_url_fomart;
// return str_replace(
// [
// '{intNId}',
// '{strPinYin}',
// ],
// [
// $intNId,
// $intPinYin
// ],
// $strFomart
// );
// return $strUrl;
}
}