Skip to content

Commit

Permalink
fix: 캐싱 데이터 교체
Browse files Browse the repository at this point in the history
  • Loading branch information
ptyoiy committed Feb 14, 2024
1 parent fc21fb6 commit 3c3baca
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions redis/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ export const initializeRedis = async () => {
*/
const initAllOngoingEvents = async () => {
const redisKey = 'allEvents';
const eachEvents = await redisClient.keys(`event:*`);
const eventsFromDb = await Event.findAll({
where: {
expired: false // 진행중인 행사만 가져오기
}
}) as IEvent[];

// 개별 이벤트 캐싱
// 과거 캐싱 기록 제거
if (eachEvents.length > 0) {
await redisClient.del(eachEvents);
}
for (const event of eventsFromDb) {
const eventRedisKey = `event:${event.id}`;
await redisClient.set(eventRedisKey, JSON.stringify(event), { EX: EXPIRE });
}

// 전체 목록 캐싱
await redisClient.set(redisKey, JSON.stringify(eventsFromDb), { EX: EXPIRE });

console.log("전체 행사:", await redisClient.get(redisKey));
console.log("진행중인 행사 개수:", await redisClient.keys(`event:*`));
console.log("진행중인 행사:", eachEvents);
}

/** 공지 글 전부 캐싱
Expand All @@ -44,6 +49,7 @@ const initAllOngoingEvents = async () => {
*/
const initAllOngoingNotices = async (priority: '일반' | '긴급') => {
const redisKey = `alerts:${priority == '일반' ? 'general' : 'urgent'}`;
const eachNotices = await redisClient.keys(`notice:*`);
const noticesFromDB = await Notice.findAll({
where: {
expired: false, // 활성화된 공지만 가져오기
Expand All @@ -54,12 +60,16 @@ const initAllOngoingNotices = async (priority: '일반' | '긴급') => {
}) as INotice[];

// 개별 공지 캐싱
// 과거 캐싱 기록 제거
if (eachNotices.length) {
await redisClient.del(eachNotices);
}
for (const notice of noticesFromDB) {
const redisKey = `notice:${notice.id}`;
await redisClient.set(redisKey, JSON.stringify(notice), { EX: EXPIRE });
}
// 전체 공지 캐싱
await redisClient.set(redisKey, JSON.stringify(noticesFromDB), { EX: EXPIRE });

priority == '긴급' && console.log(`진행중인 공지: `, await redisClient.keys(`notice:*`));
priority == '긴급' && console.log(`진행중인 공지: `, eachNotices);
}

0 comments on commit 3c3baca

Please sign in to comment.