Improve cache parformance

This commit is contained in:
ryo-ma 2025-11-22 23:18:27 +09:00
parent 72245888de
commit 20908aff97
2 changed files with 14 additions and 4 deletions

View file

@ -13,10 +13,18 @@ import { cacheProvider } from "../src/config/cache.ts";
const serviceProvider = new GithubApiService(); const serviceProvider = new GithubApiService();
const client = new GithubRepositoryService(serviceProvider).repository; const client = new GithubRepositoryService(serviceProvider).repository;
// Build cache control header with optimized caching strategy
const cacheControlHeader = [
"public",
`max-age=${CONSTANTS.CACHE_MAX_AGE}`,
`s-maxage=${CONSTANTS.CDN_CACHE_MAX_AGE}`,
`stale-while-revalidate=${CONSTANTS.STALE_WHILE_REVALIDATE}`,
].join(", ");
const defaultHeaders = new Headers( const defaultHeaders = new Headers(
{ {
"Content-Type": "image/svg+xml", "Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, "Cache-Control": cacheControlHeader,
}, },
); );
@ -86,7 +94,7 @@ async function app(req: Request): Promise<Response> {
status: error.status, status: error.status,
headers: new Headers({ headers: new Headers({
"Content-Type": "text", "Content-Type": "text",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, "Cache-Control": cacheControlHeader,
}), }),
}, },
); );
@ -132,7 +140,7 @@ async function app(req: Request): Promise<Response> {
status: userResponseInfo.code, status: userResponseInfo.code,
headers: new Headers({ headers: new Headers({
"Content-Type": "text", "Content-Type": "text",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, "Cache-Control": cacheControlHeader,
}), }),
}, },
); );

View file

@ -52,6 +52,8 @@ const HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
export const CONSTANTS = { export const CONSTANTS = {
CACHE_MAX_AGE: 18800, CACHE_MAX_AGE: 18800,
CDN_CACHE_MAX_AGE: 28800, // 8 hours for CDN edge cache
STALE_WHILE_REVALIDATE: 86400, // 24 hours - serve stale while revalidating
DEFAULT_PANEL_SIZE: 110, DEFAULT_PANEL_SIZE: 110,
DEFAULT_MAX_COLUMN: 8, DEFAULT_MAX_COLUMN: 8,
DEFAULT_MAX_ROW: 3, DEFAULT_MAX_ROW: 3,
@ -60,7 +62,7 @@ export const CONSTANTS = {
DEFAULT_NO_BACKGROUND: false, DEFAULT_NO_BACKGROUND: false,
DEFAULT_NO_FRAME: false, DEFAULT_NO_FRAME: false,
DEFAULT_GITHUB_API: "https://api.github.com/graphql", DEFAULT_GITHUB_API: "https://api.github.com/graphql",
DEFAULT_GITHUB_RETRY_DELAY: 1000, DEFAULT_GITHUB_RETRY_DELAY: 500,
REVALIDATE_TIME: HOUR_IN_MILLISECONDS, REVALIDATE_TIME: HOUR_IN_MILLISECONDS,
REDIS_TTL: HOUR_IN_MILLISECONDS * 4, REDIS_TTL: HOUR_IN_MILLISECONDS * 4,
}; };