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

Add cache to function response #22

Merged
merged 4 commits into from
Jul 15, 2020
Merged

Add cache to function response #22

merged 4 commits into from
Jul 15, 2020

Conversation

nombrekeff
Copy link
Contributor

I tried adding a cache (1h, but can be changed) to help with the commit count problem. It reduced Anshuman Verma's request from 4.5s avg to 185s avg in subsequent requests. The first request took a bit longer.
This approach has a problem, though. The stats will only refresh once the cache max-age has expired, I guess it's not that big of a deal, shield.io and similar tend to have cache in place.

I tried adding a cache (1h, but can be changed) to help with the commit count problem. It reduced Anshuman Verma's request from 4.5s avg to 185s avg in subsequent requests. The first request took a bit longer. 
This approach has a problem, though. The stats will only refresh once the cache max-age has expired, I guess it's not that big of a deal, shield.io and similar tend to have cache in place.
@anuraghazra anuraghazra linked an issue Jul 11, 2020 that may be closed by this pull request
@fransallen
Copy link

This one is legit!

Maybe consider using public, max-age= instead of s-maxage-only for general cache support.

api/index.js Outdated
@@ -189,6 +189,7 @@ module.exports = async (req, res) => {

const stats = await fetchStats(username);

res.setHeader('Cache-Control', 's-maxage=3600');
Copy link
Owner

Choose a reason for hiding this comment

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

I think 5min cache would be good enough since we need the frequent updates too

res.setHeader("Cache-Control", "public, max-age=300");

Choose a reason for hiding this comment

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

Yep, similar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

5 minutes would be good I guess, I made it one hour just to test it.

Choose a reason for hiding this comment

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

Ah! magic number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, good catch, I can move it into a constant or something.

const FIVE_MINUTES = 300;
res.setHeader('Cache-Control', `public, max-age=${FIVE_MINUTES}`);

Copy link

@dikshantpatodia dikshantpatodia Jul 16, 2020

Choose a reason for hiding this comment

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

Create a constants file under src, add all the constants there -> export -> tada

For the const, instead of FIVE_MINUTES we can name it like MAX_AGE_LIMIT_IN_MS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, I commented that to Anurag, but he said he wanted to add that to env so it can easily be changed.

Choose a reason for hiding this comment

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

Awesome

@anuraghazra anuraghazra removed a link to an issue Jul 14, 2020
@nombrekeff
Copy link
Contributor Author

I lowered it to 5 minutes. Is there any other place it should be added? I guess it's only needed because of the commit count, but might be worth adding to the pin request. What you think?

@nombrekeff
Copy link
Contributor Author

nombrekeff commented Jul 14, 2020

This one is legit!

Maybe consider using public, max-age= instead of s-maxage-only for general cache support.

@fransallen What is the difference between the 2? I have not read much about it.

I can change it to public, max-age= if necessary

@anuraghazra
Copy link
Owner

@nombrekeff I think you can also add cache headers to pin.js file.

Also let's use public, max-age since I checked it works. I did not checked the other one

@nombrekeff
Copy link
Contributor Author

Sure, I've been reading about it, and public, max-age seems a better approach. I will change it and add it to pin.js as well.

@nombrekeff
Copy link
Contributor Author

Changed and added it to pin.js as well

@anuraghazra
Copy link
Owner

@nombrekeff LGTM! thanks for the PR ❤️

@anuraghazra anuraghazra merged commit 3e9b0a0 into anuraghazra:commit-counts Jul 15, 2020
@anuraghazra
Copy link
Owner

Opps looks like the PR got merged with the commit-counts branch, no issues, i think its good i can work on that branch and merge it directly.

@nombrekeff
Copy link
Contributor Author

Great! Hope it helps 😄

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.

4 participants