From 20908aff9736613ffdb8a7adf96aa1e2963a4d15 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sat, 22 Nov 2025 23:18:27 +0900 Subject: [PATCH] Improve cache parformance --- api/index.ts | 14 +++++++++++--- src/utils.ts | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/api/index.ts b/api/index.ts index daa117e..6f39b93 100644 --- a/api/index.ts +++ b/api/index.ts @@ -13,10 +13,18 @@ import { cacheProvider } from "../src/config/cache.ts"; const serviceProvider = new GithubApiService(); 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( { "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 { status: error.status, headers: new Headers({ "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 { status: userResponseInfo.code, headers: new Headers({ "Content-Type": "text", - "Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, + "Cache-Control": cacheControlHeader, }), }, ); diff --git a/src/utils.ts b/src/utils.ts index 86f90ce..9d52973 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -52,6 +52,8 @@ const HOUR_IN_MILLISECONDS = 60 * 60 * 1000; export const CONSTANTS = { 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_MAX_COLUMN: 8, DEFAULT_MAX_ROW: 3, @@ -60,7 +62,7 @@ export const CONSTANTS = { DEFAULT_NO_BACKGROUND: false, DEFAULT_NO_FRAME: false, DEFAULT_GITHUB_API: "https://api.github.com/graphql", - DEFAULT_GITHUB_RETRY_DELAY: 1000, + DEFAULT_GITHUB_RETRY_DELAY: 500, REVALIDATE_TIME: HOUR_IN_MILLISECONDS, REDIS_TTL: HOUR_IN_MILLISECONDS * 4, };