Skip to content

Commit

Permalink
⚡️ perf: login -\> auth 변경, redis 풀스캔 O(N) 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo-Minseok committed Nov 27, 2024
1 parent 5649e67 commit 9eb5df6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions server/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,43 @@ export class AdminService {
const sessionId = uuid.v4();

if (cookie) {
this.redisService.redisClient.del(`login:${cookie}`);
this.redisService.redisClient.del(`auth:${cookie}`);
}

let cursor = '0';
let scanFlag = false;
do {
const [newCursor, keys] = await this.redisService.redisClient.scan(
cursor,
'MATCH',
'login:*',
'auth:*',
'COUNT',
100,
);

cursor = newCursor;

for (const key of keys) {
const sessionValue = await this.redisService.redisClient.get(key);
if (!keys.length) {
continue;
}

const values = await this.redisService.redisClient.mget(keys);

for (let i = 0; i < keys.length; i++) {
const sessionValue = values[i];
if (sessionValue === loginId) {
await this.redisService.redisClient.del(key);
await this.redisService.redisClient.del(keys[i]);
scanFlag = true;
break;
}
}
if (scanFlag) {
break;
}
} while (cursor !== '0');

this.redisService.redisClient.set(
`login:${sessionId}`,
`auth:${sessionId}`,
admin.loginId,
`EX`,
this.SESSION_TTL,
Expand Down
2 changes: 1 addition & 1 deletion server/src/common/guard/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CookieAuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const sid = request.cookies['sessionId'];
const loginId = await this.redisService.redisClient.get(`login:${sid}`);
const loginId = await this.redisService.redisClient.get(`auth:${sid}`);
if (!loginId) {
throw new UnauthorizedException('인증되지 않은 요청입니다.');
}
Expand Down
2 changes: 1 addition & 1 deletion server/test/statistic/today.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Today view count statistic E2E Test : GET /api/statistic/today', () =>
email: '[email protected]',
rssUrl: 'https://test.com/rss',
}),
redisService.redisClient.set('login:test1234', 'test'),
redisService.redisClient.set('auth:test1234', 'test'),
redisService.redisClient.zadd(
redisKeys.FEED_TREND_KEY,
'1',
Expand Down

0 comments on commit 9eb5df6

Please sign in to comment.