Skip to content

Commit

Permalink
πŸ› fix: redis Service μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo-Minseok committed Nov 20, 2024
1 parent bb9e800 commit 3b2a542
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion server/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AdminService {

const sessionId = uuid.v4();

await this.redisService.set(
await this.redisService.redisClient.set(
sessionId,
admin.loginId,
`EX`,
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 @@ -15,7 +15,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.get(sid);
const loginId = await this.redisService.redisClient.get(sid);
if (!loginId) {
throw new UnauthorizedException('μΈμ¦λ˜μ§€ μ•Šμ€ μš”μ²­μž…λ‹ˆλ‹€.');
}
Expand Down
17 changes: 7 additions & 10 deletions server/src/common/redis/redis.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ import Redis_Mock from 'ioredis-mock';
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const envType = process.env.NODE_ENV;
let redis: Redis;
if (envType === 'test') {
redis = new Redis_Mock();
} else {
redis = new Redis({
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
username: configService.get<string>('REDIS_USERNAME'),
password: configService.get<string>('REDIS_PASSWORD'),
});
return new Redis_Mock();
}
return redis;
return new Redis({
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
username: configService.get<string>('REDIS_USERNAME'),
password: configService.get<string>('REDIS_PASSWORD'),
});
},
},
RedisService,
Expand Down
6 changes: 2 additions & 4 deletions server/src/common/redis/redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisService extends Redis {
constructor(@Inject('REDIS_CLIENT') private readonly redisClient: Redis) {
super(redisClient.options);
}
export class RedisService {
constructor(@Inject('REDIS_CLIENT') public readonly redisClient: Redis) {}
}
6 changes: 5 additions & 1 deletion server/src/feed/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export class FeedService {
}

async getTrendList() {
const trendFeedIdList = await this.redisService.zrange('feed:trend', 0, 3);
const trendFeedIdList = await this.redisService.redisClient.zrange(
'feed:trend',
0,
3,
);
const trendFeeds = await Promise.all(
trendFeedIdList.map(async (feedId) => {
const feed = await this.feedRepository.findTrendFeed(parseInt(feedId));
Expand Down
68 changes: 34 additions & 34 deletions server/test/feed/trend.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { RedisService } from '../../src/common/redis/redis.service';
import { DataSource } from 'typeorm';
import { Feed } from '../../src/feed/feed.entity';
import { Blog } from '../../src/blog/blog.entity';
import { INestApplication } from '@nestjs/common';

describe('Trend API', () => {
let app;
let app: INestApplication;
let moduleFixture: TestingModule;
let redisService;
let redisService: RedisService;
beforeAll(async () => {
moduleFixture = await Test.createTestingModule({
imports: [AppModule],
Expand All @@ -31,7 +32,7 @@ describe('Trend API', () => {
const orm = app.get(DataSource);
const feedRepository = orm.getRepository(Feed);
const blogRepository = orm.getRepository(Blog);
await blogRepository.save([
const blogs = await blogRepository.save([
{
id: 1,
name: 'test',
Expand All @@ -57,32 +58,35 @@ describe('Trend API', () => {
await feedRepository.save([
{
id: 1,
createdAt: '2022-09-05T09:00:00.000Z',
title:
'μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ ꡬ쑰와 μ‹€ν–‰ 방식 (Ignition, TurboFan, EventLoop)',
viewCount: 0,
path: 'https://asn6878.tistory.com/9',
createdAt: '2022-09-05T09:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 1,
blog: blogs[0],
},
{
id: 2,
createdAt: '2024-08-14T14:07:49.000Z',
title:
'[넀이버 컀λ„₯νŠΈμž¬λ‹¨ λΆ€μŠ€νŠΈμΊ ν”„ 웹・λͺ¨λ°”일 9κΈ°] λ‚  것 κ·ΈλŒ€λ‘œ μž‘μ„±ν•˜λŠ” μ±Œλ¦°μ§€ 수료 ν›„κΈ° - Web',
path: 'https://velog.io/@seok3765/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%BB%A4%EB%84%A5%ED%8A%B8%EC%9E%AC%EB%8B%A8-%EB%B6%80%EC%8A%A4%ED%8A%B8%EC%BA%A0%ED%94%84-%EC%9B%B9%E3%83%BB%EB%AA%A8%EB%B0%94%EC%9D%BC-9%EA%B8%B0-%EB%82%A0-%EA%B2%83-%EA%B7%B8%EB%8C%80%EB%A1%9C-%EC%9E%91%EC%84%B1%ED%95%98%EB%8A%94-%EC%B1%8C%EB%A6%B0%EC%A7%80-%EC%88%98%EB%A3%8C-%ED%9B%84%EA%B8%B0-Web',
createdAt: '2024-08-14T14:07:49.000Z',
thumbnail:
'https://velog.velcdn.com/images/seok3765/post/2f863481-b594-46f8-9a28-7799afb58aa4/image.jpg',
blog: 2,
viewCount: 0,
blog: blogs[1],
},
{
id: 3,
createdAt: '2022-09-05T09:00:00.000Z',
viewCount: 0,
title: '제λͺ©',
path: 'https://asn6878.tistory.com/10',
createdAt: '2022-09-05T09:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 3,
blog: blogs[2],
},
{
id: 4,
Expand All @@ -91,7 +95,8 @@ describe('Trend API', () => {
createdAt: '2022-09-05T10:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 3,
viewCount: 0,
blog: blogs[2],
},
{
id: 5,
Expand All @@ -100,14 +105,15 @@ describe('Trend API', () => {
createdAt: '2022-09-05T10:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 2,
viewCount: 0,
blog: blogs[2],
},
]);
redisService = app.get(RedisService);
});

beforeEach(async () => {
redisService = app.get(RedisService);
await redisService.flushall();
await redisService.redisClient.flushall();
});

afterAll(async () => {
Expand All @@ -127,7 +133,7 @@ describe('Trend API', () => {
});

it('ν”Όλ“œ 1개만 μ‘°νšŒλμ„ λ•Œ', async () => {
await redisService.zadd('feed:trend', 10, '1');
await redisService.redisClient.zadd('feed:trend', 10, '1');
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
);
Expand All @@ -152,8 +158,8 @@ describe('Trend API', () => {

it('ν”Όλ“œ 2개만 μ‘°νšŒλμ„ λ•Œ', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -189,9 +195,9 @@ describe('Trend API', () => {
});
it('ν”Όλ“œ 3개만 μ‘°νšŒλμ„ λ•Œ', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -237,10 +243,10 @@ describe('Trend API', () => {
});
it('ν”Όλ“œ 4개만 μ‘°νšŒλμ„ λ•Œ', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.zadd('feed:trend', 40, '4'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 40, '4'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -296,11 +302,11 @@ describe('Trend API', () => {
});
it('ν”Όλ“œ 5개 이상 μ‘°νšŒλμ„ λ•Œ', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.zadd('feed:trend', 40, '4'),
redisService.zadd('feed:trend', 50, '5'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 40, '4'),
redisService.redisClient.zadd('feed:trend', 50, '5'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -355,10 +361,4 @@ describe('Trend API', () => {
});
});
});

describe('κ²Œμ‹œκΈ€μ΄ μ˜¬λ°”λ₯΄κ²Œ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ λ•Œ', () => {
it('쑰회 ν…Œμ΄λΈ”μ—λ§Œ 데이터가 있고 RDBMS에 데이터가 없을 λ•Œ', async () => {
await Promise.all([redisService.zadd('feed:trend', 10, '6')]);
});
});
});

0 comments on commit 3b2a542

Please sign in to comment.