Skip to content

feat(core): implement generic CacheService and optimize setupUser#21374

Merged
sehoon38 merged 4 commits intomainfrom
feat/setup-user-cache
Mar 6, 2026
Merged

feat(core): implement generic CacheService and optimize setupUser#21374
sehoon38 merged 4 commits intomainfrom
feat/setup-user-cache

Conversation

@sehoon38
Copy link
Copy Markdown
Contributor

@sehoon38 sehoon38 commented Mar 6, 2026

Summary

Introduces a generic, type-safe CacheService in the core package and refactors setupUser to use it. This provides a consistent way to handle in-memory caching with TTL support and automatic promise cleanup across the project.

Details

  • packages/core/src/utils/cache.ts:
    • Implemented CacheService supporting both Map and WeakMap backends.
    • Added TTL (Time To Live) support for automatic entry expiration.
    • Added automatic removal of failed promises from the cache to allow immediate retries.
    • Exported a globalCache singleton for general-purpose string-keyed caching.
  • packages/core/src/code_assist/setup.ts:
    • Refactored setupUser to use the new CacheService.
    • Centralized caching logic, making the function implementation cleaner and more robust.
  • Tests:
    • Added a comprehensive test suite for the new cache utility in cache.test.ts.
    • Consolidated and updated setup.test.ts to verify the new caching integration.

Related Issues

Related to #21310

How to Validate

  1. Run npm test -w @google/gemini-cli-core -- src/utils/cache.test.ts
  2. Run npm test -w @google/gemini-cli-core -- src/code_assist/setup.test.ts
  3. Verify the build with npm run build.

Pre-Merge Checklist

  • Added/updated tests (if needed)
  • Validated on required platforms/methods:
    • MacOS
      • npm run

@sehoon38 sehoon38 requested a review from a team as a code owner March 6, 2026 07:02
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Mar 6, 2026

Hi @sehoon38, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the core library by introducing a generic and robust caching service. This service provides efficient in-memory data storage with features like time-to-live expiration and intelligent handling of asynchronous operations. Its integration into the setupUser function streamlines user data retrieval, leading to improved performance, cleaner code, and better maintainability across the project.

Highlights

  • Generic CacheService Implementation: A new, type-safe CacheService has been introduced in the core package, offering flexible in-memory caching with support for both Map and WeakMap backends.
  • Time-To-Live (TTL) Support: The CacheService includes configurable Time-To-Live (TTL) functionality, allowing cache entries to automatically expire after a specified duration.
  • Automatic Promise Cleanup: The CacheService now automatically removes promises from the cache if they reject, enabling immediate retries for failed asynchronous operations.
  • Refactored setupUser Function: The setupUser function has been refactored to utilize the new CacheService, centralizing caching logic and improving its robustness and performance.
  • Comprehensive Testing: New and updated test suites have been added for both the CacheService and the setupUser function, ensuring the reliability and correctness of the new caching mechanisms.
Changelog
  • packages/core/src/code_assist/setup.test.ts
    • Updated copyright year to 2026.
    • Removed ChangeAuthRequestedError import.
    • Imported resetUserDataCacheForTesting for test isolation.
    • Consolidated describe blocks for setupUser tests into a single block.
    • Added resetUserDataCacheForTesting() call in beforeEach to ensure test isolation.
    • Introduced new test suites specifically for caching behavior, covering scenarios like cache hits, project ID changes, expiration, and failed promises.
    • Refactored existing tests into existing user and new user describe blocks for better organization.
    • Simplified validation and error handling tests to align with the new caching approach.
  • packages/core/src/code_assist/setup.ts
    • Imported createCache from ../utils/cache.js.
    • Implemented userDataCache using createCache with WeakMap storage and a 30-second TTL for setupUser results.
    • Exported resetUserDataCacheForTesting to allow tests to clear the cache.
    • Refactored setupUser to act as a caching wrapper, storing and retrieving UserData promises keyed by AuthClient and projectId.
    • Introduced _doSetupUser as an internal function to encapsulate the original setupUser logic, now called by the caching wrapper.
    • Added logic within the caching wrapper to remove failed promises from the cache, enabling immediate retries.
  • packages/core/src/index.ts
    • Exported all contents from the new utils/cache.js module.
  • packages/core/src/utils/cache.test.ts
    • Added a new test file for the CacheService.
    • Included tests for basic cache operations: get, set, delete, and clear.
    • Tested Time-To-Live (TTL) and expiration behavior for cache entries.
    • Verified the functionality of the getOrCreate method.
    • Tested promise support, specifically the automatic removal of failed promises from the cache.
    • Confirmed WeakMap storage behavior with object keys.
  • packages/core/src/utils/cache.ts
    • Added a new file defining CacheEntry, CacheOptions, and the CacheService class.
    • Implemented CacheService to support configurable storage (Map or WeakMap), defaultTtl, and deleteOnPromiseFailure for promises.
    • Provided get, set, getOrCreate, delete, and clear methods for cache management.
    • Ensured the get method correctly handles TTL expiration.
    • Included logic in the set method to remove failed promises from the cache if deleteOnPromiseFailure is true.
    • Made the clear method throw an error if used with WeakMap storage.
    • Provided a createCache factory function for easy cache instantiation.
    • Exported a globalCache singleton with Map storage and a 5-minute TTL for general-purpose use.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

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

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 a generic CacheService and refactors setupUser to use it for caching, which is a great improvement for performance and consistency. The changes are well-tested. However, I've identified a critical type-safety issue in the new CacheService API that could lead to silent failures, and an opportunity to simplify the setupUser implementation by better leveraging the new caching utility.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 6, 2026

Size Change: +3.07 kB (+0.01%)

Total Size: 26 MB

Filename Size Change
./bundle/gemini.js 25.5 MB +3.07 kB (+0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@gemini-cli gemini-cli bot added area/core Issues related to User Interface, OS Support, Core Functionality area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Mar 6, 2026
@sehoon38 sehoon38 force-pushed the feat/setup-user-cache branch from ead8cc3 to bf909cf Compare March 6, 2026 18:36
@sehoon38 sehoon38 added this pull request to the merge queue Mar 6, 2026
Merged via the queue into main with commit 7dce23e Mar 6, 2026
27 checks passed
@sehoon38 sehoon38 deleted the feat/setup-user-cache branch March 6, 2026 19:52
@sehoon38
Copy link
Copy Markdown
Contributor Author

sehoon38 commented Mar 8, 2026

saved ~300ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants