feature: extend default card cache time to 6 hours (#3242)

* feature: extend default card cache time to 8 hours

* reduce to six hours
This commit is contained in:
Alexandr Garbuzov 2023-09-19 12:54:27 +03:00 committed by GitHub
parent eb787af325
commit 2ee803fc16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 33 deletions

View file

@ -34,8 +34,8 @@ export default async (req, res) => {
const gistData = await fetchGist(id); const gistData = await fetchGist(id);
let cacheSeconds = clampValue( let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10), parseInt(cache_seconds || CONSTANTS.SIX_HOURS, 10),
CONSTANTS.FOUR_HOURS, CONSTANTS.SIX_HOURS,
CONSTANTS.ONE_DAY, CONSTANTS.ONE_DAY,
); );
cacheSeconds = process.env.CACHE_SECONDS cacheSeconds = process.env.CACHE_SECONDS
@ -52,7 +52,7 @@ export default async (req, res) => {
const isBothOver1K = stars > 1000 && forks > 1000; const isBothOver1K = stars > 1000 && forks > 1000;
const isBothUnder1 = stars < 1 && forks < 1; const isBothUnder1 = stars < 1 && forks < 1;
if (!cache_seconds && (isBothOver1K || isBothUnder1)) { if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
cacheSeconds = CONSTANTS.FOUR_HOURS; cacheSeconds = CONSTANTS.SIX_HOURS;
} }
res.setHeader( res.setHeader(

View file

@ -58,7 +58,7 @@ export default async (req, res) => {
let cacheSeconds = clampValue( let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.FOUR_HOURS, CONSTANTS.SIX_HOURS,
CONSTANTS.ONE_DAY, CONSTANTS.ONE_DAY,
); );
cacheSeconds = process.env.CACHE_SECONDS cacheSeconds = process.env.CACHE_SECONDS

View file

@ -41,7 +41,7 @@ export default async (req, res) => {
let cacheSeconds = clampValue( let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.FOUR_HOURS, CONSTANTS.SIX_HOURS,
CONSTANTS.ONE_DAY, CONSTANTS.ONE_DAY,
); );
cacheSeconds = process.env.CACHE_SECONDS cacheSeconds = process.env.CACHE_SECONDS
@ -58,7 +58,7 @@ export default async (req, res) => {
const isBothOver1K = stars > 1000 && forks > 1000; const isBothOver1K = stars > 1000 && forks > 1000;
const isBothUnder1 = stars < 1 && forks < 1; const isBothUnder1 = stars < 1 && forks < 1;
if (!cache_seconds && (isBothOver1K || isBothUnder1)) { if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
cacheSeconds = CONSTANTS.FOUR_HOURS; cacheSeconds = CONSTANTS.SIX_HOURS;
} }
res.setHeader( res.setHeader(

View file

@ -64,7 +64,7 @@ export default async (req, res) => {
let cacheSeconds = clampValue( let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.FOUR_HOURS, CONSTANTS.SIX_HOURS,
CONSTANTS.ONE_DAY, CONSTANTS.ONE_DAY,
); );
cacheSeconds = process.env.CACHE_SECONDS cacheSeconds = process.env.CACHE_SECONDS

View file

@ -43,7 +43,7 @@ export default async (req, res) => {
let cacheSeconds = clampValue( let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10),
CONSTANTS.FOUR_HOURS, CONSTANTS.SIX_HOURS,
CONSTANTS.ONE_DAY, CONSTANTS.ONE_DAY,
); );
cacheSeconds = process.env.CACHE_SECONDS cacheSeconds = process.env.CACHE_SECONDS

View file

@ -293,12 +293,12 @@ You can customize the appearance of all your cards however you wish with URL par
* `bg_color` - Card's background color *(hex color)* **or** a gradient in the form of *angle,start,end*. Default: `fffefe` * `bg_color` - Card's background color *(hex color)* **or** a gradient in the form of *angle,start,end*. Default: `fffefe`
* `hide_border` - Hides the card's border *(boolean)*. Default: `false` * `hide_border` - Hides the card's border *(boolean)*. Default: `false`
* `theme` - Name of the theme, choose from [all available themes](./themes/README.md). Default: `default` theme. * `theme` - Name of the theme, choose from [all available themes](./themes/README.md). Default: `default` theme.
* `cache_seconds` - Sets the cache header manually *(min: 14400, max: 86400)*. Default: `14400 seconds (4 hours)`. * `cache_seconds` - Sets the cache header manually *(min: 21600, max: 86400)*. Default: `21600 seconds (6 hours)`.
* `locale` - Sets the language in the card *(e.g. cn, de, es, etc.)*. Default: `en`. * `locale` - Sets the language in the card *(e.g. cn, de, es, etc.)*. Default: `en`.
* `border_radius` - Corner rounding on the card. Default: `4.5`. * `border_radius` - Corner rounding on the card. Default: `4.5`.
> [!WARNING]\ > [!WARNING]\
> 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. > 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.
##### Gradient in bg\_color ##### Gradient in bg\_color

View file

@ -373,21 +373,30 @@ const noop = () => {};
const logger = const logger =
process.env.NODE_ENV !== "test" ? console : { log: noop, error: noop }; process.env.NODE_ENV !== "test" ? console : { log: noop, error: noop };
// Cache settings. const ONE_MINUTE = 60;
const CARD_CACHE_SECONDS = 14400; const FIVE_MINUTES = 300;
const ERROR_CACHE_SECONDS = 600; const TEN_MINUTES = 600;
const FIFTEEN_MINUTES = 900;
const THIRTY_MINUTES = 1800;
const TWO_HOURS = 7200;
const FOUR_HOURS = 14400;
const SIX_HOURS = 21600;
const EIGHT_HOURS = 28800;
const ONE_DAY = 86400;
const CONSTANTS = { const CONSTANTS = {
ONE_MINUTE: 60, ONE_MINUTE,
FIVE_MINUTES: 300, FIVE_MINUTES,
TEN_MINUTES: 600, TEN_MINUTES,
FIFTEEN_MINUTES: 900, FIFTEEN_MINUTES,
THIRTY_MINUTES: 1800, THIRTY_MINUTES,
TWO_HOURS: 7200, TWO_HOURS,
FOUR_HOURS: 14400, FOUR_HOURS,
ONE_DAY: 86400, SIX_HOURS,
CARD_CACHE_SECONDS, EIGHT_HOURS,
ERROR_CACHE_SECONDS, ONE_DAY,
CARD_CACHE_SECONDS: SIX_HOURS,
ERROR_CACHE_SECONDS: TEN_MINUTES,
}; };
const SECONDARY_ERROR_MESSAGES = { const SECONDARY_ERROR_MESSAGES = {

View file

@ -162,22 +162,25 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"], ["Content-Type", "image/svg+xml"],
[ [
"Cache-Control", "Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${ `max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
], ],
]); ]);
}); });
it("should set proper cache", async () => { it("should set proper cache", async () => {
const { req, res } = faker({ cache_seconds: 15000 }, data_stats); const cache_seconds = 35000;
const { req, res } = faker({ cache_seconds }, data_stats);
await api(req, res); await api(req, res);
expect(res.setHeader.mock.calls).toEqual([ expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"], ["Content-Type", "image/svg+xml"],
[ [
"Cache-Control", "Cache-Control",
`max-age=7500, s-maxage=${15000}, stale-while-revalidate=${ `max-age=${
cache_seconds / 2
}, s-maxage=${cache_seconds}, stale-while-revalidate=${
CONSTANTS.ONE_DAY CONSTANTS.ONE_DAY
}`, }`,
], ],
@ -224,8 +227,8 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"], ["Content-Type", "image/svg+xml"],
[ [
"Cache-Control", "Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${ `max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
], ],
]); ]);
@ -239,8 +242,8 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"], ["Content-Type", "image/svg+xml"],
[ [
"Cache-Control", "Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${ `max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
], ],
]); ]);

View file

@ -188,8 +188,8 @@ describe("Test /api/gist", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith( expect(res.setHeader).toBeCalledWith(
"Cache-Control", "Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${ `max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
); );
}); });