Add Redis caching for lookup data and admin cache management

- Add lookup-specific cache keys and methods to CacheService
- Add cache refresh on lookup CRUD operations in AdminLookupHandler
- Add Redis caching after seed-lookups in AdminSettingsHandler
- Add ETag generation for seeded data to support client-side caching
- Update task template handler with cache invalidation
- Fix route for clear-cache endpoint

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-05 22:35:09 -06:00
parent 1b06c0639c
commit 91a1f7ebed
8 changed files with 856 additions and 100 deletions

View File

@@ -4,16 +4,32 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"github.com/treytartt/casera-api/internal/dto/responses"
"github.com/treytartt/casera-api/internal/i18n"
"github.com/treytartt/casera-api/internal/services"
)
// SeededDataResponse represents the unified seeded data response
type SeededDataResponse struct {
ResidenceTypes interface{} `json:"residence_types"`
TaskCategories interface{} `json:"task_categories"`
TaskPriorities interface{} `json:"task_priorities"`
TaskFrequencies interface{} `json:"task_frequencies"`
TaskStatuses interface{} `json:"task_statuses"`
ContractorSpecialties interface{} `json:"contractor_specialties"`
TaskTemplates responses.TaskTemplatesGroupedResponse `json:"task_templates"`
}
// StaticDataHandler handles static/lookup data endpoints
type StaticDataHandler struct {
residenceService *services.ResidenceService
taskService *services.TaskService
contractorService *services.ContractorService
residenceService *services.ResidenceService
taskService *services.TaskService
contractorService *services.ContractorService
taskTemplateService *services.TaskTemplateService
cache *services.CacheService
}
// NewStaticDataHandler creates a new static data handler
@@ -21,18 +37,56 @@ func NewStaticDataHandler(
residenceService *services.ResidenceService,
taskService *services.TaskService,
contractorService *services.ContractorService,
taskTemplateService *services.TaskTemplateService,
cache *services.CacheService,
) *StaticDataHandler {
return &StaticDataHandler{
residenceService: residenceService,
taskService: taskService,
contractorService: contractorService,
residenceService: residenceService,
taskService: taskService,
contractorService: contractorService,
taskTemplateService: taskTemplateService,
cache: cache,
}
}
// GetStaticData handles GET /api/static_data/
// Returns all lookup/reference data in a single response
// Returns all lookup/reference data in a single response with ETag support
func (h *StaticDataHandler) GetStaticData(c *gin.Context) {
// Get all lookup data
ctx := c.Request.Context()
// Check If-None-Match header for conditional request
clientETag := c.GetHeader("If-None-Match")
// Try to get cached ETag first (fast path for 304 responses)
if h.cache != nil && clientETag != "" {
cachedETag, err := h.cache.GetSeededDataETag(ctx)
if err == nil && cachedETag == clientETag {
// Client has the latest data, return 304 Not Modified
c.Status(http.StatusNotModified)
return
}
}
// Try to get cached seeded data
if h.cache != nil {
var cachedData SeededDataResponse
err := h.cache.GetCachedSeededData(ctx, &cachedData)
if err == nil {
// Cache hit - get the ETag and return data
etag, etagErr := h.cache.GetSeededDataETag(ctx)
if etagErr == nil {
c.Header("ETag", etag)
c.Header("Cache-Control", "private, max-age=3600")
}
c.JSON(http.StatusOK, cachedData)
return
} else if err != redis.Nil {
// Log cache error but continue to fetch from DB
log.Warn().Err(err).Msg("Failed to get cached seeded data")
}
}
// Cache miss - fetch all data from services
residenceTypes, err := h.residenceService.GetResidenceTypes()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_fetch_residence_types")})
@@ -69,14 +123,35 @@ func (h *StaticDataHandler) GetStaticData(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
"residence_types": residenceTypes,
"task_categories": taskCategories,
"task_priorities": taskPriorities,
"task_frequencies": taskFrequencies,
"task_statuses": taskStatuses,
"contractor_specialties": contractorSpecialties,
})
taskTemplates, err := h.taskTemplateService.GetGrouped()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_fetch_task_templates")})
return
}
// Build response
seededData := SeededDataResponse{
ResidenceTypes: residenceTypes,
TaskCategories: taskCategories,
TaskPriorities: taskPriorities,
TaskFrequencies: taskFrequencies,
TaskStatuses: taskStatuses,
ContractorSpecialties: contractorSpecialties,
TaskTemplates: taskTemplates,
}
// Cache the data and get ETag
if h.cache != nil {
etag, cacheErr := h.cache.CacheSeededData(ctx, seededData)
if cacheErr != nil {
log.Warn().Err(cacheErr).Msg("Failed to cache seeded data")
} else {
c.Header("ETag", etag)
c.Header("Cache-Control", "private, max-age=3600")
}
}
c.JSON(http.StatusOK, seededData)
}
// RefreshStaticData handles POST /api/static_data/refresh/