一键导入
app-caching
Implement application-level in-memory caching in Go using sync.Map for frequently accessed data that rarely changes
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Implement application-level in-memory caching in Go using sync.Map for frequently accessed data that rarely changes
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
MCP tool reference — exec, benchmark_start, benchmark_status, note_write, etc. Use when: executing commands on VMs, running benchmarks, or recording findings
ISUCON13 contest manual — server topology, service management, benchmark execution, and rules. Use when: checking environment setup, running benchmarks, or understanding contest constraints
ISUPipe application manual — features, terms, constraints, and API specs. Use when: understanding the app domain, checking API behavior or MUST/MAY requirements
General strategy for identifying and adding missing database indexes in ISUCON web applications
General techniques for optimizing user icon/avatar image serving in ISUCON web applications
General techniques for identifying and fixing N+1 query problems in ISUCON web applications
基于 SOC 职业分类
| name | app-caching |
| description | Implement application-level in-memory caching in Go using sync.Map for frequently accessed data that rarely changes |
アプリケーション内のメモリキャッシュで、頻繁にアクセスされるが変更が少ないデータの DB クエリを削減する。
ソースコードとスローログを分析して候補を特定する。
var cache sync.Map
func getByID(ctx context.Context, db *sqlx.DB, id int64) (*Model, error) {
// キャッシュチェック
if cached, ok := cache.Load(id); ok {
return cached.(*Model), nil
}
// キャッシュミス → DB クエリ
var model Model
if err := db.GetContext(ctx, &model, "SELECT * FROM table WHERE id = ?", id); err != nil {
return nil, err
}
// キャッシュに保存
cache.Store(id, &model)
return &model, nil
}
POST /api/initialize でデータリセットされる場合、キャッシュもクリアが必要:
func initializeHandler(c echo.Context) error {
cache = sync.Map{} // 全キャッシュをリセット
// ... 既存の初期化ロジック
}
更新 API がある場合は、更新時にもキャッシュを無効化:
func updateHandler(c echo.Context) error {
// ... DB 更新
cache.Delete(id) // または cache.Store(id, &updatedModel) で更新
}
ベンチマーク中に一切変更されないマスタデータは起動時にロード:
var masterData []Item
func loadMasterData(db *sqlx.DB) {
db.Select(&masterData, "SELECT * FROM master_table")
}
変更される可能性があるデータには TTL(有効期限)を設定:
type CacheEntry struct {
Value interface{}
ExpiresAt time.Time
}
func getCached(key string, ttl time.Duration, fetch func() (interface{}, error)) (interface{}, error) {
if entry, ok := cache.Load(key); ok {
if e := entry.(*CacheEntry); time.Now().Before(e.ExpiresAt) {
return e.Value, nil
}
}
val, err := fetch()
if err != nil { return nil, err }
cache.Store(key, &CacheEntry{Value: val, ExpiresAt: time.Now().Add(ttl)})
return val, nil
}