From e64232c0afd0d227cd2ff1ab36cca04fe23f97d2 Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Thu, 7 Sep 2023 10:49:45 +0530 Subject: [PATCH] Fixed improper cache configuration. --- package.json | 4 ++-- src/app.controller.ts | 2 +- src/app.module.ts | 15 +++++++++++---- src/cache.module.ts | 15 --------------- src/socket/socket.gateway.ts | 9 +++++---- 5 files changed, 19 insertions(+), 26 deletions(-) delete mode 100644 src/cache.module.ts diff --git a/package.json b/package.json index 14d517b..8d500b7 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ "@nestjs/terminus": "^8.0.1", "@nestjs/websockets": "^8.0.6", "@types/cache-manager": "^3.4.2", - "cache-manager": "^4.1.0", - "cache-manager-redis-store": "^3.0.1", + "cache-manager": "^3.6.0", + "cache-manager-redis-store": "^2.0.0", "ioredis": "^5.3.1", "jsonwebtoken": "^9.0.0", "jwks-rsa": "^3.0.1", diff --git a/src/app.controller.ts b/src/app.controller.ts index d2f2cf3..a4f4a40 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -16,7 +16,7 @@ export class AppController { public appConfig; constructor( private readonly wsg: SocketGateway, - @Inject('CustomCacheToken') private cacheManager: Cache, + @Inject(CACHE_MANAGER) private cacheManager: Cache, ) { this.appConfig = config().app; } diff --git a/src/app.module.ts b/src/app.module.ts index 32ea723..073bb05 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,6 +1,6 @@ import * as winston from 'winston'; -import { CacheModule, Logger, Module } from '@nestjs/common'; +import { Logger, Module } from '@nestjs/common'; import { WinstonModule, utilities as nestWinstonModuleUtilities, @@ -14,7 +14,9 @@ import { HttpModule } from '@nestjs/axios'; import { SocketGateway } from './socket/socket.gateway'; import { TerminusModule } from '@nestjs/terminus'; import { config } from './config/config'; -import { CustomCacheModule } from './cache.module'; +import { CacheModule } from '@nestjs/common'; +import * as redisStore from 'cache-manager-redis-store'; + @Module({ imports: [ WinstonModule.forRootAsync({ @@ -58,8 +60,13 @@ import { CustomCacheModule } from './cache.module'; load: [config], }), TerminusModule, - CustomCacheModule, - CacheModule.register(), + CacheModule.register({ + store: redisStore, + host: process.env.TRANSPORT_SOCKET_CACHE_HOST, + port: process.env.TRANSPORT_SOCKET_CACHE_PORT, + max: 200000, + ttl: 86400, + }), ], controllers: [AppController, HealthController], providers: [AppService, SocketGateway, Logger], diff --git a/src/cache.module.ts b/src/cache.module.ts deleted file mode 100644 index c7e5abc..0000000 --- a/src/cache.module.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { CacheModule, CACHE_MANAGER, Module } from '@nestjs/common'; -import { ConfigModule, ConfigService } from '@nestjs/config'; -import { redisStore } from 'cache-manager-redis-store'; - -@Module({ - imports: [CacheModule.register()], - providers: [ - { - provide: 'CustomCacheToken', - useExisting: CACHE_MANAGER, - }, - ], - exports: ['CustomCacheToken'], -}) -export class CustomCacheModule {} diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index f94c5f4..c19ed1c 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -34,7 +34,7 @@ export class SocketGateway constructor( private readonly appService: AppService, - @Inject('CustomCacheToken') private cacheManager: Cache, + @Inject(CACHE_MANAGER) private cacheManager: Cache, ) {} afterInit(server: Server) { @@ -47,7 +47,8 @@ export class SocketGateway ); const sessionID = client.handshake.query.deviceId; const userID = client.handshake.query.deviceId as string; - await this.cacheManager.set(userID, client.id, 86400 * 1000); + await this.cacheManager.set(userID, client.id); + this.logger.log(`Storing userID in cache: ${userID}, id: ${client.id}`); client['sessionID'] = sessionID; client['userID'] = userID; client.join(userID); @@ -60,7 +61,7 @@ export class SocketGateway async handleDisconnect(client: Socket) { this.logger.log(`Client is disconnected = ${client.id}`); - await this.cacheManager.set(client['userID'], null, 86400 * 1000); + await this.cacheManager.del(client['userID']); } @UseGuards(WsGuard) @@ -81,7 +82,7 @@ export class SocketGateway @SubscribeMessage('endConnection') async handleEndConnection(client: Socket) { this.logger.log({ msg: 'The client has closed the bot' }); - await this.cacheManager.set(client['userID'], null, 86400 * 1000); + await this.cacheManager.del(client['userID']); client.disconnect(true); return {}; }