Skip to content

feat: Implement resource-cache synchronization Logic#6134

Merged
piggggggggy merged 2 commits intocloudforet-io:feature-extra-queryfrom
piggggggggy:resource-sync
Aug 19, 2025
Merged

feat: Implement resource-cache synchronization Logic#6134
piggggggggy merged 2 commits intocloudforet-io:feature-extra-queryfrom
piggggggggy:resource-sync

Conversation

@piggggggggy
Copy link
Copy Markdown
Member

Skip Review (optional)

  • Minor changes that don't affect the functionality (e.g. style, chore, ci, test, docs)
  • Previously reviewed in feature branch, further review is not mandatory
  • Self-merge allowed for solo developers or urgent changes

Description (optional)

Introduce centralized composable, useResourceCacheSync, to manage consistency of resource query cache.

Previously, there was no standard way to update or invalidate the client-side cache after data-mutating operations (Create, Update, Delete), which could lead to stale data being displayed in the UI. This new utility provides a robust and reusable solution to keep the cache synchronized with the server state.

Changes:

  • Added useResourceCacheSync Composable:

    • Created a new composable that provides higher-order functions to wrap API calls.
    • wrapResourceCacheRefresh: Invalidates the relevant resource cache after an API call is settled, ensuring fresh data is fetched on the next query.
    • wrapResourceCacheUpdate: Directly updates the resource cache with the response data from an API call, providing a more immediate UI update without a full refetch.
  • Applied Cache Wrappers to API Clients:

    • Integrated the wrapResourceCacheRefresh and wrapResourceCacheUpdate functions into the CUD (Create, Update, Delete) methods within the api-clients composables (e.g., useProjectApi, useUserApi, useCollectorApi, etc.).
    • This automates the cache synchronization process, making the data flow more predictable and reliable across the application.

useResourceCacheSync 도입

기존에는 데이터 변경(생성, 수정, 삭제) 작업 후 클라이언트 캐시를 업데이트하거나 무효화하는 표준화된 방법이 없어 UI에 오래된(stale) 데이터가 표시될 위험이 있었습니다. 이 유틸리티는 캐시와 서버 상태의 동기화를 제공합니다.

주요 변경 사항:

  • useResourceCacheSync Composable 추가:

    • API 호출을 감싸는(wrap) 고차 함수를 제공하는 신규 Composable을 구현
    • wrapResourceCacheRefresh: API 호출이 완료된 후 관련 리소스 캐시를 무효화(invalidate)하여, 다음번 조회 시 최신 데이터를 가져오도록 보장함
    • wrapResourceCacheUpdate: API 호출의 응답 데이터로 캐시를 직접 업데이트하여, 전체 목록을 다시 가져오지 않고도 즉각적인 UI 변경을 제공함
  • API 클라이언트에 캐시 래퍼 적용:

    • api-clients 내의 Composable(예: useProjectApi, useUserApi, useCollectorApi 등)에서 CUD(생성, 수정, 삭제) 관련 메서드에 wrapResourceCacheRefreshwrapResourceCacheUpdate 함수를 적용
    • 이는 캐시 동기화 프로세스를 자동화하여 애플리케이션 전반에 걸쳐 데이터 흐름의 예측 가능성 보장

Things to Talk About (optional)

Signed-off-by: samuel.park <samuel.park@megazone.com>
Signed-off-by: samuel.park <samuel.park@megazone.com>
@piggggggggy piggggggggy requested a review from Copilot August 18, 2025 06:26
@vercel
Copy link
Copy Markdown

vercel bot commented Aug 18, 2025

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

3 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
console Ignored Ignored Preview Aug 18, 2025 6:26am
mfa-saas-qa Ignored Ignored Aug 18, 2025 6:26am
web-storybook Ignored Ignored Aug 18, 2025 6:26am

@github-actions
Copy link
Copy Markdown
Contributor

🎉 @seungyeoneeee has been randomly selected as the reviewer! Please review. 🙏

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a centralized resource cache synchronization system through the useResourceCacheSync composable to ensure UI consistency after data-mutating operations across the application.

  • Implements wrapResourceCacheRefresh and wrapResourceCacheUpdate functions to handle cache invalidation and updates
  • Refactors the composable to use generic wrapper functions instead of callback-specific methods
  • Applies cache synchronization to CUD operations across 20+ API client composables

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

File Description
use-resource-cache-sync.ts Core composable refactored with generic wrapper functions and improved type safety
use-resource-cache-sync.test.ts Comprehensive test suite covering both wrapper functions with success/failure scenarios
API client composables (20+ files) Integration of cache refresh wrappers for create, update, delete, and state-change operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

updatedResults[newDataId] = newData;
return updatedResults;
return { ...currentResults, [newDataId]: newData };
}
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

The logic for updating existing vs creating new cache entries is identical, making the conditional check redundant. Both branches return the same spread operation.

Copilot uses AI. Check for mistakes.
@piggggggggy piggggggggy requested review from sulmoJ and yuda110 and removed request for seungyeoneeee August 18, 2025 06:29
Copy link
Copy Markdown
Member

@sulmoJ sulmoJ left a comment

Choose a reason for hiding this comment

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

I have a question. I understood how to use wrapResourceCacheRefresh.
However, I don’t clearly understand wrapResourceCacheUpdate.
Could you explain its use case in more detail?

Thank you for your hardwork.

queryClient.setQueryData(resourceQueryKey, { 'project-123': initialData });

const mockApiFn = vi.fn().mockResolvedValue(updatedData);
const wrappedFn = wrapResourceCacheUpdate(mockApiFn);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommend: I think naming it updateProject is easier for other developers to understand than wrappedFn.

@piggggggggy piggggggggy merged commit 7273991 into cloudforet-io:feature-extra-query Aug 19, 2025
8 checks passed
piggggggggy added a commit that referenced this pull request Aug 19, 2025
* refactor(resource-cache-sync): make improvements useResourceCachSync



* feat(resource-cache): apply resource-query-sync HOF to api-clients



---------

Signed-off-by: samuel.park <samuel.park@megazone.com>
@piggggggggy piggggggggy deleted the resource-sync branch August 22, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants