From cdc6ad1eaf1b12d08e5c7ed4a7e76fa51326eb61 Mon Sep 17 00:00:00 2001 From: anuraghazra Date: Fri, 11 Oct 2024 23:07:05 +0530 Subject: [PATCH] feat: cost optimize, increase cache --- api/gist.js | 23 ++++------------------- api/index.js | 4 ++-- api/pin.js | 21 +++------------------ api/top-langs.js | 11 ++++------- api/wakatime.js | 2 +- src/common/utils.js | 12 +++++++++++- 6 files changed, 25 insertions(+), 48 deletions(-) diff --git a/api/gist.js b/api/gist.js index 8821c7b094b9e..5b03743ce9e6e 100644 --- a/api/gist.js +++ b/api/gist.js @@ -42,32 +42,17 @@ export default async (req, res) => { const gistData = await fetchGist(id); let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.SIX_HOURS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, + parseInt(cache_seconds || CONSTANTS.TWO_DAY, 10), + CONSTANTS.TWO_DAY, + CONSTANTS.SIX_DAY, ); cacheSeconds = process.env.CACHE_SECONDS ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds : cacheSeconds; - /* - if star count & fork count is over 1k then we are kFormating the text - and if both are zero we are not showing the stats - so we can just make the cache longer, since there is no need to frequent updates - */ - const stars = gistData.starsCount; - const forks = gistData.forksCount; - const isBothOver1K = stars > 1000 && forks > 1000; - const isBothUnder1 = stars < 1 && forks < 1; - if (!cache_seconds && (isBothOver1K || isBothUnder1)) { - cacheSeconds = CONSTANTS.SIX_HOURS; - } - res.setHeader( "Cache-Control", - `max-age=${ - cacheSeconds / 2 - }, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + `max-age=${cacheSeconds}, s-maxage=${cacheSeconds}`, ); return res.send( diff --git a/api/index.js b/api/index.js index 2029367ca3eb9..9fde2d7ea9a7c 100644 --- a/api/index.js +++ b/api/index.js @@ -79,8 +79,8 @@ export default async (req, res) => { let cacheSeconds = clampValue( parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, + CONSTANTS.TWELVE_HOURS, + CONSTANTS.TWO_DAY, ); cacheSeconds = process.env.CACHE_SECONDS ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds diff --git a/api/pin.js b/api/pin.js index 0bc029d7ffda3..bede7d87f5972 100644 --- a/api/pin.js +++ b/api/pin.js @@ -57,32 +57,17 @@ export default async (req, res) => { const repoData = await fetchRepo(username, repo); let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, + parseInt(cache_seconds || CONSTANTS.PIN_CARD_CACHE_SECONDS, 10), CONSTANTS.ONE_DAY, + CONSTANTS.TEN_DAY, ); cacheSeconds = process.env.CACHE_SECONDS ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds : cacheSeconds; - /* - if star count & fork count is over 1k then we are kFormating the text - and if both are zero we are not showing the stats - so we can just make the cache longer, since there is no need to frequent updates - */ - const stars = repoData.starCount; - const forks = repoData.forkCount; - const isBothOver1K = stars > 1000 && forks > 1000; - const isBothUnder1 = stars < 1 && forks < 1; - if (!cache_seconds && (isBothOver1K || isBothUnder1)) { - cacheSeconds = CONSTANTS.SIX_HOURS; - } - res.setHeader( "Cache-Control", - `max-age=${ - cacheSeconds / 2 - }, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + `max-age=${cacheSeconds}, s-maxage=${cacheSeconds}`, ); return res.send( diff --git a/api/top-langs.js b/api/top-langs.js index 382ee4205a87e..d1815559f372f 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -70,10 +70,9 @@ export default async (req, res) => { count_weight, ); - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, + let cacheSeconds = parseInt( + cache_seconds || CONSTANTS.TOP_LANGS_CACHE_SECONDS, + 10, ); cacheSeconds = process.env.CACHE_SECONDS ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds @@ -81,9 +80,7 @@ export default async (req, res) => { res.setHeader( "Cache-Control", - `max-age=${ - cacheSeconds / 2 - }, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + `max-age=${cacheSeconds / 2}, s-maxage=${cacheSeconds}`, ); return res.send( diff --git a/api/wakatime.js b/api/wakatime.js index de263e0644c43..73ef9986feeb7 100644 --- a/api/wakatime.js +++ b/api/wakatime.js @@ -54,7 +54,7 @@ export default async (req, res) => { let cacheSeconds = clampValue( parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, + CONSTANTS.TWO_DAY, ); cacheSeconds = process.env.CACHE_SECONDS ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds diff --git a/src/common/utils.js b/src/common/utils.js index 48ea051783b7f..b780657c1c244 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -449,7 +449,11 @@ const TWO_HOURS = 7200; const FOUR_HOURS = 14400; const SIX_HOURS = 21600; const EIGHT_HOURS = 28800; +const TWELVE_HOURS = 43200; const ONE_DAY = 86400; +const TWO_DAY = ONE_DAY * 2; +const SIX_DAY = ONE_DAY * 6; +const TEN_DAY = ONE_DAY * 10; const CONSTANTS = { ONE_MINUTE, @@ -461,8 +465,14 @@ const CONSTANTS = { FOUR_HOURS, SIX_HOURS, EIGHT_HOURS, + TWELVE_HOURS, ONE_DAY, - CARD_CACHE_SECONDS: SIX_HOURS, + TWO_DAY, + SIX_DAY, + TEN_DAY, + CARD_CACHE_SECONDS: ONE_DAY, + TOP_LANGS_CACHE_SECONDS: SIX_DAY, + PIN_CARD_CACHE_SECONDS: TEN_DAY, ERROR_CACHE_SECONDS: TEN_MINUTES, };