Skip to content

Commit 20908af

Browse files
committed
Improve cache parformance
1 parent 7224588 commit 20908af

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

api/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ import { cacheProvider } from "../src/config/cache.ts";
1313
const serviceProvider = new GithubApiService();
1414
const client = new GithubRepositoryService(serviceProvider).repository;
1515

16+
// Build cache control header with optimized caching strategy
17+
const cacheControlHeader = [
18+
"public",
19+
`max-age=${CONSTANTS.CACHE_MAX_AGE}`,
20+
`s-maxage=${CONSTANTS.CDN_CACHE_MAX_AGE}`,
21+
`stale-while-revalidate=${CONSTANTS.STALE_WHILE_REVALIDATE}`,
22+
].join(", ");
23+
1624
const defaultHeaders = new Headers(
1725
{
1826
"Content-Type": "image/svg+xml",
19-
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
27+
"Cache-Control": cacheControlHeader,
2028
},
2129
);
2230

@@ -86,7 +94,7 @@ async function app(req: Request): Promise<Response> {
8694
status: error.status,
8795
headers: new Headers({
8896
"Content-Type": "text",
89-
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
97+
"Cache-Control": cacheControlHeader,
9098
}),
9199
},
92100
);
@@ -132,7 +140,7 @@ async function app(req: Request): Promise<Response> {
132140
status: userResponseInfo.code,
133141
headers: new Headers({
134142
"Content-Type": "text",
135-
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
143+
"Cache-Control": cacheControlHeader,
136144
}),
137145
},
138146
);

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
5252

5353
export const CONSTANTS = {
5454
CACHE_MAX_AGE: 18800,
55+
CDN_CACHE_MAX_AGE: 28800, // 8 hours for CDN edge cache
56+
STALE_WHILE_REVALIDATE: 86400, // 24 hours - serve stale while revalidating
5557
DEFAULT_PANEL_SIZE: 110,
5658
DEFAULT_MAX_COLUMN: 8,
5759
DEFAULT_MAX_ROW: 3,
@@ -60,7 +62,7 @@ export const CONSTANTS = {
6062
DEFAULT_NO_BACKGROUND: false,
6163
DEFAULT_NO_FRAME: false,
6264
DEFAULT_GITHUB_API: "https://api.github.com/graphql",
63-
DEFAULT_GITHUB_RETRY_DELAY: 1000,
65+
DEFAULT_GITHUB_RETRY_DELAY: 500,
6466
REVALIDATE_TIME: HOUR_IN_MILLISECONDS,
6567
REDIS_TTL: HOUR_IN_MILLISECONDS * 4,
6668
};

0 commit comments

Comments
 (0)