Skip to content

Commit 2ee803f

Browse files
authored
feature: extend default card cache time to 6 hours (#3242)
* feature: extend default card cache time to 8 hours * reduce to six hours
1 parent eb787af commit 2ee803f

File tree

9 files changed

+45
-33
lines changed

9 files changed

+45
-33
lines changed

api/gist.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export default async (req, res) => {
3434
const gistData = await fetchGist(id);
3535

3636
let cacheSeconds = clampValue(
37-
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
38-
CONSTANTS.FOUR_HOURS,
37+
parseInt(cache_seconds || CONSTANTS.SIX_HOURS, 10),
38+
CONSTANTS.SIX_HOURS,
3939
CONSTANTS.ONE_DAY,
4040
);
4141
cacheSeconds = process.env.CACHE_SECONDS
@@ -52,7 +52,7 @@ export default async (req, res) => {
5252
const isBothOver1K = stars > 1000 && forks > 1000;
5353
const isBothUnder1 = stars < 1 && forks < 1;
5454
if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
55-
cacheSeconds = CONSTANTS.FOUR_HOURS;
55+
cacheSeconds = CONSTANTS.SIX_HOURS;
5656
}
5757

5858
res.setHeader(

api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default async (req, res) => {
5858

5959
let cacheSeconds = clampValue(
6060
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
61-
CONSTANTS.FOUR_HOURS,
61+
CONSTANTS.SIX_HOURS,
6262
CONSTANTS.ONE_DAY,
6363
);
6464
cacheSeconds = process.env.CACHE_SECONDS

api/pin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default async (req, res) => {
4141

4242
let cacheSeconds = clampValue(
4343
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
44-
CONSTANTS.FOUR_HOURS,
44+
CONSTANTS.SIX_HOURS,
4545
CONSTANTS.ONE_DAY,
4646
);
4747
cacheSeconds = process.env.CACHE_SECONDS
@@ -58,7 +58,7 @@ export default async (req, res) => {
5858
const isBothOver1K = stars > 1000 && forks > 1000;
5959
const isBothUnder1 = stars < 1 && forks < 1;
6060
if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
61-
cacheSeconds = CONSTANTS.FOUR_HOURS;
61+
cacheSeconds = CONSTANTS.SIX_HOURS;
6262
}
6363

6464
res.setHeader(

api/top-langs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default async (req, res) => {
6464

6565
let cacheSeconds = clampValue(
6666
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
67-
CONSTANTS.FOUR_HOURS,
67+
CONSTANTS.SIX_HOURS,
6868
CONSTANTS.ONE_DAY,
6969
);
7070
cacheSeconds = process.env.CACHE_SECONDS

api/wakatime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default async (req, res) => {
4343

4444
let cacheSeconds = clampValue(
4545
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
46-
CONSTANTS.FOUR_HOURS,
46+
CONSTANTS.SIX_HOURS,
4747
CONSTANTS.ONE_DAY,
4848
);
4949
cacheSeconds = process.env.CACHE_SECONDS

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ You can customize the appearance of all your cards however you wish with URL par
293293
* `bg_color` - Card's background color *(hex color)* **or** a gradient in the form of *angle,start,end*. Default: `fffefe`
294294
* `hide_border` - Hides the card's border *(boolean)*. Default: `false`
295295
* `theme` - Name of the theme, choose from [all available themes](./themes/README.md). Default: `default` theme.
296-
* `cache_seconds` - Sets the cache header manually *(min: 14400, max: 86400)*. Default: `14400 seconds (4 hours)`.
296+
* `cache_seconds` - Sets the cache header manually *(min: 21600, max: 86400)*. Default: `21600 seconds (6 hours)`.
297297
* `locale` - Sets the language in the card *(e.g. cn, de, es, etc.)*. Default: `en`.
298298
* `border_radius` - Corner rounding on the card. Default: `4.5`.
299299

300300
> [!WARNING]\
301-
> We use caching to decrease the load on our servers (see <https://github.com/anuraghazra/github-readme-stats/issues/1471#issuecomment-1271551425>). Our cards have a default cache of 4 hours (14400 seconds). Also, note that the cache is clamped to a minimum of 4 hours and a maximum of 24 hours.
301+
> We use caching to decrease the load on our servers (see <https://github.com/anuraghazra/github-readme-stats/issues/1471#issuecomment-1271551425>). Our cards have a default cache of 6 hours (21600 seconds). Also, note that the cache is clamped to a minimum of 6 hours and a maximum of 24 hours. If you want the data on your statistics card to be updated more often you can [deploy your own instance](#deploy-on-your-own) and set [environment variable](#disable-rate-limit-protections) `CACHE_SECONDS` to a value of your choosing.
302302
303303
##### Gradient in bg\_color
304304

src/common/utils.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,30 @@ const noop = () => {};
373373
const logger =
374374
process.env.NODE_ENV !== "test" ? console : { log: noop, error: noop };
375375

376-
// Cache settings.
377-
const CARD_CACHE_SECONDS = 14400;
378-
const ERROR_CACHE_SECONDS = 600;
376+
const ONE_MINUTE = 60;
377+
const FIVE_MINUTES = 300;
378+
const TEN_MINUTES = 600;
379+
const FIFTEEN_MINUTES = 900;
380+
const THIRTY_MINUTES = 1800;
381+
const TWO_HOURS = 7200;
382+
const FOUR_HOURS = 14400;
383+
const SIX_HOURS = 21600;
384+
const EIGHT_HOURS = 28800;
385+
const ONE_DAY = 86400;
379386

380387
const CONSTANTS = {
381-
ONE_MINUTE: 60,
382-
FIVE_MINUTES: 300,
383-
TEN_MINUTES: 600,
384-
FIFTEEN_MINUTES: 900,
385-
THIRTY_MINUTES: 1800,
386-
TWO_HOURS: 7200,
387-
FOUR_HOURS: 14400,
388-
ONE_DAY: 86400,
389-
CARD_CACHE_SECONDS,
390-
ERROR_CACHE_SECONDS,
388+
ONE_MINUTE,
389+
FIVE_MINUTES,
390+
TEN_MINUTES,
391+
FIFTEEN_MINUTES,
392+
THIRTY_MINUTES,
393+
TWO_HOURS,
394+
FOUR_HOURS,
395+
SIX_HOURS,
396+
EIGHT_HOURS,
397+
ONE_DAY,
398+
CARD_CACHE_SECONDS: SIX_HOURS,
399+
ERROR_CACHE_SECONDS: TEN_MINUTES,
391400
};
392401

393402
const SECONDARY_ERROR_MESSAGES = {

tests/api.test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,25 @@ describe("Test /api/", () => {
162162
["Content-Type", "image/svg+xml"],
163163
[
164164
"Cache-Control",
165-
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
166-
CONSTANTS.FOUR_HOURS
165+
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
166+
CONSTANTS.SIX_HOURS
167167
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
168168
],
169169
]);
170170
});
171171

172172
it("should set proper cache", async () => {
173-
const { req, res } = faker({ cache_seconds: 15000 }, data_stats);
173+
const cache_seconds = 35000;
174+
const { req, res } = faker({ cache_seconds }, data_stats);
174175
await api(req, res);
175176

176177
expect(res.setHeader.mock.calls).toEqual([
177178
["Content-Type", "image/svg+xml"],
178179
[
179180
"Cache-Control",
180-
`max-age=7500, s-maxage=${15000}, stale-while-revalidate=${
181+
`max-age=${
182+
cache_seconds / 2
183+
}, s-maxage=${cache_seconds}, stale-while-revalidate=${
181184
CONSTANTS.ONE_DAY
182185
}`,
183186
],
@@ -224,8 +227,8 @@ describe("Test /api/", () => {
224227
["Content-Type", "image/svg+xml"],
225228
[
226229
"Cache-Control",
227-
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
228-
CONSTANTS.FOUR_HOURS
230+
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
231+
CONSTANTS.SIX_HOURS
229232
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
230233
],
231234
]);
@@ -239,8 +242,8 @@ describe("Test /api/", () => {
239242
["Content-Type", "image/svg+xml"],
240243
[
241244
"Cache-Control",
242-
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
243-
CONSTANTS.FOUR_HOURS
245+
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
246+
CONSTANTS.SIX_HOURS
244247
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
245248
],
246249
]);

tests/gist.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ describe("Test /api/gist", () => {
188188
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
189189
expect(res.setHeader).toBeCalledWith(
190190
"Cache-Control",
191-
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
192-
CONSTANTS.FOUR_HOURS
191+
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
192+
CONSTANTS.SIX_HOURS
193193
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
194194
);
195195
});

0 commit comments

Comments
 (0)