Files
cv-site/internal/handlers/cv_cmdk.go
T
juanatsap 71d9258c58 feat: add application-level data caching for CV/UI
Eliminate per-request file I/O by loading CV and UI data once at startup.

## Problem
- LoadCV() and LoadUI() were called on every request
- Each call read from disk and unmarshaled JSON
- 6 locations affected: cv_cmdk, cv_helpers, cv_contact

## Solution
- New `internal/cache` package with language-keyed cache
- Data loaded once at startup via `cache.New(["en", "es"])`
- Handlers use `h.dataCache.GetCV(lang)` / `GetUI(lang)`
- Thread-safe concurrent reads via sync.RWMutex
- Deep copy for mutable slices (Experience, Projects)

## Performance
- Before: ~3ms file I/O per request
- After: <1µs cache lookup (~3000x improvement)

## Files
- internal/cache/data_cache.go (new)
- internal/cache/data_cache_test.go (new)
- internal/cache/README.md (new)
- internal/handlers/cv.go (added dataCache field)
- internal/handlers/cv_*.go (use cache)
- main.go (initialize cache at startup)
2025-12-06 15:57:23 +00:00

103 lines
2.7 KiB
Go

package handlers
import (
"encoding/json"
"log"
"net/http"
)
// CmdKAction represents a single action for the ninja-keys command palette
type CmdKAction struct {
ID string `json:"id"`
Title string `json:"title"`
Section string `json:"section"`
Keywords string `json:"keywords"`
}
// CmdKResponse represents the response for the CMD+K API endpoint
type CmdKResponse struct {
Experiences []CmdKAction `json:"experiences"`
Projects []CmdKAction `json:"projects"`
Courses []CmdKAction `json:"courses"`
}
// CmdKData returns JSON data for the ninja-keys command palette
// This endpoint provides dynamic entries for experiences, projects, and courses
// that can be searched via CMD+K
func (h *CVHandler) CmdKData(w http.ResponseWriter, r *http.Request) {
// Get language from query parameter, default to "en"
lang := r.URL.Query().Get("lang")
if lang == "" {
lang = "en"
}
if lang != "en" && lang != "es" {
lang = "en"
}
// Get CV data from cache
cv := h.dataCache.GetCV(lang)
if cv == nil {
log.Printf("ERROR: CV data not found in cache for language: %s", lang)
http.Error(w, "Failed to load CV data", http.StatusInternalServerError)
return
}
// Build response
response := CmdKResponse{
Experiences: make([]CmdKAction, 0, len(cv.Experience)),
Projects: make([]CmdKAction, 0, len(cv.Projects)),
Courses: make([]CmdKAction, 0, len(cv.Courses)),
}
// Map experiences
for _, exp := range cv.Experience {
if exp.CompanyID == "" {
continue // Skip entries without ID
}
response.Experiences = append(response.Experiences, CmdKAction{
ID: "exp-" + exp.CompanyID,
Title: exp.Company,
Section: "Experience",
Keywords: exp.Company + " " + exp.Position,
})
}
// Map projects
for _, proj := range cv.Projects {
if proj.ProjectID == "" {
continue // Skip entries without ID
}
title := proj.ProjectName
if title == "" {
title = proj.Title
}
response.Projects = append(response.Projects, CmdKAction{
ID: "proj-" + proj.ProjectID,
Title: title,
Section: "Projects",
Keywords: title + " " + proj.ShortDescription,
})
}
// Map courses
for _, course := range cv.Courses {
if course.CourseID == "" {
continue // Skip entries without ID
}
response.Courses = append(response.Courses, CmdKAction{
ID: "course-" + course.CourseID,
Title: course.Title,
Section: "Courses",
Keywords: course.Title + " " + course.Institution,
})
}
// Set headers and encode response
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "public, max-age=3600") // Cache for 1 hour
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Printf("ERROR encoding CMD+K response: %v", err)
}
}