ランキング応答のエッジキャッシュ導入#147
Merged
Merged
Conversation
ランキングの元データ cache_data/ranking_cache は rankingUpdateGo が毎時 更新するのみで変化しないが、従来は表示の度に関数実行→Firestore読み取りが 走っていた。ユーザーは日本・関数とFirestoreはUS(us-central1)にあるため、 この毎回の往復が表示遅延の一因になっている。 - ranking.go: Cache-Control を出し分け。全員共通のグローバル応答 (screen_name 無し)は public, s-maxage=300 で共有キャッシュ可能に、 my_rank を含む個人化応答(screen_name 付き)は private, no-store で どの階層にもキャッシュさせない(他人に別人の順位が返る事故を防止)。 - firebase.json: /rankingGo rewrite を追加し、Firebase Hosting CDN を ランキング取得の前段に置けるようにする。 - web: ランキング取得先を rankingBaseUrl(RANKING_BASE_URL、未設定時は API_URL にフォールバック)から取得。本番で RANKING_BASE_URL に Hosting オリジンを設定するとエッジキャッシュが有効になり、dev/emulator では 従来どおり関数を直叩きするため挙動は変わらない。 - ranking_test.go: Cache-Control 出し分けの単体テストを追加。 - docs/backend.md: 設計判断を記録。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DwkB6Hj2JggoKcy7FoxNcU
ランキングのエッジキャッシュを有効化するための RANKING_BASE_URL を、 PROD_ENV シークレット(write-onlyで全文再入力が必要)ではなく GitHub Actions の Variable から web/.env に追記する。公開URLのため Secret に する必要がなく、変数1個の追加だけで有効化できる。未設定時は空となり フロントの API_URL フォールバックで従来動作のままになるため無害。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DwkB6Hj2JggoKcy7FoxNcU
本番とステージング(d-shrine-dev)でランキング取得のオリジンが異なるため、 GitHub Actions Variable を環境別に分ける。既存の PROD_ENV / DEV_ENV の命名に 合わせ、本番は PROD_RANKING_BASE_URL、ステージングは DEV_RANKING_BASE_URL を それぞれの .env に追記する。いずれも未設定時は空となり API_URL フォールバックで 従来動作のままになるため無害。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DwkB6Hj2JggoKcy7FoxNcU
Contributor
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
kojira
pushed a commit
that referenced
this pull request
Jul 10, 2026
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DwkB6Hj2JggoKcy7FoxNcU
kojira
pushed a commit
that referenced
this pull request
Jul 10, 2026
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DwkB6Hj2JggoKcy7FoxNcU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
ランキング表示が遅く、ユーザー数の伸びを妨げている。ボトルネックは CDN(静的配信)ではなく、関数コールドスタート + Firestore 読み取りの毎回の往復。ユーザーは日本、関数と Firestore は US(
us-central1)にあるため、表示の度にこの往復が発生している。ランキングの元データ
cache_data/ranking_cacheはrankingUpdateGo(毎時0 * * * *)が更新するだけで、それ以外では変化しない。にもかかわらず表示の度に関数実行 → Firestore 読み取りが走っていた。そこで応答を CDN のエッジでキャッシュさせ、関数・Firestore への到達自体を減らす。コスト増は無し(min-instances 不要、Firebase Hosting のキャッシュは無料枠)。
変更内容
app/functions-go/ranking.go:Cache-Controlを出し分け(setRankingCacheHeaders)。screen_name無し・トップ画面/未ログイン閲覧という最多経路)→public, max-age=60, s-maxage=300, stale-while-revalidate=600。元データは最短でも毎時更新なので5分の陳腐化は無害。screen_name付き・my_rankを含む)→private, no-store。共有キャッシュに載せると他人に別人の順位が返る事故になるため、どの階層にもキャッシュさせない。app/firebase.json:/rankingGoの rewrite を追加し、Firebase Hosting CDN をランキング取得の前段に置けるようにする(既存ogpRewriteGoと同じ書式)。web/nuxt.config.js/web/components/ranking.vue: ランキング取得先をrankingBaseUrl(RANKING_BASE_URL、未設定時はAPI_URLにフォールバック)から取得。本番/ステージングで Hosting オリジンを設定するとエッジキャッシュが有効になり、未設定の dev/emulator では従来どおり関数を直叩きするため挙動は変わらない。.github/workflows/prod-deploy.yml/dev-deploy.yml:RANKING_BASE_URLを GitHub Actions Variable(PROD_RANKING_BASE_URL/DEV_RANKING_BASE_URL)からweb/.envに注入。公開 URL のため Secret 不要で、環境ごとに値を分ける。未設定時は空となりAPI_URLフォールバックで従来動作のままになるため無害。app/functions-go/ranking_test.go:Cache-Control出し分けの単体テストを追加。docs/backend.md: 設計判断を記録。デプロイ時に必要な設定(マージ後)
GitHub Actions の Variables に以下を設定(このPRのマージだけでは有効化しない。各環境のデプロイ実行時に焼き込まれる):
PROD_RANKING_BASE_URLhttps://d-shrine.jp(末尾スラッシュ無し)env/prod)DEV_RANKING_BASE_URLenv/dev)検証
go build ./... && go vet ./...:passgo test(TestRanking_CacheHeaders):pass補足(このPRには含めない別の改善候補)
sanpaiの逐次 Firestore 書き込み削減など、書き込み系の改善は別途。🤖 Generated with Claude Code
Generated by Claude Code