-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(caching): coalesce concurrent requests to wrap
function
#599
fix(caching): coalesce concurrent requests to wrap
function
#599
Conversation
Hi @douglascayers - thanks for putting this together. A couple of questions around this:
|
@jaredwray Thanks for the follow up. I've rebased this PR and have answers to your questions below. Thanks
This is a drop-in patch update. No breaking changes. Users don't have to make any changes to benefit from this change. The functional difference is that cache manager's When there's a cache miss, instead of calling the user's function to fetch a value to cache once per request (which can exhaust downstream API rate limits, be slower, etc), one promise goes off to do the work and each of the other concurrent requests receive a promise that promises to resolve to the same value as the first promise that's actually executing the user's function.
Yes, I intend to support it. I see value in the coalescing logic as a standalone helper library, which is agnostic to caching though that is the main driver for the solution. With that said, I'm not opposed to adding the code directly to this project if you all want to maintain it going forward.
The API for the This PR includes new tests to demonstrate the problem when I also have an example on the promise-coalesce project's README, too. Let me know if you have any other questions or concerns, thanks |
Codecov Report
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. @@ Coverage Diff @@
## master #599 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 5
Lines 292 295 +3
Branches 75 76 +1
=========================================
+ Hits 292 295 +3
|
@douglascayers - thanks so much for all the work on this and we will be working to get this deployed in the next couple weeks. |
Fantastic, thanks! |
[](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [cache-manager](https://togithub.com/node-cache-manager/node-cache-manager) | [`5.2.4` -> `5.3.1`](https://renovatebot.com/diffs/npm/cache-manager/5.2.4/5.3.1) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>node-cache-manager/node-cache-manager (cache-manager)</summary> ### [`v5.3.1`](https://togithub.com/node-cache-manager/node-cache-manager/releases/tag/v5.3.1) [Compare Source](https://togithub.com/node-cache-manager/node-cache-manager/compare/v5.3.0...v5.3.1) Major fix as `5.3.0` did not have the dist folder on release. #### What's Changed - updating to always run vitest with coverage by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/605](https://togithub.com/node-cache-manager/node-cache-manager/pull/605) - fix(refreshThreshold): don't run if ttl is -1 by [@​mihirgupta0900](https://togithub.com/mihirgupta0900) in [https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604) - fix(caching): coalesce concurrent requests to `wrap` function by [@​douglascayers](https://togithub.com/douglascayers) in [https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599) - upgrading lru-cache to 10.0.2 by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/609](https://togithub.com/node-cache-manager/node-cache-manager/pull/609) - upgrading eslint, prettier, and types to latest by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/610](https://togithub.com/node-cache-manager/node-cache-manager/pull/610) #### New Contributors - [@​mihirgupta0900](https://togithub.com/mihirgupta0900) made their first contribution in [https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604) - [@​douglascayers](https://togithub.com/douglascayers) made their first contribution in [https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599) **Full Changelog**: jaredwray/cacheable@5.2.4...v5.3.1 ### [`v5.3.0`](https://togithub.com/node-cache-manager/node-cache-manager/releases/tag/v5.3.0) [Compare Source](https://togithub.com/node-cache-manager/node-cache-manager/compare/5.2.4...v5.3.0) #### What's Changed - updating to always run vitest with coverage by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/605](https://togithub.com/node-cache-manager/node-cache-manager/pull/605) - fix(refreshThreshold): don't run if ttl is -1 by [@​mihirgupta0900](https://togithub.com/mihirgupta0900) in [https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604) - fix(caching): coalesce concurrent requests to `wrap` function by [@​douglascayers](https://togithub.com/douglascayers) in [https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599) - upgrading lru-cache to 10.0.2 by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/609](https://togithub.com/node-cache-manager/node-cache-manager/pull/609) - upgrading eslint, prettier, and types to latest by [@​jaredwray](https://togithub.com/jaredwray) in [https://github.com/node-cache-manager/node-cache-manager/pull/610](https://togithub.com/node-cache-manager/node-cache-manager/pull/610) #### New Contributors - [@​mihirgupta0900](https://togithub.com/mihirgupta0900) made their first contribution in [https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604) - [@​douglascayers](https://togithub.com/douglascayers) made their first contribution in [https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599) **Full Changelog**: jaredwray/cacheable@5.2.4...v5.3.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/tf2pickup-org/server). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy41OS44IiwidXBkYXRlZEluVmVyIjoiMzcuNTkuOCIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Fixes #417
In the
v4.x
version, thecaching.wrap()
function would coalesce multiple concurrent requests to minimize how many times the generator function was invoked. This was a helpful optimization to reduce load on downstream systems like databases and APIs.This pull request adds that cache miss relief logic back by using the promise-coalesce module, which is said logic in a generic, reusable function.
For tests, the
#417
issue test demonstrates the undesired behavior. The updatedwrap()
tests demonstrate the desired behavior by coalescing requests.Thanks