Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go_College_最終発表資料__外部公開用_.pdf

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 Go_College_最終発表資料__外部公開用_.pdf

Avatar for xe-pc23

xe-pc23

March 23, 2026
Tweet

Other Decks in Programming

Transcript

  1. model/collection.go 1 2 3 4 5 6 7 8 9

    10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 func SelectCollectionItemsByUserID(ctx context.Context, userID string) ([]*CollectionItem, error) { var items []*CollectionItem key := collectionListKey(userID) err := db.Cache.Get(ctx, key, &items) if err == nil { log.Printf("collection items found in Redis for user ID: %s", userID) return items, nil } log.Printf("collection items not found in Redis for user ID: %s, fetching from MySQL: %v", userID, err) //======// [MySQLの処理] //======// // Redisにコレクションアイテムのリストを保存する if err := db.Cache.Set(&cache.Item{ Ctx: ctx, Key: key, Value: items, TTL: 7 * 24 * time.Hour, }); err != nil { log.Printf("failed to set collection items to Redis for user ID: %s, error: %v", userID, err) } else { log.Printf("collection items cached in Redis for user ID: %s", userID) } return items, nil } コレクションのRedis実装 Flowchart next:なぜTTL1週間 1
  2. 削除の処理を実装しているから コレクションのRedis実装 Flowchart model/gacha.go 1 2 3 4 5 6

    7 8 9 10 11 12 13 14 15 16 17 func SaveGachaResult(ctx context.Context, userID string, collectionIDs []int) error { // === 【DB更新処理】 ==================== // トランザクション開始 // 1. ガチャ結果をバルクインサート // 2. コインを消費 // トランザクション終了 (Commit) // ===================================== // DBの更新が成功したら、古いキャッシュを「削除」する key := collectionListKey(userID) if err := db.Cache.Delete(ctx, key); err != nil { log.Printf("failed to delete cache: %v", err) } return nil } 削除処理を実装しているが、TTLを設定しているのはユーザーがゲームをしなく なった時、キャッシュが溜まり続けるのを防ぐため1週間のTTLを設定している 補足 1
  3. 最終課題での挑戦 Redis Redisを実装した時としてない時の パフォーマンスの比較を実際に行いました 補足 -n :総リクエスト数 -c 同時並列数 1

    2 3 4 5 #インストール方法 brew install hey #テストで使用するコマンド hey -n 50000 -c 100 -H "x-token: $TOKEN" "$BASE_URL/collection/ list" HTTPリクエストの負荷テストツール 引用:https://github.com/rakyll/hey 1