feat: cost optimize, increase cache

This commit is contained in:
anuraghazra 2024-10-11 23:07:05 +05:30
parent 37bf205914
commit cdc6ad1eaf
6 changed files with 25 additions and 48 deletions

View File

@ -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(

View File

@ -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

View File

@ -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(

View File

@ -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(

View File

@ -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

View File

@ -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,
};