Skip to content

Commit

Permalink
♻️ refactor: 싱글톤 패턴 getInstance() 제거, 단일 객체 export 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo-Minseok committed Jan 3, 2025
1 parent 2307892 commit 28e240c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
19 changes: 8 additions & 11 deletions feed-crawler/src/common/mysql-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import { PoolConnection } from "mysql2/promise";
import logger from "./logger";
import { FeedDetail, RssObj } from "./types";

export class MySQLRepository {
private static instance: MySQLRepository;
private pool = this.createPool();
private nameTag: string = "[MySQL]";
private constructor() {}

static getInstance(): MySQLRepository {
if (!MySQLRepository.instance) {
MySQLRepository.instance = new MySQLRepository();
}
return MySQLRepository.instance;
class MySQLRepository {
private pool: mysql.Pool;
private nameTag: string;
constructor() {
this.pool = this.createPool();
this.nameTag = "[MySQL]";
}

private createPool() {
Expand Down Expand Up @@ -100,3 +95,5 @@ export class MySQLRepository {
await this.pool.end();
}
}

export const mysqlRepository = new MySQLRepository();
20 changes: 9 additions & 11 deletions feed-crawler/src/common/redis-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import logger from "./logger";
import { redisConstant } from "./constant";
import { FeedDetail } from "./types";

export class RedisRepository {
private static instance: RedisRepository;
class RedisRepository {
private redis: Redis;
private nameTag: string = "[Redis]";
private constructor() {}
private nameTag: string;

constructor() {
this.nameTag = "[Redis]";
}

private connect() {
this.redis = new Redis({
host: process.env.REDIS_HOST,
Expand All @@ -17,13 +20,6 @@ export class RedisRepository {
});
}

static getInstance() {
if (!RedisRepository.instance) {
RedisRepository.instance = new RedisRepository();
}
return RedisRepository.instance;
}

async deleteRecentFeed() {
try {
this.connect();
Expand Down Expand Up @@ -109,3 +105,5 @@ export class RedisRepository {
);
}
}

export const redisRepository = new RedisRepository();
16 changes: 6 additions & 10 deletions feed-crawler/src/feed-crawler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RedisRepository } from "./common/redis-access.js";
import { MySQLRepository } from "./common/mysql-access.js";
import { redisRepository } from "./common/redis-access.js";
import { mysqlRepository } from "./common/mysql-access.js";
import logger from "./common/logger.js";
import { RssObj, FeedDetail, RawFeed } from "./common/types.js";
import { XMLParser } from "fast-xml-parser";
Expand All @@ -13,20 +13,16 @@ dotenv.config({
});

export class FeedCrawler {
private mysql: MySQLRepository;
private redis: RedisRepository;
private urlParser: URLParser;

constructor() {
this.mysql = MySQLRepository.getInstance();
this.redis = RedisRepository.getInstance();
this.urlParser = new URLParser();
}

async start() {
await this.redis.deleteRecentFeed();
await redisRepository.deleteRecentFeed();

const rssObjects = await this.mysql.selectAllRss();
const rssObjects = await mysqlRepository.selectAllRss();

if (!rssObjects || !rssObjects.length) {
logger.info("등록된 RSS가 없습니다.");
Expand All @@ -42,9 +38,9 @@ export class FeedCrawler {
}
logger.info(`총 ${newFeeds.length}개의 새로운 피드가 있습니다.`);

const insertedData = await this.mysql.insertFeeds(newFeeds);
const insertedData = await mysqlRepository.insertFeeds(newFeeds);

await this.redis.setRecentFeedList(insertedData);
await redisRepository.setRecentFeedList(insertedData);
}

private async findNewFeeds(
Expand Down

0 comments on commit 28e240c

Please sign in to comment.