-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from jjikky/refactor/word-performance
Refactor/word performance
- Loading branch information
Showing
10 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const cron = require('node-cron'); | ||
const redisClient = require('../redis'); | ||
const mongoose = require('mongoose'); | ||
const Word = require('../../../routes/word/word.model'); | ||
|
||
// NOTE : 매 정각마다 Redis 조회수를 DB에 반영 | ||
cron.schedule('0 * * * *', async () => { | ||
try { | ||
const cachedWordsRaw = await redisClient.sendCommand(['ZRANGE', 'popular_words', '0', '-1', 'WITHSCORES']); | ||
|
||
const updates = []; | ||
for (let i = 0; i < cachedWordsRaw.length; i += 2) { | ||
const word = cachedWordsRaw[i]; | ||
const redisFreq = Number(cachedWordsRaw[i + 1]); | ||
|
||
// NOTE : word 스키마의 조회수 증가 미들웨어를 우회하기 위해 Mongoose 대신 MongoDB 드라이버를 사용 | ||
const dbWord = await mongoose.connection.collection('words').findOne({ word }, { projection: { freq: 1 } }); | ||
const dbFreq = dbWord ? dbWord.freq : 0; | ||
|
||
if (redisFreq !== dbFreq) { | ||
updates.push({ | ||
updateOne: { | ||
filter: { word }, | ||
update: { freq: redisFreq }, | ||
}, | ||
}); | ||
} | ||
} | ||
if (updates.length > 0) { | ||
await Word.bulkWrite(updates); | ||
console.log('✅ Redis 조회수를 DB에 성공적으로 동기화했습니다.'); | ||
} else { | ||
console.log('ℹ️ 이미 동기화된 조회수입니다.'); | ||
} | ||
} catch (error) { | ||
console.error('❌ Redis와 DB 동기화 중 오류 발생:', error); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = (req, res, next) => { | ||
const startTime = Date.now(); | ||
|
||
res.on('finish', () => { | ||
const latency = Date.now() - startTime; | ||
console.log(`API Latency for ${req.method} ${req.originalUrl}: ${latency}ms\n`); | ||
}); | ||
|
||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters