diff --git a/src/app.controller.ts b/src/app.controller.ts index b2bd22d9..7a435c53 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -18,7 +18,7 @@ import { IpLocation, IpRecord } from './common/decorator/ip.decorator' import { AllowAllCorsInterceptor } from './common/interceptors/allow-all-cors.interceptor' import { RedisKeys } from './constants/cache.constant' import { OptionModel } from './modules/configs/configs.model' -import { CacheService } from './processors/cache/cache.service' +import { CacheService } from './processors/redis/cache.service' import { getRedisKey } from './utils/redis.util' @Controller() diff --git a/src/app.module.ts b/src/app.module.ts index 1116db1e..312ee678 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -43,17 +43,17 @@ import { SnippetModule } from './modules/snippet/snippet.module' import { ToolModule } from './modules/tool/tool.module' import { TopicModule } from './modules/topic/topic.module' import { UserModule } from './modules/user/user.module' -import { CacheModule } from './processors/cache/cache.module' import { DatabaseModule } from './processors/database/database.module' import { GatewayModule } from './processors/gateway/gateway.module' import { HelperModule } from './processors/helper/helper.module' import { LoggerModule } from './processors/logger/logger.module' +import { RedisModule } from './processors/redis/redis.module' @Module({ imports: [ LoggerModule, DatabaseModule, - CacheModule, + RedisModule, AggregateModule, AnalyzeModule, diff --git a/src/common/interceptors/analyze.interceptor.ts b/src/common/interceptors/analyze.interceptor.ts index e25f3878..703989b7 100644 --- a/src/common/interceptors/analyze.interceptor.ts +++ b/src/common/interceptors/analyze.interceptor.ts @@ -20,7 +20,7 @@ import { ReturnModelType } from '@typegoose/typegoose' import { RedisKeys } from '~/constants/cache.constant' import { AnalyzeModel } from '~/modules/analyze/analyze.model' import { OptionModel } from '~/modules/configs/configs.model' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer' import { InjectModel } from '~/transformers/model.transformer' import { getIp } from '~/utils/ip.util' diff --git a/src/common/interceptors/cache.interceptor.ts b/src/common/interceptors/cache.interceptor.ts index dac24bce..cba7e013 100644 --- a/src/common/interceptors/cache.interceptor.ts +++ b/src/common/interceptors/cache.interceptor.ts @@ -23,7 +23,7 @@ import { Reflector } from '@nestjs/core' import { REDIS } from '~/app.config' import * as META from '~/constants/meta.constant' import * as SYSTEM from '~/constants/system.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer' /** diff --git a/src/common/interceptors/idempotence.interceptor.ts b/src/common/interceptors/idempotence.interceptor.ts index fd97711c..0cbbd525 100644 --- a/src/common/interceptors/idempotence.interceptor.ts +++ b/src/common/interceptors/idempotence.interceptor.ts @@ -17,7 +17,7 @@ import { HTTP_IDEMPOTENCE_OPTIONS, } from '~/constants/meta.constant' import { REFLECTOR } from '~/constants/system.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getIp, getRedisKey, hashString } from '~/utils' const IdempotenceHeaderKey = 'x-idempotence' diff --git a/src/modules/aggregate/aggregate.service.ts b/src/modules/aggregate/aggregate.service.ts index 0ce36d26..68b3e292 100644 --- a/src/modules/aggregate/aggregate.service.ts +++ b/src/modules/aggregate/aggregate.service.ts @@ -10,9 +10,9 @@ import { AnyParamConstructor } from '@typegoose/typegoose/lib/types' import { CacheKeys, RedisKeys } from '~/constants/cache.constant' import { EventBusEvents } from '~/constants/event-bus.constant' -import { CacheService } from '~/processors/cache/cache.service' import { WebEventsGateway } from '~/processors/gateway/web/events.gateway' import { UrlBuilderService } from '~/processors/helper/helper.url-builder.service' +import { CacheService } from '~/processors/redis/cache.service' import { addYearCondition } from '~/transformers/db-query.transformer' import { getRedisKey } from '~/utils/redis.util' import { getShortDate } from '~/utils/time.util' diff --git a/src/modules/analyze/analyze.controller.ts b/src/modules/analyze/analyze.controller.ts index d85ba3bb..8bfa2dd8 100644 --- a/src/modules/analyze/analyze.controller.ts +++ b/src/modules/analyze/analyze.controller.ts @@ -6,7 +6,7 @@ import { Auth } from '~/common/decorator/auth.decorator' import { Paginator } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { PagerDto } from '~/shared/dto/pager.dto' import { getRedisKey } from '~/utils/redis.util' import { getTodayEarly, getWeekStart } from '~/utils/time.util' diff --git a/src/modules/analyze/analyze.service.ts b/src/modules/analyze/analyze.service.ts index 46d26a35..cea11e1d 100644 --- a/src/modules/analyze/analyze.service.ts +++ b/src/modules/analyze/analyze.service.ts @@ -6,7 +6,7 @@ import { Injectable } from '@nestjs/common' import { ReturnModelType } from '@typegoose/typegoose' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { InjectModel } from '~/transformers/model.transformer' import { getRedisKey } from '~/utils/redis.util' diff --git a/src/modules/backup/backup.service.ts b/src/modules/backup/backup.service.ts index 4bfffadc..2a65b966 100644 --- a/src/modules/backup/backup.service.ts +++ b/src/modules/backup/backup.service.ts @@ -15,8 +15,8 @@ import { import { MONGO_DB } from '~/app.config' import { BusinessEvents, EventScope } from '~/constants/business-event.constant' import { BACKUP_DIR, DATA_DIR } from '~/constants/path.constant' -import { CacheService } from '~/processors/cache/cache.service' import { EventManagerService } from '~/processors/helper/helper.event.service' +import { CacheService } from '~/processors/redis/cache.service' import { getMediumDateTime } from '~/utils' import { getFolderSize, installPKG } from '~/utils/system.util' diff --git a/src/modules/configs/configs.service.ts b/src/modules/configs/configs.service.ts index d1284b37..b1c724ee 100644 --- a/src/modules/configs/configs.service.ts +++ b/src/modules/configs/configs.service.ts @@ -17,8 +17,9 @@ import { BeAnObject } from '@typegoose/typegoose/lib/types' import { EventScope } from '~/constants/business-event.constant' import { RedisKeys } from '~/constants/cache.constant' import { EventBusEvents } from '~/constants/event-bus.constant' -import { CacheService } from '~/processors/cache/cache.service' import { EventManagerService } from '~/processors/helper/helper.event.service' +import { CacheService } from '~/processors/redis/cache.service' +import { SubPubBridgeService } from '~/processors/redis/subpub.service' import { InjectModel } from '~/transformers/model.transformer' import { sleep } from '~/utils' import { getRedisKey } from '~/utils/redis.util' @@ -53,6 +54,7 @@ export class ConfigsService { private readonly optionModel: ReturnModelType, private readonly userService: UserService, private readonly redis: CacheService, + private readonly subpub: SubPubBridgeService, private readonly eventManager: EventManagerService, ) { @@ -200,7 +202,7 @@ export class ConfigsService { scope: EventScope.TO_SYSTEM, }) } else { - this.redis.publish(EventBusEvents.EmailInit, '') + this.subpub.publish(EventBusEvents.EmailInit, '') } } diff --git a/src/modules/pageproxy/pageproxy.controller.ts b/src/modules/pageproxy/pageproxy.controller.ts index 11c3f8ca..4517f7c7 100644 --- a/src/modules/pageproxy/pageproxy.controller.ts +++ b/src/modules/pageproxy/pageproxy.controller.ts @@ -20,7 +20,7 @@ import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { RedisKeys } from '~/constants/cache.constant' import { LOCAL_ADMIN_ASSET_PATH } from '~/constants/path.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getRedisKey } from '~/utils/redis.util' import { dashboard } from '../../../package.json' diff --git a/src/modules/pty/pty.gateway.ts b/src/modules/pty/pty.gateway.ts index 6ac04d72..99516507 100644 --- a/src/modules/pty/pty.gateway.ts +++ b/src/modules/pty/pty.gateway.ts @@ -17,9 +17,9 @@ import { RedisKeys } from '~/constants/cache.constant' import { DATA_DIR } from '~/constants/path.constant' import { AuthService } from '~/modules/auth/auth.service' import { ConfigsService } from '~/modules/configs/configs.service' -import { CacheService } from '~/processors/cache/cache.service' import { createAuthGateway } from '~/processors/gateway/shared/auth.gateway' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' import { getIp, getRedisKey } from '~/utils' const AuthGateway = createAuthGateway({ namespace: 'pty', authway: 'jwt' }) diff --git a/src/modules/pty/pty.service.ts b/src/modules/pty/pty.service.ts index e4e60237..abfa98bb 100644 --- a/src/modules/pty/pty.service.ts +++ b/src/modules/pty/pty.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getRedisKey } from '~/utils' @Injectable() diff --git a/src/modules/serverless/serverless.service.ts b/src/modules/serverless/serverless.service.ts index a1bccab4..64ddf689 100644 --- a/src/modules/serverless/serverless.service.ts +++ b/src/modules/serverless/serverless.service.ts @@ -19,10 +19,10 @@ import { RedisKeys } from '~/constants/cache.constant' import { ErrorCodeEnum } from '~/constants/error-code.constant' import { DATA_DIR, NODE_REQUIRE_PATH } from '~/constants/path.constant' import { isTest } from '~/global/env.global' -import { CacheService } from '~/processors/cache/cache.service' import { DatabaseService } from '~/processors/database/database.service' import { AssetService } from '~/processors/helper/helper.asset.service' import { HttpService } from '~/processors/helper/helper.http.service' +import { CacheService } from '~/processors/redis/cache.service' import { InjectModel } from '~/transformers/model.transformer' import { UniqueArray } from '~/ts-helper/unique' import { deepCloneWithFunction, getRedisKey, safePathJoin } from '~/utils' diff --git a/src/modules/snippet/snippet.service.ts b/src/modules/snippet/snippet.service.ts index 78b97b04..520cab54 100644 --- a/src/modules/snippet/snippet.service.ts +++ b/src/modules/snippet/snippet.service.ts @@ -9,7 +9,7 @@ import { } from '@nestjs/common' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { InjectModel } from '~/transformers/model.transformer' import { getRedisKey } from '~/utils' diff --git a/src/modules/tool/tool.controller.ts b/src/modules/tool/tool.controller.ts index c1711204..6dbcf86a 100644 --- a/src/modules/tool/tool.controller.ts +++ b/src/modules/tool/tool.controller.ts @@ -4,7 +4,7 @@ import { Auth } from '~/common/decorator/auth.decorator' import { HttpCache } from '~/common/decorator/cache.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getRedisKey } from '~/utils/redis.util' import { ConfigsService } from '../configs/configs.service' diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index 9d0ace98..1749053a 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -13,7 +13,7 @@ import { ReturnModelType } from '@typegoose/typegoose' import { BusinessException } from '~/common/exceptions/business.exception' import { RedisKeys } from '~/constants/cache.constant' import { ErrorCodeEnum } from '~/constants/error-code.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { InjectModel } from '~/transformers/model.transformer' import { getAvatar, sleep } from '~/utils' import { getRedisKey } from '~/utils/redis.util' diff --git a/src/processors/gateway/admin/events.gateway.ts b/src/processors/gateway/admin/events.gateway.ts index 07574b52..90cc6f89 100644 --- a/src/processors/gateway/admin/events.gateway.ts +++ b/src/processors/gateway/admin/events.gateway.ts @@ -10,8 +10,9 @@ import { } from '@nestjs/websockets' import { LOG_DIR } from '~/constants/path.constant' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' +import { SubPubBridgeService } from '~/processors/redis/subpub.service' import { BusinessEvents } from '../../../constants/business-event.constant' import { AuthService } from '../../../modules/auth/auth.service' @@ -27,6 +28,7 @@ export class AdminEventsGateway protected readonly jwtService: JWTService, protected readonly authService: AuthService, private readonly cacheService: CacheService, + private readonly subpub: SubPubBridgeService, ) { super(jwtService, authService, cacheService) } @@ -54,11 +56,11 @@ export class AdminEventsGateway }) .on('data', handler) .on('end', () => { - this.cacheService.subscribe('log', handler) + this.subpub.subscribe('log', handler) stream.close() }) } else { - this.cacheService.subscribe('log', handler) + this.subpub.subscribe('log', handler) } } @@ -66,7 +68,7 @@ export class AdminEventsGateway unsubscribeStdOut(client: Socket) { const cb = this.subscribeSocketToHandlerMap.get(client) if (cb) { - this.cacheService.unsubscribe('log', cb as any) + this.subpub.unsubscribe('log', cb as any) } this.subscribeSocketToHandlerMap.delete(client) } diff --git a/src/processors/gateway/shared/auth.gateway.ts b/src/processors/gateway/shared/auth.gateway.ts index 1ee483a3..e79c2576 100644 --- a/src/processors/gateway/shared/auth.gateway.ts +++ b/src/processors/gateway/shared/auth.gateway.ts @@ -10,8 +10,8 @@ import { Emitter } from '@socket.io/redis-emitter' import { EventBusEvents } from '~/constants/event-bus.constant' import { AuthService } from '~/modules/auth/auth.service' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' import { BusinessEvents } from '../../../constants/business-event.constant' import { BoardcastBaseGateway } from '../base.gateway' diff --git a/src/processors/gateway/system/events.gateway.ts b/src/processors/gateway/system/events.gateway.ts index ac537b85..7c24bcfd 100644 --- a/src/processors/gateway/system/events.gateway.ts +++ b/src/processors/gateway/system/events.gateway.ts @@ -5,8 +5,8 @@ import { WebSocketGateway, } from '@nestjs/websockets' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' import { AuthService } from '../../../modules/auth/auth.service' import { createAuthGateway } from '../shared/auth.gateway' diff --git a/src/processors/gateway/web/events.gateway.ts b/src/processors/gateway/web/events.gateway.ts index 88947f1e..a67c7e33 100644 --- a/src/processors/gateway/web/events.gateway.ts +++ b/src/processors/gateway/web/events.gateway.ts @@ -17,7 +17,7 @@ import { Emitter } from '@socket.io/redis-emitter' import { BusinessEvents } from '~/constants/business-event.constant' import { RedisKeys } from '~/constants/cache.constant' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getRedisKey } from '~/utils/redis.util' import { getShortDate } from '~/utils/time.util' diff --git a/src/processors/helper/helper.counting.service.ts b/src/processors/helper/helper.counting.service.ts index f9c1e5c2..5c33fafc 100644 --- a/src/processors/helper/helper.counting.service.ts +++ b/src/processors/helper/helper.counting.service.ts @@ -7,8 +7,8 @@ import { PostModel } from '~/modules/post/post.model' import { InjectModel } from '~/transformers/model.transformer' import { getRedisKey } from '~/utils/redis.util' -import { CacheService } from '../cache/cache.service' import { DatabaseService } from '../database/database.service' +import { CacheService } from '../redis/cache.service' @Injectable() export class CountingService { diff --git a/src/processors/helper/helper.cron.service.ts b/src/processors/helper/helper.cron.service.ts index 111a704e..b93d6802 100644 --- a/src/processors/helper/helper.cron.service.ts +++ b/src/processors/helper/helper.cron.service.ts @@ -26,7 +26,7 @@ import { SearchService } from '~/modules/search/search.service' import { InjectModel } from '~/transformers/model.transformer' import { getRedisKey } from '~/utils/redis.util' -import { CacheService } from '../cache/cache.service' +import { CacheService } from '../redis/cache.service' import { HttpService } from './helper.http.service' @Injectable() diff --git a/src/processors/helper/helper.email.service.ts b/src/processors/helper/helper.email.service.ts index 085a3bfa..1b46c101 100644 --- a/src/processors/helper/helper.email.service.ts +++ b/src/processors/helper/helper.email.service.ts @@ -9,7 +9,7 @@ import { OnEvent } from '@nestjs/event-emitter' import { EventBusEvents } from '~/constants/event-bus.constant' import { ConfigsService } from '~/modules/configs/configs.service' -import { CacheService } from '../cache/cache.service' +import { SubPubBridgeService } from '../redis/subpub.service' import { AssetService } from './helper.asset.service' export enum ReplyMailType { @@ -29,13 +29,13 @@ export class EmailService { constructor( private readonly configsService: ConfigsService, private readonly assetService: AssetService, - private readonly cacheService: CacheService, + readonly subpub: SubPubBridgeService, ) { this.init() this.logger = new Logger(EmailService.name) if (cluster.isWorker) { - cacheService.subscribe(EventBusEvents.EmailInit, () => { + subpub.subscribe(EventBusEvents.EmailInit, () => { this.init() }) } diff --git a/src/processors/helper/helper.http.service.ts b/src/processors/helper/helper.http.service.ts index 7e6704e2..90360093 100644 --- a/src/processors/helper/helper.http.service.ts +++ b/src/processors/helper/helper.http.service.ts @@ -10,7 +10,7 @@ import { RedisKeys } from '~/constants/cache.constant' import { getRedisKey } from '~/utils' import { version } from '../../../package.json' -import { CacheService } from '../cache/cache.service' +import { CacheService } from '../redis/cache.service' declare module 'axios' { interface AxiosRequestConfig { diff --git a/src/processors/helper/helper.jwt.service.ts b/src/processors/helper/helper.jwt.service.ts index 47df2813..46ce86ba 100644 --- a/src/processors/helper/helper.jwt.service.ts +++ b/src/processors/helper/helper.jwt.service.ts @@ -8,7 +8,7 @@ import { CLUSTER, SECURITY } from '~/app.config' import { RedisKeys } from '~/constants/cache.constant' import { getRedisKey, md5 } from '~/utils' -import { CacheService } from '../cache/cache.service' +import { CacheService } from '../redis/cache.service' @Injectable() export class JWTService { diff --git a/src/processors/helper/helper.tq.service.ts b/src/processors/helper/helper.tq.service.ts index b10fffb9..9584ee76 100644 --- a/src/processors/helper/helper.tq.service.ts +++ b/src/processors/helper/helper.tq.service.ts @@ -5,7 +5,7 @@ import { Injectable } from '@nestjs/common' import { safeJSONParse } from '~/utils' -import { CacheService } from '../cache/cache.service' +import { CacheService } from '../redis/cache.service' type ITask = RedisMap< string, diff --git a/src/processors/cache/cache.service.ts b/src/processors/redis/cache.service.ts similarity index 71% rename from src/processors/cache/cache.service.ts rename to src/processors/redis/cache.service.ts index f1f32196..21d9c19b 100755 --- a/src/processors/cache/cache.service.ts +++ b/src/processors/redis/cache.service.ts @@ -5,8 +5,6 @@ import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common' import { getRedisKey } from '~/utils/redis.util' -import { RedisSubPub } from '../../utils/redis-subpub.util' - // Cache 客户端管理器 // 获取器 @@ -48,26 +46,6 @@ export class CacheService { return this.cache.set(key, value, options) } - private _redisSubPub: RedisSubPub - - get redisSubPub(): RedisSubPub { - return ( - this._redisSubPub ?? - (this._redisSubPub = require('../../utils/redis-subpub.util').redisSubPub) - ) - } - public async publish(event: string, data: any) { - return this.redisSubPub.publish(event, data) - } - - public async subscribe(event: string, callback: (data: any) => void) { - return this.redisSubPub.subscribe(event, callback) - } - - public async unsubscribe(event: string, callback: (data: any) => void) { - return this.redisSubPub.unsubscribe(event, callback) - } - public getClient() { return this.redisClient } diff --git a/src/processors/cache/cache.config.service.ts b/src/processors/redis/redis.config.service.ts similarity index 94% rename from src/processors/cache/cache.config.service.ts rename to src/processors/redis/redis.config.service.ts index 4b2d06fe..c17f3ee8 100755 --- a/src/processors/cache/cache.config.service.ts +++ b/src/processors/redis/redis.config.service.ts @@ -16,7 +16,7 @@ import { import { REDIS } from '~/app.config' @Injectable() -export class CacheConfigService implements CacheOptionsFactory { +export class RedisConfigService implements CacheOptionsFactory { // 缓存配置 public createCacheOptions(): CacheModuleOptions { const redisOptions: RedisOptions = { diff --git a/src/processors/cache/cache.module.ts b/src/processors/redis/redis.module.ts similarity index 51% rename from src/processors/cache/cache.module.ts rename to src/processors/redis/redis.module.ts index 1b47a7a2..867080d3 100644 --- a/src/processors/cache/cache.module.ts +++ b/src/processors/redis/redis.module.ts @@ -6,18 +6,19 @@ */ import { Global, Module, CacheModule as NestCacheModule } from '@nestjs/common' -import { CacheConfigService } from './cache.config.service' import { CacheService } from './cache.service' +import { RedisConfigService } from './redis.config.service' +import { SubPubBridgeService } from './subpub.service' @Global() @Module({ imports: [ NestCacheModule.registerAsync({ - useClass: CacheConfigService, - inject: [CacheConfigService], + useClass: RedisConfigService, + inject: [RedisConfigService], }), ], - providers: [CacheConfigService, CacheService], - exports: [CacheService], + providers: [RedisConfigService, CacheService, SubPubBridgeService], + exports: [CacheService, SubPubBridgeService], }) -export class CacheModule {} +export class RedisModule {} diff --git a/src/processors/redis/subpub.service.ts b/src/processors/redis/subpub.service.ts new file mode 100644 index 00000000..22014ce2 --- /dev/null +++ b/src/processors/redis/subpub.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@nestjs/common' + +import { redisSubPub } from '~/utils/redis-subpub.util' + +@Injectable() +export class SubPubBridgeService { + public async publish(event: string, data: any) { + return redisSubPub.publish(event, data) + } + + public async subscribe(event: string, callback: (data: any) => void) { + return redisSubPub.subscribe(event, callback) + } + + public async unsubscribe(event: string, callback: (data: any) => void) { + return redisSubPub.unsubscribe(event, callback) + } +} diff --git a/src/utils/redis-subpub.util.ts b/src/utils/redis-subpub.util.ts index 1aeb8f21..93ae5c31 100644 --- a/src/utils/redis-subpub.util.ts +++ b/src/utils/redis-subpub.util.ts @@ -32,7 +32,7 @@ class RedisSubPub { await this.pubClient.publish(channel, _data) } - ctc = new WeakMap() + private ctc = new WeakMap() public async subscribe(event: string, callback: (data: any) => void) { const myChannel = this.channelPrefix + event diff --git a/test/helper/redis-mock.helper.ts b/test/helper/redis-mock.helper.ts index 4f88dd6e..d80a5170 100644 --- a/test/helper/redis-mock.helper.ts +++ b/test/helper/redis-mock.helper.ts @@ -1,7 +1,7 @@ import IORedis, { Redis } from 'ioredis' import RedisMemoryServer from 'redis-memory-server' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' export class MockCacheService { private client: Redis diff --git a/test/src/app.controller.e2e-spec.ts b/test/src/app.controller.e2e-spec.ts index abc0d8ad..1b206bc4 100644 --- a/test/src/app.controller.e2e-spec.ts +++ b/test/src/app.controller.e2e-spec.ts @@ -4,7 +4,7 @@ import { Test } from '@nestjs/testing' import { AppController } from '~/app.controller' import { fastifyApp } from '~/common/adapters/fastify.adapter' import { OptionModel } from '~/modules/configs/configs.model' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' import { getModelToken } from '~/transformers/model.transformer' describe('AppController (e2e)', () => { diff --git a/test/src/modules/auth/auth.service.spec.ts b/test/src/modules/auth/auth.service.spec.ts index 76316cd3..6fd03e5a 100644 --- a/test/src/modules/auth/auth.service.spec.ts +++ b/test/src/modules/auth/auth.service.spec.ts @@ -2,8 +2,8 @@ import { Test } from '@nestjs/testing' import { AuthService } from '~/modules/auth/auth.service' import { UserModel } from '~/modules/user/user.model' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' import { getModelToken } from '~/transformers/model.transformer' describe('Test AuthService', () => { diff --git a/test/src/modules/configs/configs.service.spec.ts b/test/src/modules/configs/configs.service.spec.ts index 83984a1a..f21a3f36 100644 --- a/test/src/modules/configs/configs.service.spec.ts +++ b/test/src/modules/configs/configs.service.spec.ts @@ -9,8 +9,9 @@ import { RedisKeys } from '~/constants/cache.constant' import { OptionModel } from '~/modules/configs/configs.model' import { ConfigsService } from '~/modules/configs/configs.service' import { UserService } from '~/modules/user/user.service' -import { CacheService } from '~/processors/cache/cache.service' import { EventManagerService } from '~/processors/helper/helper.event.service' +import { CacheService } from '~/processors/redis/cache.service' +import { SubPubBridgeService } from '~/processors/redis/subpub.service' import { getModelToken } from '~/transformers/model.transformer' import { getRedisKey } from '~/utils/redis.util' @@ -45,6 +46,10 @@ describe('Test ConfigsService', () => { useValue: redisService$, }, { provide: EventManagerService, useValue: { emit: mockEmitFn } }, + { + provide: SubPubBridgeService, + useValue: {}, + }, ], }).compile() diff --git a/test/src/modules/serverless/serverless.service.spec.ts b/test/src/modules/serverless/serverless.service.spec.ts index 4a1a3b8a..a1cced1a 100644 --- a/test/src/modules/serverless/serverless.service.spec.ts +++ b/test/src/modules/serverless/serverless.service.spec.ts @@ -7,10 +7,10 @@ import { getModelForClass } from '@typegoose/typegoose' import { createMockedContextResponse } from '~/modules/serverless/mock-response.util' import { ServerlessService } from '~/modules/serverless/serverless.service' import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model' -import { CacheService } from '~/processors/cache/cache.service' import { DatabaseService } from '~/processors/database/database.service' import { AssetService } from '~/processors/helper/helper.asset.service' import { HttpService } from '~/processors/helper/helper.http.service' +import { CacheService } from '~/processors/redis/cache.service' import { getModelToken } from '~/transformers/model.transformer' describe('test serverless function service', () => { diff --git a/test/src/modules/snippet/snippet.controller.e2e-spec.ts b/test/src/modules/snippet/snippet.controller.e2e-spec.ts index ba4d6a1b..3816474a 100644 --- a/test/src/modules/snippet/snippet.controller.e2e-spec.ts +++ b/test/src/modules/snippet/snippet.controller.e2e-spec.ts @@ -10,8 +10,8 @@ import { ServerlessService } from '~/modules/serverless/serverless.service' import { SnippetController } from '~/modules/snippet/snippet.controller' import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model' import { SnippetService } from '~/modules/snippet/snippet.service' -import { CacheService } from '~/processors/cache/cache.service' import { DatabaseService } from '~/processors/database/database.service' +import { CacheService } from '~/processors/redis/cache.service' import { getModelToken } from '~/transformers/model.transformer' describe('test /snippets', () => { diff --git a/test/src/modules/snippet/snippet.service.spec.ts b/test/src/modules/snippet/snippet.service.spec.ts index 49c2db03..62a46d79 100644 --- a/test/src/modules/snippet/snippet.service.spec.ts +++ b/test/src/modules/snippet/snippet.service.spec.ts @@ -1,14 +1,16 @@ +import { dbHelper } from 'test/helper/db-mock.helper' +import { redisHelper } from 'test/helper/redis-mock.helper' + import { BadRequestException, NotFoundException } from '@nestjs/common' import { Test } from '@nestjs/testing' import { getModelForClass } from '@typegoose/typegoose' -import { dbHelper } from 'test/helper/db-mock.helper' -import { redisHelper } from 'test/helper/redis-mock.helper' -import { getModelToken } from '~/transformers/model.transformer' + import { ServerlessService } from '~/modules/serverless/serverless.service' import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model' import { SnippetService } from '~/modules/snippet/snippet.service' -import { CacheService } from '~/processors/cache/cache.service' import { DatabaseService } from '~/processors/database/database.service' +import { CacheService } from '~/processors/redis/cache.service' +import { getModelToken } from '~/transformers/model.transformer' describe('test Snippet Service', () => { let service: SnippetService diff --git a/test/src/modules/user/user.controller.spec.ts b/test/src/modules/user/user.controller.spec.ts index d6d3c0c2..d4479dea 100644 --- a/test/src/modules/user/user.controller.spec.ts +++ b/test/src/modules/user/user.controller.spec.ts @@ -1,8 +1,9 @@ import { Test } from '@nestjs/testing' + import { AuthService } from '~/modules/auth/auth.service' import { UserController } from '~/modules/user/user.controller' import { UserService } from '~/modules/user/user.service' -import { CacheService } from '~/processors/cache/cache.service' +import { CacheService } from '~/processors/redis/cache.service' describe('test UserModule controller', () => { let userController: UserController diff --git a/test/src/modules/user/user.service.spec.ts b/test/src/modules/user/user.service.spec.ts index 7ccfd277..401093c6 100644 --- a/test/src/modules/user/user.service.spec.ts +++ b/test/src/modules/user/user.service.spec.ts @@ -4,8 +4,8 @@ import { Test } from '@nestjs/testing' import { AuthService } from '~/modules/auth/auth.service' import { UserModel } from '~/modules/user/user.model' import { UserService } from '~/modules/user/user.service' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' import { getModelToken } from '~/transformers/model.transformer' describe('test UserModule service', () => { diff --git a/test/src/processors/helper/helper.jwt.service.spec.ts b/test/src/processors/helper/helper.jwt.service.spec.ts index 23bbde3c..825f7f9f 100644 --- a/test/src/processors/helper/helper.jwt.service.spec.ts +++ b/test/src/processors/helper/helper.jwt.service.spec.ts @@ -2,8 +2,8 @@ import { redisHelper } from 'test/helper/redis-mock.helper' import { Test } from '@nestjs/testing' -import { CacheService } from '~/processors/cache/cache.service' import { JWTService } from '~/processors/helper/helper.jwt.service' +import { CacheService } from '~/processors/redis/cache.service' describe('test jwt service', () => { let service: JWTService