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:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -161,3 +162,199 @@ func (c *CacheService) GetCachedStaticData(ctx context.Context, dest interface{}
|
||||
func (c *CacheService) InvalidateStaticData(ctx context.Context) error {
|
||||
return c.Delete(ctx, StaticDataKey)
|
||||
}
|
||||
|
||||
// Lookup data cache helpers - each lookup type gets its own key
|
||||
const (
|
||||
LookupKeyPrefix = "lookup:"
|
||||
LookupCategoriesKey = LookupKeyPrefix + "categories"
|
||||
LookupPrioritiesKey = LookupKeyPrefix + "priorities"
|
||||
LookupStatusesKey = LookupKeyPrefix + "statuses"
|
||||
LookupFrequenciesKey = LookupKeyPrefix + "frequencies"
|
||||
LookupResidenceTypesKey = LookupKeyPrefix + "residence_types"
|
||||
LookupSpecialtiesKey = LookupKeyPrefix + "specialties"
|
||||
LookupTaskTemplatesKey = LookupKeyPrefix + "task_templates"
|
||||
LookupDataTTL = 24 * time.Hour // Lookup data rarely changes
|
||||
)
|
||||
|
||||
// CacheLookupData caches data for a specific lookup type
|
||||
func (c *CacheService) CacheLookupData(ctx context.Context, key string, data interface{}) error {
|
||||
return c.Set(ctx, key, data, LookupDataTTL)
|
||||
}
|
||||
|
||||
// GetCachedLookupData retrieves cached lookup data for a specific key
|
||||
func (c *CacheService) GetCachedLookupData(ctx context.Context, key string, dest interface{}) error {
|
||||
return c.Get(ctx, key, dest)
|
||||
}
|
||||
|
||||
// InvalidateLookupData removes cached data for a specific lookup type
|
||||
func (c *CacheService) InvalidateLookupData(ctx context.Context, key string) error {
|
||||
return c.Delete(ctx, key)
|
||||
}
|
||||
|
||||
// InvalidateAllLookups removes all cached lookup data
|
||||
func (c *CacheService) InvalidateAllLookups(ctx context.Context) error {
|
||||
keys := []string{
|
||||
LookupCategoriesKey,
|
||||
LookupPrioritiesKey,
|
||||
LookupStatusesKey,
|
||||
LookupFrequenciesKey,
|
||||
LookupResidenceTypesKey,
|
||||
LookupSpecialtiesKey,
|
||||
LookupTaskTemplatesKey,
|
||||
StaticDataKey, // Also invalidate the combined static data
|
||||
SeededDataKey, // Invalidate unified seeded data
|
||||
SeededDataETagKey, // Invalidate seeded data ETag
|
||||
}
|
||||
return c.Delete(ctx, keys...)
|
||||
}
|
||||
|
||||
// CacheCategories caches task categories
|
||||
func (c *CacheService) CacheCategories(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupCategoriesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedCategories retrieves cached task categories
|
||||
func (c *CacheService) GetCachedCategories(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupCategoriesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateCategories removes cached task categories
|
||||
func (c *CacheService) InvalidateCategories(ctx context.Context) error {
|
||||
// Invalidate both specific key and combined static data
|
||||
return c.Delete(ctx, LookupCategoriesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CachePriorities caches task priorities
|
||||
func (c *CacheService) CachePriorities(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupPrioritiesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedPriorities retrieves cached task priorities
|
||||
func (c *CacheService) GetCachedPriorities(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupPrioritiesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidatePriorities removes cached task priorities
|
||||
func (c *CacheService) InvalidatePriorities(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupPrioritiesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CacheStatuses caches task statuses
|
||||
func (c *CacheService) CacheStatuses(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupStatusesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedStatuses retrieves cached task statuses
|
||||
func (c *CacheService) GetCachedStatuses(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupStatusesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateStatuses removes cached task statuses
|
||||
func (c *CacheService) InvalidateStatuses(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupStatusesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CacheFrequencies caches task frequencies
|
||||
func (c *CacheService) CacheFrequencies(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupFrequenciesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedFrequencies retrieves cached task frequencies
|
||||
func (c *CacheService) GetCachedFrequencies(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupFrequenciesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateFrequencies removes cached task frequencies
|
||||
func (c *CacheService) InvalidateFrequencies(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupFrequenciesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CacheResidenceTypes caches residence types
|
||||
func (c *CacheService) CacheResidenceTypes(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupResidenceTypesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedResidenceTypes retrieves cached residence types
|
||||
func (c *CacheService) GetCachedResidenceTypes(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupResidenceTypesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateResidenceTypes removes cached residence types
|
||||
func (c *CacheService) InvalidateResidenceTypes(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupResidenceTypesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CacheSpecialties caches contractor specialties
|
||||
func (c *CacheService) CacheSpecialties(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupSpecialtiesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedSpecialties retrieves cached contractor specialties
|
||||
func (c *CacheService) GetCachedSpecialties(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupSpecialtiesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateSpecialties removes cached contractor specialties
|
||||
func (c *CacheService) InvalidateSpecialties(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupSpecialtiesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// CacheTaskTemplates caches task templates
|
||||
func (c *CacheService) CacheTaskTemplates(ctx context.Context, data interface{}) error {
|
||||
return c.CacheLookupData(ctx, LookupTaskTemplatesKey, data)
|
||||
}
|
||||
|
||||
// GetCachedTaskTemplates retrieves cached task templates
|
||||
func (c *CacheService) GetCachedTaskTemplates(ctx context.Context, dest interface{}) error {
|
||||
return c.GetCachedLookupData(ctx, LookupTaskTemplatesKey, dest)
|
||||
}
|
||||
|
||||
// InvalidateTaskTemplates removes cached task templates
|
||||
func (c *CacheService) InvalidateTaskTemplates(ctx context.Context) error {
|
||||
return c.Delete(ctx, LookupTaskTemplatesKey, StaticDataKey)
|
||||
}
|
||||
|
||||
// Unified seeded data cache helpers
|
||||
const (
|
||||
SeededDataKey = "seeded_data"
|
||||
SeededDataETagKey = "seeded_data:etag"
|
||||
SeededDataTTL = 24 * time.Hour
|
||||
)
|
||||
|
||||
// CacheSeededData caches the unified seeded data and generates an ETag
|
||||
func (c *CacheService) CacheSeededData(ctx context.Context, data interface{}) (string, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal seeded data: %w", err)
|
||||
}
|
||||
|
||||
// Generate MD5 ETag from the JSON data
|
||||
hash := md5.Sum(jsonData)
|
||||
etag := fmt.Sprintf("\"%x\"", hash)
|
||||
|
||||
// Store both the data and the ETag
|
||||
if err := c.client.Set(ctx, SeededDataKey, jsonData, SeededDataTTL).Err(); err != nil {
|
||||
return "", fmt.Errorf("failed to cache seeded data: %w", err)
|
||||
}
|
||||
|
||||
if err := c.client.Set(ctx, SeededDataETagKey, etag, SeededDataTTL).Err(); err != nil {
|
||||
return "", fmt.Errorf("failed to cache seeded data etag: %w", err)
|
||||
}
|
||||
|
||||
return etag, nil
|
||||
}
|
||||
|
||||
// GetCachedSeededData retrieves cached unified seeded data
|
||||
func (c *CacheService) GetCachedSeededData(ctx context.Context, dest interface{}) error {
|
||||
return c.Get(ctx, SeededDataKey, dest)
|
||||
}
|
||||
|
||||
// GetSeededDataETag retrieves the cached ETag for seeded data
|
||||
func (c *CacheService) GetSeededDataETag(ctx context.Context) (string, error) {
|
||||
return c.GetString(ctx, SeededDataETagKey)
|
||||
}
|
||||
|
||||
// InvalidateSeededData removes cached seeded data and its ETag
|
||||
func (c *CacheService) InvalidateSeededData(ctx context.Context) error {
|
||||
return c.Delete(ctx, SeededDataKey, SeededDataETagKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user