diff --git a/BE/src/common/redis/redis.domain-service.ts b/BE/src/common/redis/redis.domain-service.ts index 39fa2d5..27d212d 100644 --- a/BE/src/common/redis/redis.domain-service.ts +++ b/BE/src/common/redis/redis.domain-service.ts @@ -1,14 +1,19 @@ -import { Injectable, Inject } from '@nestjs/common'; +import { Injectable, Inject, OnModuleInit } from '@nestjs/common'; import Redis from 'ioredis'; @Injectable() -export class RedisDomainService { +export class RedisDomainService implements OnModuleInit { constructor( @Inject('REDIS_CLIENT') private readonly redis: Redis, @Inject('REDIS_PUBLISHER') private readonly publisher: Redis, @Inject('REDIS_SUBSCRIBER') private readonly subscriber: Redis, ) {} + async onModuleInit() { + const keys = await this.redis.keys('connections:*'); + if (keys.length > 0) await this.redis.del(keys); + } + async exists(key: string): Promise { return this.redis.exists(key); } @@ -82,11 +87,27 @@ export class RedisDomainService { return this.subscriber.unsubscribe(channel); } + async setConnection(key: string, value: number) { + return this.redis.set(`connections:${key}`, value); + } + + async getConnection(key: string): Promise { + return Number(await this.redis.get(`connections:${key}`)); + } + + async delConnection(key: string) { + return this.redis.del(`connections:${key}`); + } + + async existsConnection(key: string) { + return this.redis.exists(`connections:${key}`); + } + async increment(key: string) { - return this.redis.incr(key); + return this.redis.incr(`connections:${key}`); } async decrement(key: string) { - return this.redis.decr(key); + return this.redis.decr(`connections:${key}`); } } diff --git a/BE/src/stockSocket/stock-price-socket.service.ts b/BE/src/stockSocket/stock-price-socket.service.ts index 8897ea1..7da6b40 100644 --- a/BE/src/stockSocket/stock-price-socket.service.ts +++ b/BE/src/stockSocket/stock-price-socket.service.ts @@ -69,11 +69,11 @@ export class StockPriceSocketService extends BaseStockSocketDomainService { async subscribeByCode(trKey: string) { // 아무 서버도 한투와 구독 중이지 않을때 - if (!(await this.redisDomainService.exists(trKey))) { + if (!(await this.redisDomainService.existsConnection(trKey))) { this.baseSocketDomainService.registerCode(this.TR_ID, trKey); await this.redisDomainService.subscribe(`stock/${trKey}`); this.register.push(trKey); - await this.redisDomainService.set(trKey, 1); + await this.redisDomainService.setConnection(trKey, 1); this.connection[trKey] = 1; return; @@ -113,8 +113,8 @@ export class StockPriceSocketService extends BaseStockSocketDomainService { } // 레디스 내에서 모든 연결이 종료됐을 경우 - if ((await this.redisDomainService.get(trKey)) === 0) { - await this.redisDomainService.del(trKey); + if ((await this.redisDomainService.getConnection(trKey)) === 0) { + await this.redisDomainService.delConnection(trKey); } } } @@ -122,7 +122,7 @@ export class StockPriceSocketService extends BaseStockSocketDomainService { @Cron('*/5 * * * *') async checkConnection() { for (const trKey of this.register) { - if (!(await this.redisDomainService.exists(trKey))) { + if (!(await this.redisDomainService.existsConnection(trKey))) { this.baseSocketDomainService.unregisterCode(this.TR_ID, trKey); const idx = this.register.indexOf(trKey); if (idx) this.register.splice(idx, 1);