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

Allow cache to be async #26

Merged
merged 18 commits into from
Oct 13, 2021
Merged

Conversation

Richienb
Copy link
Contributor

@Richienb Richienb commented Sep 24, 2021

Fixes #3, Fixes #14, Closes #19

index.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <[email protected]>
Signed-off-by: Richie Bendall <[email protected]>
index.ts Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <[email protected]>
Signed-off-by: Richie Bendall <[email protected]>
index.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
readme.md Show resolved Hide resolved
test-d/index.test-d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

CI is failing.

Richienb and others added 3 commits October 5, 2021 14:43
Signed-off-by: Richie Bendall <[email protected]>
@sindresorhus
Copy link
Owner

For future reference, it would have been better if this pull request was split into a separate pull request for each issue it fixed. Massive pull requests are a pain to review.

@sindresorhus
Copy link
Owner

sindresorhus commented Oct 9, 2021

Does this introduce any breaking changes other than removal of maxAge? Would you be able to list the changes in this PR which can be used for the release notes?

index.ts Show resolved Hide resolved
test.ts Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <[email protected]>
@Richienb
Copy link
Contributor Author

Richienb commented Oct 9, 2021

  • Allow .get() and .has() cache methods to return a promise
  • Remove maxAge option. This means instead of .set() being provided an object with the properties value and maxAge, it will only be provided value as the first argument.
  • Promises returned from a memoized function will be cached internally and take priority over cache. The promise cache does not persist outside of the current instance and properties assigned to a returned promise will not be kept.
  • cache will be provided the values of resolved promises instead of the promises themselves.

Signed-off-by: Richie Bendall <[email protected]>
@sindresorhus sindresorhus changed the title Allow .get() and .has() to return a promise Allow cache to be async Oct 13, 2021
@sindresorhus sindresorhus merged commit 19b418e into sindresorhus:main Oct 13, 2021
@sindresorhus
Copy link
Owner

@Richienb Thanks for working on this. This is ready to be released right? There are no more planned breaking changes?

@Richienb
Copy link
Contributor Author

@sindresorhus Nothing else from me - it's good to go.

@JaneJeon
Copy link

Hallelujah! Thanks for working on this @Richienb

Comment on lines +11 to +15
has: (key: KeyType) => Promise<boolean> | boolean;
get: (key: KeyType) => Promise<ValueType | undefined> | ValueType | undefined;
set: (key: KeyType, value: ValueType) => void;
delete: (key: KeyType) => void;
clear?: () => void;
Copy link

Choose a reason for hiding this comment

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

Seems unreasonable to expect an async cache to have async has and get but synchronous set, delete and clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was made with the intention that set, delete and clear could return promises but we don't care about waiting for them. We could add support for asynchronous functions as a mostly non-breaking change however.

See #3 (comment)

has: (key: KeyType) => Promise<boolean> | boolean;
get: (key: KeyType) => Promise<ValueType | undefined> | ValueType | undefined;
set: (key: KeyType, value: ValueType) => void;
delete: (key: KeyType) => void;
Copy link

Choose a reason for hiding this comment

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

cache.delete is never called. It should either be used or not required by this interface.

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.

Feature request: Return stale value while revalidating How to get it working TypeScript? Back with Keyv
5 participants