Skip to content

feat: getOrSet#1125

Merged
jaredwray merged 4 commits intojaredwray:mainfrom
corradodellorusso:feat/get-or-set
May 28, 2025
Merged

feat: getOrSet#1125
jaredwray merged 4 commits intojaredwray:mainfrom
corradodellorusso:feat/get-or-set

Conversation

@corradodellorusso
Copy link
Contributor

@corradodellorusso corradodellorusso commented May 20, 2025

Please check if the PR fulfills these requirements

  • Followed the Contributing guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
This pull request introduces a new getOrSet method to the Cacheable class, refactors the wrap function to leverage this new method, and adds corresponding tests to ensure functionality. The changes aim to improve cache management by simplifying code and preventing cache stampedes.

New Features:

  • Added a getOrSet method to the Cacheable class, which retrieves a cached value or computes and caches it if not present. This method includes support for TTL and error caching. (packages/cacheable/src/index.ts)

Refactoring:

  • Refactored the wrap function to delegate caching logic to the new getOrSet method, simplifying the implementation. (packages/cacheable/src/wrap.ts)

Type and Import Updates:

  • Introduced a new GetOrSetOptions type to encapsulate options for the getOrSet method. (packages/cacheable/src/index.ts)
  • Updated imports to include coalesceAsync in index.ts and removed it from wrap.ts as it is now used exclusively in getOrSet. (packages/cacheable/src/index.ts, packages/cacheable/src/wrap.ts) [1] [2]

Testing Enhancements:

  • Added tests for the getOrSet method to verify proper caching behavior and prevention of cache stampedes. (packages/cacheable/test/index.test.ts)
  • Minor cleanup in wrap.test.ts by removing unused imports. (packages/cacheable/test/wrap.test.ts)

@corradodellorusso corradodellorusso changed the title Feat/get or set feat: getOrSet May 20, 2025
@jaredwray
Copy link
Owner

@corradodellorusso - thanks and will do a review tomorrow.

Copy link
Owner

@jaredwray jaredwray left a comment

Choose a reason for hiding this comment

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

I noticed you replaced the wrap function with your getOrSet. Should we do that as I do plan long term to have wrap be in its own module that enables cache-manager and cacheable.

@jaredwray jaredwray merged commit 0687a58 into jaredwray:main May 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments