refactor: move cache seconds calculation logic into reusable function (#4532)

* refactor: move cache seconds calculation logic into reusable function

* dev

---------

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
Alexandr Garbuzov
2025-10-06 23:32:47 +03:00
committed by GitHub
co-authored by Alexandr
parent fde13639aa
commit ade7d53653
6 changed files with 60 additions and 60 deletions
+8 -15
View File
@@ -1,15 +1,11 @@
// @ts-check
import {
clampValue,
CONSTANTS,
renderError,
parseBoolean,
} from "../src/common/utils.js";
import { CONSTANTS, renderError, parseBoolean } from "../src/common/utils.js";
import { gistWhitelist } from "../src/common/envs.js";
import { isLocaleAvailable } from "../src/translations.js";
import { renderGistCard } from "../src/cards/gist.js";
import { fetchGist } from "../src/fetchers/gist.js";
import { resolveCacheSeconds } from "../src/common/cache.js";
export default async (req, res) => {
const {
@@ -60,15 +56,12 @@ export default async (req, res) => {
try {
const gistData = await fetchGist(id);
let cacheSeconds = clampValue(
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;
const cacheSeconds = resolveCacheSeconds({
requested: cache_seconds,
def: CONSTANTS.TWO_DAY,
min: CONSTANTS.TWO_DAY,
max: CONSTANTS.SIX_DAY,
});
res.setHeader(
"Cache-Control",
+7 -10
View File
@@ -2,9 +2,9 @@
import { renderStatsCard } from "../src/cards/stats.js";
import { blacklist } from "../src/common/blacklist.js";
import { resolveCacheSeconds } from "../src/common/cache.js";
import { whitelist } from "../src/common/envs.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
@@ -103,15 +103,12 @@ export default async (req, res) => {
showStats.includes("discussions_answered"),
parseInt(commits_year, 10),
);
let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.TWELVE_HOURS,
CONSTANTS.TWO_DAY,
);
cacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
: cacheSeconds;
const cacheSeconds = resolveCacheSeconds({
requested: cache_seconds,
def: CONSTANTS.CARD_CACHE_SECONDS,
min: CONSTANTS.TWELVE_HOURS,
max: CONSTANTS.TWO_DAY,
});
res.setHeader(
"Cache-Control",
+8 -15
View File
@@ -2,13 +2,9 @@
import { renderRepoCard } from "../src/cards/repo.js";
import { blacklist } from "../src/common/blacklist.js";
import { resolveCacheSeconds } from "../src/common/cache.js";
import { whitelist } from "../src/common/envs.js";
import {
clampValue,
CONSTANTS,
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { CONSTANTS, parseBoolean, renderError } from "../src/common/utils.js";
import { fetchRepo } from "../src/fetchers/repo.js";
import { isLocaleAvailable } from "../src/translations.js";
@@ -80,15 +76,12 @@ export default async (req, res) => {
try {
const repoData = await fetchRepo(username, repo);
let cacheSeconds = clampValue(
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;
const cacheSeconds = resolveCacheSeconds({
requested: cache_seconds,
def: CONSTANTS.PIN_CARD_CACHE_SECONDS,
min: CONSTANTS.ONE_DAY,
max: CONSTANTS.TEN_DAY,
});
res.setHeader(
"Cache-Control",
+7 -10
View File
@@ -2,9 +2,9 @@
import { renderTopLanguages } from "../src/cards/top-languages.js";
import { blacklist } from "../src/common/blacklist.js";
import { resolveCacheSeconds } from "../src/common/cache.js";
import { whitelist } from "../src/common/envs.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
@@ -105,15 +105,12 @@ export default async (req, res) => {
size_weight,
count_weight,
);
let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.TOP_LANGS_CACHE_SECONDS, 10),
CONSTANTS.TWO_DAY,
CONSTANTS.TEN_DAY,
);
cacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
: cacheSeconds;
const cacheSeconds = resolveCacheSeconds({
requested: cache_seconds,
def: CONSTANTS.TOP_LANGS_CACHE_SECONDS,
min: CONSTANTS.TWO_DAY,
max: CONSTANTS.TEN_DAY,
});
res.setHeader(
"Cache-Control",
+7 -10
View File
@@ -2,7 +2,6 @@
import { renderWakatimeCard } from "../src/cards/wakatime.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
@@ -11,6 +10,7 @@ import {
import { whitelist } from "../src/common/envs.js";
import { fetchWakatimeStats } from "../src/fetchers/wakatime.js";
import { isLocaleAvailable } from "../src/translations.js";
import { resolveCacheSeconds } from "../src/common/cache.js";
export default async (req, res) => {
const {
@@ -71,15 +71,12 @@ export default async (req, res) => {
try {
const stats = await fetchWakatimeStats({ username, api_domain });
let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.SIX_HOURS,
CONSTANTS.TWO_DAY,
);
cacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
: cacheSeconds;
const cacheSeconds = resolveCacheSeconds({
requested: cache_seconds,
def: CONSTANTS.CARD_CACHE_SECONDS,
min: CONSTANTS.SIX_HOURS,
max: CONSTANTS.TWO_DAY,
});
res.setHeader(
"Cache-Control",
+23
View File
@@ -0,0 +1,23 @@
import { clampValue } from "./utils";
/**
* Resolves the cache seconds based on the requested, default, min, and max values.
*
* @param {Object} args The parameters object.
* @param {number} args.requested The requested cache seconds.
* @param {number} args.def The default cache seconds.
* @param {number} args.min The minimum cache seconds.
* @param {number} args.max The maximum cache seconds.
* @returns {number} The resolved cache seconds.
*/
const resolveCacheSeconds = ({ requested, def, min, max }) => {
let cacheSeconds = clampValue(parseInt(requested || def, 10), min, max);
cacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
: cacheSeconds;
return cacheSeconds;
};
export { resolveCacheSeconds };