Skip to content
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

feat: improve caching - adds TTL, cacheTransport and cacheGetKey #739

Merged
merged 4 commits into from
May 24, 2023

Conversation

iamelevich
Copy link
Contributor

@iamelevich iamelevich commented Mar 31, 2023

Problem

Current cache implementation not really useful in production, because it can store only first response, but this behaviour not good for a lot of cases. For example: I'm using Circuit Breaker to get config data and endpoint returns different responses based on arguments. I want it to be cached, but based on parameters.

Solution

  • Make cache configurable: using options.cacheTransport you can implement cache solution that you want.
  • Added cacheTTL option to update cached value after expiration time.
  • Added cacheGetKey option to implement way how to get cache key from arguments.

Problems

  • All methods in cacheTransport should be sync, so you can only implement something that don't need async operations.
  • Default method to createKey uses JSON.stringify and this is not optimal solution, so better to customize it.

Signed-off-by: Ilya Amelevich <ilya.amelevich@gmail.com>
@coveralls
Copy link

coveralls commented Mar 31, 2023

Pull Request Test Coverage Report for Build 5069068464

  • 29 of 30 (96.67%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 98.236%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/circuit.js 19 20 95.0%
Totals Coverage Status
Change from base Build 4356082473: -0.2%
Covered Lines: 355
Relevant Lines: 356

💛 - Coveralls

@mateus4k
Copy link

What about performance before and after these changes?

@iamelevich
Copy link
Contributor Author

What about performance before and after these changes?

I'm not sure are there exists any preformance tests to measure it. Probably you can help me with that?

The best approach for caching - let user define cache logic and transport for caching. Just provide interface. I will try to do that when will have some free time.

Current solution just make built-in caching more useful, cause for a lot of usage scenarios it's not enough to cache only first response.

refactor: move default cache implementation to the separate file
Signed-off-by: Ilya Amelevich <ilya.amelevich@gmail.com>
Signed-off-by: Ilya Amelevich <ilya.amelevich@gmail.com>
@iamelevich iamelevich changed the title feat: add cacheTTL option Improve caching May 1, 2023
Copy link
Member

@lance lance left a comment

Choose a reason for hiding this comment

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

@iamelevich this looks great! Thanks for the contribution. 🙇 I just have one small suggestion.

lib/circuit.js Outdated Show resolved Hide resolved
lib/circuit.js Show resolved Hide resolved
Signed-off-by: Ilya Amelevich <ilya.amelevich@gmail.com>
@iamelevich
Copy link
Contributor Author

@lance Done. Please check.

@iamelevich iamelevich requested a review from lance May 24, 2023 13:09
@lance lance changed the title Improve caching feat: improve caching - adds TTL, cacheTransport and cacheGetKey May 24, 2023
Copy link
Member

@lance lance left a comment

Choose a reason for hiding this comment

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

@iamelevich thanks again for this contribution!

@lance lance merged commit dc9be53 into nodeshift:main May 24, 2023
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.

4 participants