11const FinancialSummary = require ( "../models/FinancialSummary" ) ;
22const { fetchStockPrice } = require ( "./fetchStockPrice" ) ;
3+ const NodeCache = require ( "node-cache" ) ;
4+
5+ const cache = new NodeCache ( { stdTTL : 3600 } ) ; // 1시간 캐시
36
47/** reprt_code → 분기 말일 매핑 */
58const PERIOD_END = {
@@ -21,6 +24,14 @@ function getPeriodDate(entry) {
2124 * @returns {Promise<object> } 계산된 지표들
2225 */
2326async function computeMetrics ( stockCode , dateStr ) {
27+ const cacheKey = `${ stockCode } _${ dateStr } ` ;
28+ console . log ( "cacheKey 생성:" , cacheKey ) ;
29+
30+ const cached = cache . get ( cacheKey ) ;
31+ if ( cached ) {
32+ console . log ( "📦 캐시된 데이터 반환:" , cacheKey ) ;
33+ return cached ;
34+ }
2435 // DB에서 불러오기
2536 const doc = await FinancialSummary . findOne ( { stock_code : stockCode } ) . lean ( ) ;
2637 if ( ! doc ) throw new Error ( "금융 요약 데이터가 없습니다: " + stockCode ) ;
@@ -64,9 +75,9 @@ async function computeMetrics(stockCode, dateStr) {
6475 ) ;
6576
6677 // 지표 계산
67- const per = eps ? stockPrice / eps : null ;
68- const pbr = bps ? stockPrice / bps : null ;
69- const psr = ttmRevenue ? ( stockPrice * shareCount ) / ttmRevenue : null ;
78+ // const per = eps ? stockPrice / eps : null;
79+ // const pbr = bps ? stockPrice / bps : null;
80+ // const psr = ttmRevenue ? (stockPrice * shareCount) / ttmRevenue : null;
7081
7182 // 시계열 데이터
7283 const series = {
@@ -80,16 +91,10 @@ async function computeMetrics(stockCode, dateStr) {
8091 operatingGrowthRate : valid . map ( ( e ) => e . operating_growth_rate ) ,
8192 } ;
8293
83- return {
94+ const result = {
8495 price : { price : stockPrice , date : priceDate } ,
85- per,
86- pbr,
87- psr,
8896 stockPrice,
8997 shareCount,
90- // per,
91- // psr,
92- // pbr,
9398 eps,
9499 bps,
95100 roe : last . roe ,
@@ -100,6 +105,11 @@ async function computeMetrics(stockCode, dateStr) {
100105 profit_diff_rate : last . profit_diff_rate ,
101106 series,
102107 } ;
108+
109+ cache . set ( `${ stockCode } _${ dateStr } ` , result ) ;
110+ console . log ( "📝 캐시에 저장함:" , `${ stockCode } _${ dateStr } ` ) ;
111+
112+ return result ;
103113}
104114
105115module . exports = { computeMetrics } ;
0 commit comments