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(javascript): add cache layer #274

Merged
merged 7 commits into from
Mar 23, 2022
Merged

Conversation

shortcuts
Copy link
Member

@shortcuts shortcuts commented Mar 22, 2022

🧭 What and Why

🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-176

Changes included:

  • Copy the cache logic from the current client to our client-common folder
    • Only type changes have been done
  • Copy existing tests to our client-common/src/__tests__ folder
    • Mostly some method and type changes
    • As it's not part of the CTS, I thought it was a good addition. I'll make those run along with the CTS in a later PR.
  • Update templates to reflect the cache usage
  • Bump package size to their closest size limit

Next step:

Run the client-common tests along with the CTS. #279

Diagram for fun

flowchart LR
A[Previous jobs..] --> B[Clients jobs] --> C{Tests}
C -->D[CTS]
C -->E[client-common tests]
D --> F[Codegen]
E --> F[Codegen]
Loading

Implement requests and responses cache in the transporter #281

🧪 Test

  • CI :D
  • Generated branch

@netlify
Copy link

netlify bot commented Mar 22, 2022

Deploy Preview for api-clients-automation canceled.

Name Link
🔨 Latest commit e8a1a69
🔍 Latest deploy log https://app.netlify.com/sites/api-clients-automation/deploys/623b2e3392b596000861ff13

@shortcuts shortcuts marked this pull request as draft March 22, 2022 14:29
@shortcuts
Copy link
Member Author

shortcuts commented Mar 22, 2022

✗ The generated branch has been deleted.

If the PR has been merged, you can check the generated code on the generated/main branch.

@shortcuts shortcuts self-assigned this Mar 22, 2022
@shortcuts shortcuts requested review from a team, eunjae-lee and damcou and removed request for a team March 22, 2022 15:28
@shortcuts shortcuts marked this pull request as ready for review March 22, 2022 15:28
@@ -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(),
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

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(),
Copy link
Contributor

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?

Copy link
Member Author

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));
Copy link
Contributor

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.

Copy link
Collaborator

@millotp millotp left a 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 ?

@@ -0,0 +1,49 @@
import { createNullCache } from '../createNullCache';
Copy link
Collaborator

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

Copy link
Member Author

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

Copy link
Contributor

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'

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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(),
Copy link
Collaborator

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

Copy link
Member Author

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

Copy link
Collaborator

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 ?

Copy link
Member Author

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!

@shortcuts
Copy link
Member Author

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 ?

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 😓

@millotp
Copy link
Collaborator

millotp commented Mar 22, 2022

Ah ok don't worry !

preset: 'ts-jest',
testEnvironment: 'jsdom',
roots: ['src/__tests__/cache'],
Copy link
Collaborator

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 ?

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

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

fix here boyz df85f88

@shortcuts shortcuts requested a review from millotp March 23, 2022 14:25
millotp
millotp previously approved these changes Mar 23, 2022
Copy link
Collaborator

@millotp millotp left a comment

Choose a reason for hiding this comment

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

💯

@shortcuts shortcuts enabled auto-merge (squash) March 23, 2022 14:26
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.

None yet

3 participants