Skip to content

refactor: TanStack Query 캐싱 정책 도입 및 QueryClient 인스턴스 고정 - #18

Merged
LeeSangHyeok0731 merged 2 commits into
mainfrom
refactor/query-caching-policy
Jul 13, 2026
Merged

refactor: TanStack Query 캐싱 정책 도입 및 QueryClient 인스턴스 고정#18
LeeSangHyeok0731 merged 2 commits into
mainfrom
refactor/query-caching-policy

Conversation

@LeeSangHyeok0731

Copy link
Copy Markdown
Contributor

개요 💡

TanStack Query에 staleTime이 설정되어 있지 않아, 페이지를 이동할 때마다 이미 받아온 데이터를 매번 다시 요청하고 있었습니다. 도메인별 캐싱 정책을 도입해 불필요한 API 호출을 줄입니다.

작업내용 ⌨️

1. 도메인별 캐싱 정책 도입 (shared/constants/queryOptions.ts)

데이터 성격에 따라 staleTime을 분리했습니다. 모든 쿼리에 동일한 값을 주면 실시간성이 중요한 세탁기 상태가 오래된 값으로 보이거나, 거의 바뀌지 않는 목록을 불필요하게 재요청하게 됩니다.

도메인 staleTime 근거
세탁기 상태 30초 사용자가 기기를 쓰는 즉시 변경됨
예약 목록 1분 비교적 자주 변경됨
고장 신고 / 사용자 목록 3분 변경 빈도가 낮음
관리자 본인 정보 30분 세션 중 사실상 고정

전역 기본값으로 staleTime 1분, gcTime 10분을 설정했습니다.

2. QueryClient 인스턴스 고정 (shared/lib/TanStackProvider.tsx)

new QueryClient()가 컴포넌트 본문에서 호출되고 있어, 리렌더가 발생하면 캐시가 통째로 버려질 수 있는 구조였습니다. useState로 인스턴스를 고정했습니다.

참고: 실제 계측 결과 현재 이 프로젝트에서는 reactCompiler의 메모이제이션 덕분에 해당 리렌더가 발생하지 않아 요청 수에 미치는 영향은 없었습니다. 다만 리렌더가 발생하는 순간 캐싱이 전부 무력화되는 구조이므로 함께 수정했습니다.

계측 결과 📊

axios 요청 인터셉터에 임시 카운터를 붙여, 고정 시나리오(대시보드 → 세탁기 → 예약 → 신고 → 사용자 → 대시보드 → 세탁기 → 예약) 기준으로 측정했습니다. 계측용 코드는 이 PR에 포함되지 않습니다.

상태 API 요청 수
개선 전 (staleTime 없음) 45건
개선 후 7건

재방문 구간에서 발생하던 중복 요청이 제거되어 약 84% 감소했습니다.

검증 ✅

  • pnpm lint — 통과 (기존 쿠키 유틸 관련 경고 2건은 이 변경과 무관)
  • pnpm check-types — 통과
  • pnpm build — 통과

staleTime이 설정되지 않아 페이지 이동마다 동일 데이터를 재요청하던 문제를 해결한다.
도메인 특성에 따라 staleTime을 분리하고 전역 기본 캐싱 정책을 추가한다.
QueryClient가 렌더마다 재생성되어 캐시가 버려질 수 있는 구조도 useState로 고정한다.
@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
washer Ready Ready Preview, Comment Jul 13, 2026 11:22am

Request Review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces domain-specific staleTime configurations for various TanStack Query hooks and wraps the QueryClient instantiation in useState within TanStackProvider to prevent unnecessary cache resets on re-renders. The feedback points out a potential issue where the global DEFAULT_GC_TIME (10 minutes) is shorter than the staleTime for MY_INFO (30 minutes), which would cause the cache to be garbage collected prematurely. It is recommended to increase DEFAULT_GC_TIME to at least 30 minutes to ensure the cache is preserved as expected.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/shared/constants/queryOptions.ts Outdated
gcTime(10분)이 MY_INFO의 staleTime(30분)보다 짧아 비활성 쿼리의 캐시가
먼저 수거되던 문제를 해결한다.
@LeeSangHyeok0731
LeeSangHyeok0731 merged commit a5008e6 into main Jul 13, 2026
3 checks passed
@LeeSangHyeok0731
LeeSangHyeok0731 deleted the refactor/query-caching-policy branch July 13, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants