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
Ahn nath/cache option apis #373
Conversation
@ahn-nath Make sure to also look into the conflicts in the draft PR once you return. :) |
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 a feature like this, it would be good to add some tests. You can mock the interceptor if needed to make sure that the cache contents change, and then that the API call is not made on a subsequent request after the first one is cached
@thyeggman Hey Jacob! I think #296 should be completed before we start writing the tests for this update. Maybe we can also have optional tests that use the actual Cicero API instead of the mocked version that will be made in #296. |
Then, if I understand correctly, this PR can be approved once the other
issue is closed, correct?
El vie., 28 de octubre de 2022 10:49 p. m., Param Siddharth <
***@***.***> escribió:
… @thyeggman <https://github.com/thyeggman> Hey Jacob! I think #296
<#296> should be completed
before we start writing the tests for this update. Maybe we can also have
optional tests that use the actual Cicero API instead of the mocked version
that will be made in #296
<#296>.
—
Reply to this email directly, view it on GitHub
<#373 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQBB52UEKBCLK4REWYK4P53WFSGELANCNFSM6AAAAAARPRYJEE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@ahn-nath Yes. However, there are some changes requested. You'll need to make them before the PR is ready to merge. :) But you can look at it once you are back from the conference. |
Making the calculation and the variables more self-descriptive (1000 - unit of milliseconds, 60 - unit for seconds, 60 - unit for minutes, 24 - unit for number of hours in a day, 7 - unit for number of days in a week). Co-authored-by: Jacob Wallraff <thyeggman@github.com>
…amEquity/amplify into ahn-nath/cache-option-apis
… a timeout property
As request by my mentor, Jacob, I have added some tests to guarantee that the library works as expected. 3 tests were added. Description 1. we test that the configuration we set is valid and available to check
2. we test that the cache is working as expected with concurrent requests
3. we test that the cache is invalidated when the ttl expires
Looking forward to your thoughts and review @thyeggman, @paramsiddharth. |
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.
@ahn-nath I added a comment. Please check it out. :) Thank you.
Checked 👍 |
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.
@ahn-nath Looks good to me, Nath! :) Good work!
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 tests you wrote are good test cases to have, although there are come basic cases that would be good to test as well, such as a simple test to make a single request, verify that it is not cached, and then repeat the request and make sure it is cached.
I left a few comments on the behavior of Promise.all
as well
@thyeggman |
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 feel that a simple test to check for a single request is unnecessary and repetitive
You're right, it does become redundant if you change the third test as I suggested. This looks good to me now, thanks for making the changes!
Closes #294
We have explored a caching option with the library axios-cache-interceptor to “cache API calls to reduce the number of credits we use”. For this caching option we need to see the lifetime of a cached response, a way to persist the day permanently and across different user sessions and, keep in mind, the margin to consider for cutting expenses.
Description of relevant aspects:
Set options to invalidate the cache and the lifetime of a cached response
To define a limit or deadline for a cached response to be valid and updated, we make use of the property "ttl", as part of the “per request configuration”. We set it to the reasonable time limit of 1 week in milliseconds: 604800000.
Source: https://axios-cache-interceptor.js.org/#/pages/per-request-configuration?id=cachettl
Add persistent storage
The lifetime of a request will only make sense and work if we use persistent storage. There are several options they provide, such as:
I am not considering using a database. Instead, I think we can use documents that are stored in the file system for persistence or variables that are initialized once during the server lifetime. I am using a Map structure that is working with the
buildStorage
function of the library.Considerations:
If the app/amplify project uses several servers to work with the representatives' data, libraries that help you persist the data elsewhere may be a better option, such as Node Redis. Now, this simpler solution can prevent the complexity such a library would add and provide a working option that fits our case. I am open to your comments and suggestions if my judgment is incorrect.
I discussed with my mentors that in some cases, when important data changes, we may need to update the website data as soon as possible, and not wait until the scheduled time to refresh the API comes. Such a case is when we need to send a letter and require a valid address. In those cases, as suggested by my mentors @thyeggman and @paramsiddharth, an alternative would be to make a real API call and update the caching storage as per demand, or when the user triggers the function responsible for sending the letter.
Results and the effect of this implementation
On average, we could assume that I make 9 calls per day, with a total of 63 calls per week. If we are only making real API calls (updating the cache) once a week, that is, only making one call to the API per week, we are using, roughly, 1.58% of the current average. For that case, we would be cutting 98.5% of our weekly use.