# BAT-005 Implementation - Status: completed - Batch: BAT-005 ## Plan Summary - REQ-016: KISBroker 에 선택적 Redis 토큰 캐시(L1 인메모리 → L2 캐시 → 발급) + graceful fallback. orbit Redis 패턴 이식. ## Changed Files - trex_core/adapters/broker/token_cache.py (신규): `TokenCache` Protocol, `RedisTokenCache`(redis.from_url, 키 `trex:broker:kis:{account_ref}:token`, JSON 토큰+만료+app_key suffix, ex=ttl, 예외 graceful), `InMemoryTokenCache`(더블/폴백). - trex_core/adapters/broker/kis.py: `token_cache` 주입, `_ensure_token` 3-레벨(L1/L2/issue) + 발급 시 store. - trex_core/runtime/config.py: `KISConfig.redis_url` + `TREX_KIS_REDIS_URL`. - trex_core/runtime/daemon.py: redis_url 있으면 `RedisTokenCache` 주입. - pyproject.toml: `redis>=5` 의존. - tests/adapters/broker/test_token_cache.py (신규). ## Execution Log 1. TokenCache 추상화 + Redis/InMemory 구현(app_key suffix 안전, 만료 검증, 예외 graceful). 2. KISBroker _ensure_token 3-레벨 + store. 3. config/daemon wiring + pyproject redis. 4. 테스트 작성, redis 라이브러리 설치 후 실행. ## Validation - `pytest tests/adapters/broker/test_token_cache.py`: 4 passed (캐시 히트 HTTP 0 / 인스턴스 간 공유 / graceful fallback / app_key suffix 무효화). - 전체 `pytest`: **96 passed** (기존 92 + 신규 4), ruff clean. - 실 Redis 서버 불요: 인메모리 캐시 더블 + httpx.post 스텁으로 결정적 검증. RedisTokenCache 의 redis.from_url 경로는 운영 시 사용(라이브러리 import OK 확인). ## Remaining Risks - 없음(저위험). Redis 장애 시 graceful fallback(발급 진행). 실 Redis 서버 통합 검증은 운영 환경에서(라이브러리/와이어링 확인 완료).