Skip to content

🧹 chore: Optimize cache middleware performance#3979

Merged
gaby merged 4 commits into
update-cache-handling-for-authenticationfrom
improve-cache-middleware-performance
Jan 3, 2026
Merged

🧹 chore: Optimize cache middleware performance#3979
gaby merged 4 commits into
update-cache-handling-for-authenticationfrom
improve-cache-middleware-performance

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented Jan 3, 2026

Summary

  • avoid holding the cache mutex during storage I/O and reuse fasthttp date handling when serving cached responses
  • refactor Cache-Control parsing to reuse parsed directives, using utils.EqualFold for directive comparisons, and clamp duration conversions
  • store cached response headers as a slice-backed structure and regenerate msgp code to match

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 3, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (3)
  • master
  • v2
  • main

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @gaby, 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 focuses on enhancing the performance and efficiency of the cache middleware. It achieves this by optimizing mutex handling to reduce contention during storage operations, refining the parsing of Cache-Control headers for better reuse and accuracy, and restructuring how response headers are stored to improve serialization and retrieval. These changes collectively aim to make the caching mechanism faster and more robust, particularly under high load.

Highlights

  • Concurrency Optimization: The cache mutex is no longer held during potentially blocking storage I/O operations, significantly improving concurrency and reducing contention within the cache middleware.
  • Efficient Cache-Control Parsing: Cache-Control header parsing has been refactored for greater efficiency, reusing parsed directives, utilizing utils.EqualFold for case-insensitive comparisons, and clamping duration conversions to prevent overflows.
  • Optimized Header Storage and Date Handling: Cached response headers are now stored as a slice-backed structure ([]cachedHeader) instead of a map, and fasthttp's highly optimized date handling is leveraged for improved performance when serving cached responses.
  • Serialization Code Regeneration: The msgp code for serialization and deserialization has been regenerated to accurately reflect the new slice-backed header storage structure, ensuring data integrity and compatibility.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 3, 2026

Codecov Report

❌ Patch coverage is 86.51685% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.92%. Comparing base (d8b3408) to head (4b8608c).
⚠️ Report is 5 commits behind head on update-cache-handling-for-authentication.

Files with missing lines Patch % Lines
middleware/cache/cache.go 86.51% 16 Missing and 8 partials ⚠️
Additional details and impacted files
@@                             Coverage Diff                              @@
##           update-cache-handling-for-authentication    #3979      +/-   ##
============================================================================
- Coverage                                     90.99%   90.92%   -0.08%     
============================================================================
  Files                                           119      119              
  Lines                                         10672    10749      +77     
============================================================================
+ Hits                                           9711     9773      +62     
- Misses                                          610      618       +8     
- Partials                                        351      358       +7     
Flag Coverage Δ
unittests 90.92% <86.51%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 significant performance optimizations to the cache middleware. The changes focus on reducing lock contention by releasing the mutex during I/O operations, which is a great improvement. The refactoring of Cache-Control header parsing to a zero-allocation, single-pass implementation is excellent for performance. Additionally, switching to fasthttp's date handling functions and changing the internal storage of headers from a map to a slice are smart optimizations. The code is cleaner and more efficient. I've found a few minor areas for improvement regarding some redundant lock operations in error handling paths, but overall this is a high-quality contribution.

Comment thread middleware/cache/cache.go Outdated
Comment thread middleware/cache/cache.go Outdated
Comment thread middleware/cache/cache.go Outdated
@gaby gaby marked this pull request as ready for review January 3, 2026 18:34
@gaby gaby requested a review from a team as a code owner January 3, 2026 18:34
@gaby gaby requested review from ReneWerner87, efectn and sixcolors and removed request for a team January 3, 2026 18:34
@gaby gaby merged commit 80d999e into update-cache-handling-for-authentication Jan 3, 2026
17 of 20 checks passed
@gaby gaby deleted the improve-cache-middleware-performance branch January 3, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant