Skip to content

Commit

Permalink
⚡ 不再在每次请求时创建 schema 以节省性能
Browse files Browse the repository at this point in the history
  • Loading branch information
cfdxkk committed Mar 10, 2024
1 parent f2d2405 commit 1816fac
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 182 deletions.
14 changes: 9 additions & 5 deletions src/dbPool/schema/DanmakuSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Schema } from 'mongoose'

/**
* 弾幕数据
*/
const DanmakuSchema = {
class DanmakuSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** KVID 视频 ID - 非空 */
videoId: { type: Number, required: true },
/** 弹幕发送者的用户的 UID - 非空 */
Expand All @@ -22,9 +24,11 @@ const DanmakuSchema = {
enableRainbow: { type: Boolean, required: false, default: false },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'danmaku',
collectionName = 'danmaku'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export default DanmakuSchema
export const DanmakuSchema = new DanmakuSchemaFactory()
21 changes: 14 additions & 7 deletions src/dbPool/schema/SequenceSchema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Schema } from 'mongoose'

/**
* 自增序列
* @param _id 自增的项,比如:videoId
* @param sequenceValue 自增的值
*/
export const SequenceValueSchema = {
schema: {
_id: { type: String, unique: true, required: true }, // 例如 'videoId'
export class SequenceValueSchemaFactory {
schema = {
/** 自增的项,比如:videoId */
_id: { type: String, unique: true, required: true },
/** 自增的值 */
sequenceValue: { type: Number, required: true },
},
collectionName: 'sequence-value',
}
/** MongoDB 集合名 */
collectionName = 'sequence-value'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export const SequenceValueSchema = new SequenceValueSchemaFactory()
37 changes: 23 additions & 14 deletions src/dbPool/schema/UserSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Schema } from 'mongoose'

/**
* 用户安全认证集合
*/
export const UserAuthSchema = {
class UserAuthSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** 用户的 UID - 非空 */
uid: { type: Number, unique: true, required: true },
/** 用户邮箱 - 非空 */
Expand All @@ -23,11 +25,13 @@ export const UserAuthSchema = {

/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'user-auth',
collectionName = 'user-auth'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export const UserAuthSchema = new UserAuthSchemaFactory()

/**
* 用户的个人标签
Expand Down Expand Up @@ -62,9 +66,9 @@ const UserWebsiteSchema = {
/**
* 用户信息集合
*/
export const UserInfoSchema = {
class UserInfoSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** 用户的 UID - 非空 - 唯一 */
uid: { type: Number, unique: true, required: true },
/** 用户名 - 唯一 */
Expand All @@ -89,11 +93,13 @@ export const UserInfoSchema = {
userWebsite: { type: UserWebsiteSchema },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'user-info',
collectionName = 'user-info'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export const UserInfoSchema = new UserInfoSchemaFactory()

/**
* 用户关联账户的隐私设置
Expand All @@ -108,9 +114,9 @@ const UserLinkAccountsPrivacySettingSchema = {
/**
* 用户个性设定集合
*/
export const UserSettingsSchema = {
class UserSettingsSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** 用户的 UID - 非空 - 唯一 */
uid: { type: Number, unique: true, required: true },
/** 是否启用 Cookie - 布尔 */
Expand Down Expand Up @@ -155,7 +161,10 @@ export const UserSettingsSchema = {
userLinkAccountsPrivacySetting: { type: [UserLinkAccountsPrivacySettingSchema] },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 // WARN 不要使用单词的复数形式,Mongoose 会自动添加! */
collectionName: 'user-setting',
collectionName = 'user-setting'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}
export const UserSettingsSchema = new UserSettingsSchemaFactory()
36 changes: 24 additions & 12 deletions src/dbPool/schema/VideoCommentSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Schema } from 'mongoose'

/**
* 在父评论或子评论中存储的子评论 ID
*/
Expand Down Expand Up @@ -48,9 +50,9 @@ export const VideoSubCommentSchema = {
/**
* 视频评论数据
*/
export const VideoCommentSchema = {
class VideoCommentSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** 评论的路由 - 非空 */ /** 如:1.2.3(视频的第一个评论的第二个回复的第三个回复) */
commentRoute: { type: String, required: true },
/** KVID 视频 ID - 非空 */
Expand All @@ -73,17 +75,20 @@ export const VideoCommentSchema = {
subCommentsCount: { type: Number, required: true },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'video-comment',
collectionName = 'video-comment'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}
export const VideoCommentSchema = new VideoCommentSchemaFactory()

/**
* 视频评论点赞
*/
export const VideoCommentUpvoteSchema = {
class VideoCommentUpvoteSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** KVID 视频 ID - 非空 */
videoId: { type: Number, required: true },
/** 评论的ID - 非空 */
Expand All @@ -96,18 +101,21 @@ export const VideoCommentUpvoteSchema = {
deleteFlag: { type: Boolean, required: true },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'video-comment-upvote',
collectionName = 'video-comment-upvote'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}
export const VideoCommentUpvoteSchema = new VideoCommentUpvoteSchemaFactory()


/**
* 视频评论点踩
*/
export const VideoCommentDownvoteSchema = {
class VideoCommentDownvoteSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** KVID 视频 ID - 非空 */
videoId: { type: Number, required: true },
/** 评论的ID - 非空 */
Expand All @@ -120,7 +128,11 @@ export const VideoCommentDownvoteSchema = {
deleteFlag: { type: Boolean, required: true },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'video-comment-downvote',
collectionName = 'video-comment-downvote'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export const VideoCommentDownvoteSchema = new VideoCommentDownvoteSchemaFactory()
14 changes: 9 additions & 5 deletions src/dbPool/schema/VideoSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Schema } from 'mongoose'

/**
* 分P 视频的数据
*/
Expand Down Expand Up @@ -29,9 +31,9 @@ const VideoTagSchema = {
/**
* 视频数据
*/
const VideoSchema = {
class VideoSchemaFactory {
/** MongoDB Schema */
schema: {
schema = {
/** KVID 视频 ID - 非空 - 唯一 */
videoId: { type: Number, unique: true, required: true },
/** 视频标题 - 非空 */
Expand Down Expand Up @@ -60,9 +62,11 @@ const VideoSchema = {
videoTags: { type: [VideoTagSchema], required: true },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
},
}
/** MongoDB 集合名 */
collectionName: 'video',
collectionName = 'video'
/** Mongoose Schema 实例 */
schemaInstance = new Schema(this.schema)
}

export default VideoSchema
export const VideoSchema = new VideoSchemaFactory()
22 changes: 10 additions & 12 deletions src/service/DanmakuService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InferSchemaType, Schema } from 'mongoose'
import { InferSchemaType } from 'mongoose'
import { EmitDanmakuRequestDto, EmitDanmakuResponseDto, GetDanmakuByKvidRequestDto, GetDanmakuByKvidResponseDto } from '../controller/DanmakuControllerDto.js'
import { insertData2MongoDB, selectDataFromMongoDB } from '../dbPool/DbClusterPool.js'
import { QueryType, SelectType } from '../dbPool/DbClusterPoolTypes.js'
import DanmakuSchema from '../dbPool/schema/DanmakuSchema.js'
import { DanmakuSchema } from '../dbPool/schema/DanmakuSchema.js'
import { checkUserTokenService } from './UserService.js'

/**
Expand All @@ -16,17 +16,16 @@ export const emitDanmakuService = async (emitDanmakuRequest: EmitDanmakuRequestD
try {
if (checkEmitDanmakuRequest(emitDanmakuRequest)) {
if ((await checkUserTokenService(uid, token)).success) {
const { collectionName, schema: danmakuSchema } = DanmakuSchema
const schema = new Schema(danmakuSchema)
type Danmaku = InferSchemaType<typeof schema>
const { collectionName, schemaInstance } = DanmakuSchema
type Danmaku = InferSchemaType<typeof schemaInstance>
const nowDate = new Date().getTime()
const danmaku: Danmaku = {
uid,
...emitDanmakuRequest,
editDateTime: nowDate,
}
try {
const insertData2MongoDBResult = await insertData2MongoDB(danmaku, schema, collectionName)
const insertData2MongoDBResult = await insertData2MongoDB(danmaku, schemaInstance, collectionName)
if (insertData2MongoDBResult && insertData2MongoDBResult.success) {
return { success: true, message: '弹幕发送成功!', danmaku: emitDanmakuRequest }
}
Expand Down Expand Up @@ -56,13 +55,12 @@ export const emitDanmakuService = async (emitDanmakuRequest: EmitDanmakuRequestD
export const getDanmakuListByKvidService = async (getDanmakuByKvidRequest: GetDanmakuByKvidRequestDto): Promise<GetDanmakuByKvidResponseDto> => {
try {
if (checkGetDanmakuByKvidRequest(getDanmakuByKvidRequest)) {
const { collectionName, schema: danmakuSchema } = DanmakuSchema
const schema = new Schema(danmakuSchema)
type Danmaku = InferSchemaType<typeof schema>
const { collectionName, schemaInstance } = DanmakuSchema
type Danmaku = InferSchemaType<typeof schemaInstance>
const where: QueryType<Danmaku> = {
videoId: getDanmakuByKvidRequest.videoId,
}

const select: SelectType<Danmaku> = {
videoId: 1,
time: 1,
Expand All @@ -73,9 +71,9 @@ export const getDanmakuListByKvidService = async (getDanmakuByKvidRequest: GetDa
enableRainbow: 1,
editDateTime: 1,
}

try {
const result = await selectDataFromMongoDB(where, select, schema, collectionName)
const result = await selectDataFromMongoDB(where, select, schemaInstance, collectionName)
const danmakuList = result.result
if (result.success) {
if (danmakuList && danmakuList.length > 0) {
Expand Down
10 changes: 4 additions & 6 deletions src/service/SequenceValueService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Schema } from 'mongoose'
import { getNextSequenceValuePool } from '../dbPool/DbClusterPool.js'
import { SequenceValueSchema } from '../dbPool/schema/SequenceSchema.js'
import { getNextSequenceValuePool } from '../dbPool/DbClusterPool.js';
import { SequenceValueSchema } from '../dbPool/schema/SequenceSchema.js';

// NOTE 自增序列默认会跳过的值
const __DEFAULT_SEQUENCE_EJECT__: number[] = [9, 42, 233, 404, 2233, 10388, 10492, 114514]
Expand Down Expand Up @@ -29,10 +28,9 @@ type SequenceNumberResultType = {
export const getNextSequenceValueService = async (sequenceId: string, sequenceDefaultNumber: number = 0, sequenceStep: number = 1): Promise<SequenceNumberResultType> => {
try {
if (sequenceId) {
const { collectionName, schema: sequenceValueSchema } = SequenceValueSchema
const schema = new Schema(sequenceValueSchema)
const { collectionName, schemaInstance } = SequenceValueSchema
try {
const getNextSequenceValue = await getNextSequenceValuePool(sequenceId, schema, collectionName, sequenceDefaultNumber, sequenceStep)
const getNextSequenceValue = await getNextSequenceValuePool(sequenceId, schemaInstance, collectionName, sequenceDefaultNumber, sequenceStep)
const sequenceValue = getNextSequenceValue?.result
if (getNextSequenceValue.success && sequenceValue !== null && sequenceValue !== undefined) {
return { success: true, sequenceId, sequenceValue, message: '获取自增 ID 成功' }
Expand Down
Loading

0 comments on commit 1816fac

Please sign in to comment.