Skip to content

Commit 0afa6c4

Browse files
opheliagoldsteinqwerty541Copilot
authored
refactor: reorganize cache TTL constants (anuraghazra#4550)
* refactor: reorganize cache TTL constants * Update tests/wakatime.test.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Alexandr <qwerty541zxc@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3c22841 commit 0afa6c4

13 files changed

Lines changed: 158 additions & 103 deletions

File tree

api/gist.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// @ts-check
22

3-
import { CONSTANTS, renderError, parseBoolean } from "../src/common/utils.js";
3+
import { renderError, parseBoolean } from "../src/common/utils.js";
44
import { isLocaleAvailable } from "../src/translations.js";
55
import { renderGistCard } from "../src/cards/gist.js";
66
import { fetchGist } from "../src/fetchers/gist.js";
77
import {
8+
CACHE_TTL,
89
resolveCacheSeconds,
910
setCacheHeaders,
1011
setErrorCacheHeaders,
@@ -65,9 +66,9 @@ export default async (req, res) => {
6566
const gistData = await fetchGist(id);
6667
const cacheSeconds = resolveCacheSeconds({
6768
requested: parseInt(cache_seconds, 10),
68-
def: CONSTANTS.TWO_DAY,
69-
min: CONSTANTS.TWO_DAY,
70-
max: CONSTANTS.SIX_DAY,
69+
def: CACHE_TTL.GIST_CARD.DEFAULT,
70+
min: CACHE_TTL.GIST_CARD.MIN,
71+
max: CACHE_TTL.GIST_CARD.MAX,
7172
});
7273

7374
setCacheHeaders(res, cacheSeconds);

api/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import { renderStatsCard } from "../src/cards/stats.js";
44
import { guardAccess } from "../src/common/access.js";
55
import {
6+
CACHE_TTL,
67
resolveCacheSeconds,
78
setCacheHeaders,
89
setErrorCacheHeaders,
910
} from "../src/common/cache.js";
10-
import {
11-
CONSTANTS,
12-
parseArray,
13-
parseBoolean,
14-
renderError,
15-
} from "../src/common/utils.js";
11+
import { parseArray, parseBoolean, renderError } from "../src/common/utils.js";
1612
import { fetchStats } from "../src/fetchers/stats.js";
1713
import { isLocaleAvailable } from "../src/translations.js";
1814

@@ -94,9 +90,9 @@ export default async (req, res) => {
9490
);
9591
const cacheSeconds = resolveCacheSeconds({
9692
requested: parseInt(cache_seconds, 10),
97-
def: CONSTANTS.CARD_CACHE_SECONDS,
98-
min: CONSTANTS.TWELVE_HOURS,
99-
max: CONSTANTS.TWO_DAY,
93+
def: CACHE_TTL.STATS_CARD.DEFAULT,
94+
min: CACHE_TTL.STATS_CARD.MIN,
95+
max: CACHE_TTL.STATS_CARD.MAX,
10096
});
10197

10298
setCacheHeaders(res, cacheSeconds);

api/pin.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import { renderRepoCard } from "../src/cards/repo.js";
44
import { guardAccess } from "../src/common/access.js";
55
import {
6+
CACHE_TTL,
67
resolveCacheSeconds,
78
setCacheHeaders,
89
setErrorCacheHeaders,
910
} from "../src/common/cache.js";
10-
import { CONSTANTS, parseBoolean, renderError } from "../src/common/utils.js";
11+
import { parseBoolean, renderError } from "../src/common/utils.js";
1112
import { fetchRepo } from "../src/fetchers/repo.js";
1213
import { isLocaleAvailable } from "../src/translations.js";
1314

@@ -67,9 +68,9 @@ export default async (req, res) => {
6768
const repoData = await fetchRepo(username, repo);
6869
const cacheSeconds = resolveCacheSeconds({
6970
requested: parseInt(cache_seconds, 10),
70-
def: CONSTANTS.PIN_CARD_CACHE_SECONDS,
71-
min: CONSTANTS.ONE_DAY,
72-
max: CONSTANTS.TEN_DAY,
71+
def: CACHE_TTL.PIN_CARD.DEFAULT,
72+
min: CACHE_TTL.PIN_CARD.MIN,
73+
max: CACHE_TTL.PIN_CARD.MAX,
7374
});
7475

7576
setCacheHeaders(res, cacheSeconds);

api/top-langs.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import { renderTopLanguages } from "../src/cards/top-languages.js";
44
import { guardAccess } from "../src/common/access.js";
55
import {
6+
CACHE_TTL,
67
resolveCacheSeconds,
78
setCacheHeaders,
89
setErrorCacheHeaders,
910
} from "../src/common/cache.js";
10-
import {
11-
CONSTANTS,
12-
parseArray,
13-
parseBoolean,
14-
renderError,
15-
} from "../src/common/utils.js";
11+
import { parseArray, parseBoolean, renderError } from "../src/common/utils.js";
1612
import { fetchTopLanguages } from "../src/fetchers/top-languages.js";
1713
import { isLocaleAvailable } from "../src/translations.js";
1814

@@ -124,9 +120,9 @@ export default async (req, res) => {
124120
);
125121
const cacheSeconds = resolveCacheSeconds({
126122
requested: parseInt(cache_seconds, 10),
127-
def: CONSTANTS.TOP_LANGS_CACHE_SECONDS,
128-
min: CONSTANTS.TWO_DAY,
129-
max: CONSTANTS.TEN_DAY,
123+
def: CACHE_TTL.TOP_LANGS_CARD.DEFAULT,
124+
min: CACHE_TTL.TOP_LANGS_CARD.MIN,
125+
max: CACHE_TTL.TOP_LANGS_CARD.MAX,
130126
});
131127

132128
setCacheHeaders(res, cacheSeconds);

api/wakatime.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// @ts-check
22

33
import { renderWakatimeCard } from "../src/cards/wakatime.js";
4-
import {
5-
CONSTANTS,
6-
parseArray,
7-
parseBoolean,
8-
renderError,
9-
} from "../src/common/utils.js";
4+
import { parseArray, parseBoolean, renderError } from "../src/common/utils.js";
105
import { fetchWakatimeStats } from "../src/fetchers/wakatime.js";
116
import { isLocaleAvailable } from "../src/translations.js";
127
import {
8+
CACHE_TTL,
139
resolveCacheSeconds,
1410
setCacheHeaders,
1511
setErrorCacheHeaders,
@@ -80,9 +76,9 @@ export default async (req, res) => {
8076
const stats = await fetchWakatimeStats({ username, api_domain });
8177
const cacheSeconds = resolveCacheSeconds({
8278
requested: parseInt(cache_seconds, 10),
83-
def: CONSTANTS.CARD_CACHE_SECONDS,
84-
min: CONSTANTS.SIX_HOURS,
85-
max: CONSTANTS.TWO_DAY,
79+
def: CACHE_TTL.WAKATIME_CARD.DEFAULT,
80+
min: CACHE_TTL.WAKATIME_CARD.MIN,
81+
max: CACHE_TTL.WAKATIME_CARD.MAX,
8682
});
8783

8884
setCacheHeaders(res, cacheSeconds);

src/common/cache.js

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,64 @@
11
// @ts-check
22

3-
import { clampValue, CONSTANTS } from "./utils.js";
3+
import { clampValue } from "./utils.js";
4+
5+
const MIN = 60;
6+
const HOUR = 60 * MIN;
7+
const DAY = 24 * HOUR;
8+
9+
/**
10+
* Common durations in seconds.
11+
*/
12+
const DURATIONS = {
13+
ONE_MINUTE: MIN,
14+
FIVE_MINUTES: 5 * MIN,
15+
TEN_MINUTES: 10 * MIN,
16+
FIFTEEN_MINUTES: 15 * MIN,
17+
THIRTY_MINUTES: 30 * MIN,
18+
19+
TWO_HOURS: 2 * HOUR,
20+
FOUR_HOURS: 4 * HOUR,
21+
SIX_HOURS: 6 * HOUR,
22+
EIGHT_HOURS: 8 * HOUR,
23+
TWELVE_HOURS: 12 * HOUR,
24+
25+
ONE_DAY: DAY,
26+
TWO_DAY: 2 * DAY,
27+
SIX_DAY: 6 * DAY,
28+
TEN_DAY: 10 * DAY,
29+
};
30+
31+
/**
32+
* Common cache TTL values in seconds.
33+
*/
34+
const CACHE_TTL = {
35+
STATS_CARD: {
36+
DEFAULT: DURATIONS.ONE_DAY,
37+
MIN: DURATIONS.TWELVE_HOURS,
38+
MAX: DURATIONS.TWO_DAY,
39+
},
40+
TOP_LANGS_CARD: {
41+
DEFAULT: DURATIONS.SIX_DAY,
42+
MIN: DURATIONS.TWO_DAY,
43+
MAX: DURATIONS.TEN_DAY,
44+
},
45+
PIN_CARD: {
46+
DEFAULT: DURATIONS.TEN_DAY,
47+
MIN: DURATIONS.ONE_DAY,
48+
MAX: DURATIONS.TEN_DAY,
49+
},
50+
GIST_CARD: {
51+
DEFAULT: DURATIONS.TWO_DAY,
52+
MIN: DURATIONS.ONE_DAY,
53+
MAX: DURATIONS.TEN_DAY,
54+
},
55+
WAKATIME_CARD: {
56+
DEFAULT: DURATIONS.ONE_DAY,
57+
MIN: DURATIONS.TWELVE_HOURS,
58+
MAX: DURATIONS.TWO_DAY,
59+
},
60+
ERROR: DURATIONS.TEN_MINUTES,
61+
};
462

563
/**
664
* Resolves the cache seconds based on the requested, default, min, and max values.
@@ -56,7 +114,7 @@ const setCacheHeaders = (res, cacheSeconds) => {
56114
"Cache-Control",
57115
`max-age=${cacheSeconds}, ` +
58116
`s-maxage=${cacheSeconds}, ` +
59-
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
117+
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
60118
);
61119
};
62120

@@ -80,10 +138,16 @@ const setErrorCacheHeaders = (res) => {
80138
// Use lower cache period for errors.
81139
res.setHeader(
82140
"Cache-Control",
83-
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
84-
`s-maxage=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
85-
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
141+
`max-age=${CACHE_TTL.ERROR}, ` +
142+
`s-maxage=${CACHE_TTL.ERROR}, ` +
143+
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
86144
);
87145
};
88146

89-
export { resolveCacheSeconds, setCacheHeaders, setErrorCacheHeaders };
147+
export {
148+
resolveCacheSeconds,
149+
setCacheHeaders,
150+
setErrorCacheHeaders,
151+
DURATIONS,
152+
CACHE_TTL,
153+
};

src/common/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
export { blacklist } from "./blacklist.js";
24
export { Card } from "./Card.js";
35
export { createProgressNode } from "./createProgressNode.js";
@@ -20,7 +22,6 @@ export {
2022
getCardColors,
2123
wrapTextMultiline,
2224
logger,
23-
CONSTANTS,
2425
CustomError,
2526
MissingParamError,
2627
measureText,

src/common/utils.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -452,34 +452,6 @@ const noop = () => {};
452452
const logger =
453453
process.env.NODE_ENV === "test" ? { log: noop, error: noop } : console;
454454

455-
const MIN = 60;
456-
const HOUR = 60 * MIN;
457-
const DAY = 24 * HOUR;
458-
459-
const CONSTANTS = {
460-
ONE_MINUTE: MIN,
461-
FIVE_MINUTES: 5 * MIN,
462-
TEN_MINUTES: 10 * MIN,
463-
FIFTEEN_MINUTES: 15 * MIN,
464-
THIRTY_MINUTES: 30 * MIN,
465-
466-
TWO_HOURS: 2 * HOUR,
467-
FOUR_HOURS: 4 * HOUR,
468-
SIX_HOURS: 6 * HOUR,
469-
EIGHT_HOURS: 8 * HOUR,
470-
TWELVE_HOURS: 12 * HOUR,
471-
472-
ONE_DAY: DAY,
473-
TWO_DAY: 2 * DAY,
474-
SIX_DAY: 6 * DAY,
475-
TEN_DAY: 10 * DAY,
476-
477-
CARD_CACHE_SECONDS: DAY,
478-
TOP_LANGS_CACHE_SECONDS: 6 * DAY,
479-
PIN_CARD_CACHE_SECONDS: 10 * DAY,
480-
ERROR_CACHE_SECONDS: 10 * MIN,
481-
};
482-
483455
/**
484456
* Missing query parameter class.
485457
*/
@@ -648,7 +620,6 @@ export {
648620
getCardColors,
649621
wrapTextMultiline,
650622
logger,
651-
CONSTANTS,
652623
CustomError,
653624
MissingParamError,
654625
measureText,

0 commit comments

Comments
 (0)