@@ -5,25 +5,39 @@ import { ConfigService } from '@nestjs/config';
55
66@Injectable ( )
77export class RedisService {
8- private defaultConnection : Redis ;
8+ private sessionConnection : Redis ;
99 private eventConnection : Redis ;
10+ private activeUserConnection : Redis ;
11+
12+ private readonly SESSION_TTL = 60 * 30 ;
13+ private readonly ACTIVE_USER_TTL = 60 * 5 ;
1014
1115 constructor (
1216 private readonly adminDBManager : AdminDBManager ,
1317 private readonly configService : ConfigService ,
1418 ) {
15- this . setDefaultConnection ( ) ;
19+ this . setSessionConnection ( ) ;
1620 this . setEventConnection ( ) ;
21+ this . setActiveUserConnection ( ) ;
1722 }
1823
19- private setDefaultConnection ( ) {
20- this . defaultConnection = new Redis ( {
24+ private setSessionConnection ( ) {
25+ this . sessionConnection = new Redis ( {
2126 host : this . configService . get < string > ( 'REDIS_HOST' ) ,
2227 port : this . configService . get < number > ( 'REDIS_PORT' ) ,
28+ db : this . configService . get < number > ( 'REDIS_DATABASE_SESSION' ) ,
2329 } ) ;
2430
25- this . defaultConnection . on ( 'ready' , ( ) => {
26- this . defaultConnection . config ( 'SET' , 'notify-keyspace-events' , 'Exg' ) ;
31+ this . sessionConnection . on ( 'ready' , ( ) => {
32+ this . sessionConnection . config ( 'SET' , 'notify-keyspace-events' , 'Exg' ) ;
33+ } ) ;
34+ }
35+
36+ private setActiveUserConnection ( ) {
37+ this . activeUserConnection = new Redis ( {
38+ host : this . configService . get < string > ( 'REDIS_HOST' ) ,
39+ port : this . configService . get < number > ( 'REDIS_PORT' ) ,
40+ db : this . configService . get < number > ( 'REDIS_DATABASE_ACTIVE_USER' ) ,
2741 } ) ;
2842 }
2943
@@ -42,28 +56,24 @@ export class RedisService {
4256 if ( ! key ) {
4357 return null ;
4458 }
45- return this . defaultConnection . hgetall ( key ) ;
59+ return this . sessionConnection . hgetall ( key ) ;
4660 }
4761
4862 public async existSession ( key : string ) {
49- return this . defaultConnection . exists ( key ) ;
63+ return this . sessionConnection . exists ( key ) ;
5064 }
5165
5266 public async setNewSession ( key : string ) {
5367 const session = await this . existSession ( key ) ;
5468 if ( ! session ) {
55- await this . defaultConnection . hset ( key , 'rowCount' , 0 ) ;
69+ await this . sessionConnection . hset ( key , 'rowCount' , 0 ) ;
5670 await this . adminDBManager . initUserDatabase ( key ) ;
5771 }
58- await this . setExpireTime ( key , 60 * 60 ) ;
72+ await this . sessionConnection . expire ( key , this . SESSION_TTL ) ;
5973 }
6074
6175 public async deleteSession ( key : string ) {
62- await this . defaultConnection . del ( key ) ;
63- }
64-
65- public async setExpireTime ( key : string , ttl : number ) {
66- await this . defaultConnection . expire ( key , ttl ) ;
76+ await this . sessionConnection . del ( key ) ;
6777 }
6878
6979 private subscribeToExpiredEvents ( ) {
@@ -75,10 +85,14 @@ export class RedisService {
7585 }
7686
7787 public async getRowCount ( key : string ) {
78- return this . defaultConnection . hget ( key , 'rowCount' ) ;
88+ return this . sessionConnection . hget ( key , 'rowCount' ) ;
7989 }
8090
8191 public async setRowCount ( key : string , rowCount : number ) {
82- await this . defaultConnection . hset ( key , 'rowCount' , rowCount ) ;
92+ await this . sessionConnection . hset ( key , 'rowCount' , rowCount ) ;
93+ }
94+
95+ public async setActiveUser ( key : string ) {
96+ this . activeUserConnection . expire ( key , this . ACTIVE_USER_TTL ) ;
8397 }
8498}
0 commit comments