From ab796b7853b5a27afdc6d7944563481518d850fb Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 18 Jan 2022 17:05:12 +0800 Subject: [PATCH] refactor: clean aggregate cache to event --- src/constants/event.constant.ts | 2 ++ src/modules/aggregate/aggregate.service.ts | 15 ++++++++++++++- src/modules/note/note.service.ts | 9 +++++---- src/modules/post/post.service.ts | 10 ++++++---- src/processors/cache/cache.service.ts | 11 ----------- src/utils/cluster.util.ts | 9 +++++++++ test/helper/redis-mock.helper.ts | 11 ----------- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/constants/event.constant.ts b/src/constants/event.constant.ts index 9f3d8191..7266b09f 100644 --- a/src/constants/event.constant.ts +++ b/src/constants/event.constant.ts @@ -2,4 +2,6 @@ export enum EventBusEvents { EmailInit = 'email.init', PushSearch = 'search.push', TokenExpired = 'token.expired', + + CleanAggregateCache = 'cache.aggregate', } diff --git a/src/modules/aggregate/aggregate.service.ts b/src/modules/aggregate/aggregate.service.ts index 0d10ff79..df83d37c 100644 --- a/src/modules/aggregate/aggregate.service.ts +++ b/src/modules/aggregate/aggregate.service.ts @@ -1,11 +1,13 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common' +import { OnEvent } from '@nestjs/event-emitter' import { DocumentType, ReturnModelType } from '@typegoose/typegoose' import { AnyParamConstructor } from '@typegoose/typegoose/lib/types' import dayjs from 'dayjs' import { pick } from 'lodash' import { FilterQuery } from 'mongoose' import { URL } from 'url' -import { RedisKeys } from '~/constants/cache.constant' +import { CacheKeys, RedisKeys } from '~/constants/cache.constant' +import { EventBusEvents } from '~/constants/event.constant' import { CacheService } from '~/processors/cache/cache.service' import { WebEventsGateway } from '~/processors/gateway/web/events.gateway' import { addYearCondition } from '~/utils/query.util' @@ -370,4 +372,15 @@ export class AggregateService { todayOnlineTotal: todayOnlineTotal || 0, } } + + @OnEvent(EventBusEvents.CleanAggregateCache, { async: true }) + public clearAggregateCache() { + return Promise.all([ + this.cacheService.getClient().del(CacheKeys.RSS), + this.cacheService.getClient().del(CacheKeys.RSSXmlCatch), + this.cacheService.getClient().del(CacheKeys.AggregateCatch), + this.cacheService.getClient().del(CacheKeys.SiteMapCatch), + this.cacheService.getClient().del(CacheKeys.SiteMapXmlCatch), + ]) + } } diff --git a/src/modules/note/note.service.ts b/src/modules/note/note.service.ts index ee565366..138d3001 100644 --- a/src/modules/note/note.service.ts +++ b/src/modules/note/note.service.ts @@ -1,10 +1,11 @@ import { Injectable } from '@nestjs/common' +import { EventEmitter2 } from '@nestjs/event-emitter' import { DocumentType } from '@typegoose/typegoose' import { isDefined, isMongoId } from 'class-validator' import { FilterQuery } from 'mongoose' import { InjectModel } from 'nestjs-typegoose' import { CannotFindException } from '~/common/exceptions/cant-find.exception' -import { CacheService } from '~/processors/cache/cache.service' +import { EventBusEvents } from '~/constants/event.constant' import { EventTypes } from '~/processors/gateway/events.types' import { WebEventsGateway } from '~/processors/gateway/web/events.gateway' import { ImageService } from '~/processors/helper/helper.image.service' @@ -18,7 +19,7 @@ export class NoteService { private readonly noteModel: MongooseModel, private readonly imageService: ImageService, private readonly webGateway: WebEventsGateway, - private readonly cacheService: CacheService, + private readonly eventEmitter: EventEmitter2, ) { this.needCreateDefult() } @@ -82,8 +83,8 @@ export class NoteService { public async create(document: NoteModel) { const doc = await this.noteModel.create(document) process.nextTick(async () => { + this.eventEmitter.emit(EventBusEvents.CleanAggregateCache) await Promise.all([ - this.cacheService.clearAggregateCache(), this.imageService.recordImageDimensions(this.noteModel, doc._id), doc.hide || doc.password ? null @@ -108,8 +109,8 @@ export class NoteService { { new: true }, ) process.nextTick(async () => { + this.eventEmitter.emit(EventBusEvents.CleanAggregateCache) await Promise.all([ - this.cacheService.clearAggregateCache(), this.imageService.recordImageDimensions(this.noteModel, id), this.model.findById(id).then((doc) => { delete doc.password diff --git a/src/modules/post/post.service.ts b/src/modules/post/post.service.ts index 67a24f56..328597a0 100644 --- a/src/modules/post/post.service.ts +++ b/src/modules/post/post.service.ts @@ -4,11 +4,12 @@ import { Inject, Injectable, } from '@nestjs/common' +import { EventEmitter2 } from '@nestjs/event-emitter' import { isDefined } from 'class-validator' import { omit } from 'lodash' import { FilterQuery, PaginateOptions } from 'mongoose' import { InjectModel } from 'nestjs-typegoose' -import { CacheService } from '~/processors/cache/cache.service' +import { EventBusEvents } from '~/constants/event.constant' import { EventTypes } from '~/processors/gateway/events.types' import { WebEventsGateway } from '~/processors/gateway/web/events.gateway' import { ImageService } from '~/processors/helper/helper.image.service' @@ -28,7 +29,7 @@ export class PostService { private categoryService: CategoryService, private readonly webgateway: WebEventsGateway, private readonly imageService: ImageService, - private readonly cacheService: CacheService, + private readonly eventEmitter: EventEmitter2, ) {} get model() { @@ -61,8 +62,8 @@ export class PostService { }) process.nextTick(async () => { + this.eventEmitter.emit(EventBusEvents.CleanAggregateCache) await Promise.all([ - this.cacheService.clearAggregateCache(), this.webgateway.broadcast(EventTypes.POST_CREATE, { ...res.toJSON(), category, @@ -104,6 +105,8 @@ export class PostService { { new: true }, ) process.nextTick(async () => { + this.eventEmitter.emit(EventBusEvents.CleanAggregateCache) + // 更新图片信息缓存 await Promise.all([ this.imageService.recordImageDimensions(this.postModel, id), @@ -111,7 +114,6 @@ export class PostService { EventTypes.POST_UPDATE, await this.postModel.findById(id), ), - this.cacheService.clearAggregateCache(), ]) }) diff --git a/src/processors/cache/cache.service.ts b/src/processors/cache/cache.service.ts index 5f74b4ca..9cd05c67 100755 --- a/src/processors/cache/cache.service.ts +++ b/src/processors/cache/cache.service.ts @@ -1,7 +1,6 @@ import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common' import { Cache } from 'cache-manager' import { Redis } from 'ioredis' -import { CacheKeys } from '~/constants/cache.constant' // Cache 客户端管理器 @@ -47,14 +46,4 @@ export class CacheService { public getClient() { return this.redisClient } - - public clearAggregateCache() { - return Promise.all([ - this.redisClient.del(CacheKeys.RSS), - this.redisClient.del(CacheKeys.RSSXmlCatch), - this.redisClient.del(CacheKeys.AggregateCatch), - this.redisClient.del(CacheKeys.SiteMapCatch), - this.redisClient.del(CacheKeys.SiteMapXmlCatch), - ]) - } } diff --git a/src/utils/cluster.util.ts b/src/utils/cluster.util.ts index 05328b4e..1278e326 100644 --- a/src/utils/cluster.util.ts +++ b/src/utils/cluster.util.ts @@ -1,5 +1,14 @@ +/** + * 服务集群工具 + */ + import cluster from 'cluster' import { EventBusEvents } from '~/constants/event.constant' +/** + * Worker 事件 Emit + * @param event + * @param data + */ export const workerEmit = (event: EventBusEvents, data?: any) => { if (cluster.isWorker) { process.send({ event, data, workerId: cluster.worker.id }) diff --git a/test/helper/redis-mock.helper.ts b/test/helper/redis-mock.helper.ts index ecc5aca7..5e2c09ce 100644 --- a/test/helper/redis-mock.helper.ts +++ b/test/helper/redis-mock.helper.ts @@ -1,6 +1,5 @@ import IORedis from 'ioredis' import RedisMemoryServer from 'redis-memory-server' -import { CacheKeys } from '~/constants/cache.constant' export class MockCacheService { private client: IORedis.Redis @@ -23,16 +22,6 @@ export class MockCacheService { public getClient() { return this.redisClient } - - public clearAggregateCache() { - return Promise.all([ - this.redisClient.del(CacheKeys.RSS), - this.redisClient.del(CacheKeys.RSSXmlCatch), - this.redisClient.del(CacheKeys.AggregateCatch), - this.redisClient.del(CacheKeys.SiteMapCatch), - this.redisClient.del(CacheKeys.SiteMapXmlCatch), - ]) - } } export const createMockRedis = async () => {