feat: Redis 싱글턴 클라이언트 추가 + kis_realtime_stream SETEX 연동

- backend/app/core/redis.py 신규 생성
  - TTL 상수: TTL_TICK/QUOTE=10s, RECOMMENDATIONS=1800s, ANALYSIS=3600s
  - init_redis() / close_redis() → FastAPI lifespan 훅
  - setex_json() / get_json() / delete_key() 헬퍼
  - Redis 미연결 시 경고 로그 후 non-blocking 계속 동작
- kis_realtime_stream.py: tick/quote 수신 즉시 Redis SETEX 캐시
  - _cache_realtime_data() 모듈 함수 추가
  - tick:{code} / quote:{code} 키 저장 (API 없이 현재가 조회 가능)
- main.py lifespan: init_redis() / close_redis() 호출 추가
This commit is contained in:
2026-02-25 10:01:00 +09:00
parent 7517aa6ce8
commit 3a416b7598
3 changed files with 166 additions and 5 deletions

View File

@@ -351,10 +351,14 @@ async def lifespan(app: FastAPI):
except Exception as e:
logger.warning(f"⚠️ 데이트레이딩 포지션 복구 실패 (non-critical): {e}")
# ── httpx 싱글톤 클라이언트 초기화 (이슈 6) ─────────────────────────
# ── httpx 싱글톤 클라이언트 초기화 ──────────────────────────────────────
from app.api.endpoints.mcp import init_http_client, close_http_client
await init_http_client()
# ── Redis 클라이언트 초기화 ───────────────────────────────────────────────
from app.core.redis import init_redis, close_redis
await init_redis()
# 워커 + 스케줄러 시작
app.state.db_worker_task = asyncio.create_task(db_writer_worker())
app.state.auto_trade_task = asyncio.create_task(auto_trade_scheduler())
@@ -385,12 +389,18 @@ async def lifespan(app: FastAPI):
except Exception as e:
logger.warning(f"Failed to stop {task_attr} gracefully: {e}")
# ── httpx 클라이언트 종료 (이슈 6) ──────────────────────────────────
# ── httpx 클라이언트 종료 ─────────────────────────────────────────────────
try:
await close_http_client()
except Exception as e:
logger.warning(f"Failed to close http_client: {e}")
# ── Redis 클라이언트 종료 ─────────────────────────────────────────────────
try:
await close_redis()
except Exception as e:
logger.warning(f"Failed to close Redis: {e}")
logger.info("Shutting down Auto Stock Trading System...")
await engine.dispose()