-
Notifications
You must be signed in to change notification settings - Fork 12
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(javascript): add cache layer #274
Conversation
✅ Deploy Preview for api-clients-automation canceled.
|
✗ The generated branch has been deleted. If the PR has been merged, you can check the generated code on the |
@@ -43,6 +44,9 @@ export function {{apiName}}Api( | |||
}, | |||
requester: options?.requester ?? createHttpRequester(), | |||
userAgents: [{ segment: 'Node.js', version: process.versions.node }], | |||
responsesCache: createNullCache(), | |||
requestsCache: createNullCache(), | |||
hostsCache: createMemoryCache(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remind me what hostsCache
is?
Too many types of cache, and I'm quite puzzled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It contains the hosts that responded with timeout
or down
status: https://github.com/algolia/api-clients-automation/blob/main/clients/algoliasearch-client-javascript/packages/client-common/src/createTransporter.ts#L177-L185
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will store whether a algolia host is down or not, to stop calling it if it's down, and it's used to randomize which host is used also
@@ -39,6 +47,14 @@ export function algoliasearch( | |||
requester: options?.requester ?? createXhrRequester(), | |||
userAgents: [{ segment: 'Browser' }], | |||
authMode: 'WithinQueryParameters', | |||
responsesCache: createMemoryCache(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if memory cache should be the default value here. Probably null cache for all (response, request, host for both node and browser). What do you think?
What do we already have for the v4 js client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same here as the v4 js client, which seems to work well but I don't know much about the limitations here
|
||
namespace[JSON.stringify(key)] = value; | ||
|
||
getStorage().setItem(namespaceKey, JSON.stringify(namespace)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried about what happens if the local storage is full. Of course, it's not about this pull-request. I'm just throwing a topic that we should think about in a separate pull-request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I skimmed through the actual cache code because it's well tested.
However you are not using the cache in the transporter, is it on purpose ?
clients/algoliasearch-client-javascript/packages/client-common/jest.config.ts
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,49 @@ | |||
import { createNullCache } from '../createNullCache'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it makes more sense to have src
and tests
folders at the root, instead of src/__tests__/
, but we need to define it across the entire repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep make sense, we could change all of them together once defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer to have test files as close as their source files. It's too troublesome to import like
import { something } from '../../../../tests/some/nested/folder'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could even be https://twitter.com/antfu7/status/1505299814069977088 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shortcuts haha yeah I saw that, and thought interesting!
@@ -45,6 +46,14 @@ export function {{apiName}}Api( | |||
requester: options?.requester ?? createXhrRequester(), | |||
userAgents: [{ segment: 'Browser' }], | |||
authMode: 'WithinQueryParameters', | |||
responsesCache: createMemoryCache(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache should be configurable through the options
param IMO, for both node
and browser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, and I think I've recently saw some people using their own it so it definitely make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this change for the review ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently exposing the requestOptions
to each methods of the clients so I'll introduce it with it in a later PR!
It introduced a lot of changes so I'm writing the stacked PR atm, I shouldn't have put the PR ready for review yet 😓 |
Ah ok don't worry ! |
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
roots: ['src/__tests__/cache'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roots
would make more sense with src/__tests__
right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we only have cache
tests for now I've just left it like that, but indeed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix here boyz df85f88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
algolia/api-clients-automation#274 Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com>
🧭 What and Why
🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-176
Changes included:
client-common
folderclient-common/src/__tests__
folderNext step:
Run the
client-common
tests along with theCTS
. #279Diagram for fun
Implement
requests
andresponses
cache in thetransporter
#281🧪 Test