This commit is contained in:
Harsha Rahul Boggaram 2025-09-06 14:10:26 -07:00
parent f227c4040f
commit 3bea950588
2 changed files with 41 additions and 68 deletions

View File

@ -116,12 +116,8 @@ func (h *HybridCache) processEvent(event CacheEvent) {
switch event.Type {
case "delete":
h.local.Delete(context.Background(), event.Key)
slog.Debug("processed cache delete event", "key", event.Key, "source", event.Source)
case "clear":
h.local.Clear(context.Background())
slog.Debug("processed cache clear event", "source", event.Source)
default:
slog.Debug("unknown cache event type", "type", event.Type)
}
}
@ -132,40 +128,32 @@ func (h *HybridCache) Set(ctx context.Context, key string, value any) {
// SetWithTTL adds a value to both Redis and local cache with custom TTL.
func (h *HybridCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration) {
// Always set in local cache
// Always set in local cache first
h.local.SetWithTTL(ctx, key, value, ttl)
// Try to set in Redis
// Try to set in Redis (no event needed - other pods will get it on demand)
if h.redis != nil {
h.redis.SetWithTTL(ctx, key, value, ttl)
// Publish set event (optional, mainly for monitoring)
event := CacheEvent{
Type: "set",
Key: key,
Timestamp: time.Now(),
Source: h.podID,
}
if err := h.redis.Publish(ctx, event); err != nil {
slog.Debug("failed to publish cache set event", "key", key, "error", err)
}
}
}
// Get retrieves a value from cache, trying Redis first, then local cache.
// Get retrieves a value from cache, trying local first for speed, then Redis.
func (h *HybridCache) Get(ctx context.Context, key string) (any, bool) {
// Try Redis first if available
// Try local cache first for speed
if value, ok := h.local.Get(ctx, key); ok {
return value, true
}
// Try Redis if local cache miss and Redis is available
if h.redis != nil {
if value, ok := h.redis.Get(ctx, key); ok {
// Also update local cache for faster subsequent access
// Populate local cache for faster subsequent access
h.local.SetWithTTL(ctx, key, value, h.config.DefaultTTL)
return value, true
}
}
// Fallback to local cache
return h.local.Get(ctx, key)
return nil, false
}
// Delete removes a value from both Redis and local cache.
@ -276,4 +264,3 @@ type CacheStats struct {
PodID string `json:"pod_id"`
EventQueueSize int64 `json:"event_queue_size"`
}

View File

@ -89,7 +89,6 @@ func (r *RedisCache) Set(ctx context.Context, key string, value any) {
// SetWithTTL adds a value to the cache with a custom TTL.
func (r *RedisCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration) {
// Serialize the value to JSON
data, err := json.Marshal(value)
if err != nil {
slog.Error("failed to marshal cache value", "key", key, "error", err)
@ -99,10 +98,7 @@ func (r *RedisCache) SetWithTTL(ctx context.Context, key string, value any, ttl
redisKey := r.buildKey(key)
if err := r.client.Set(ctx, redisKey, data, ttl).Err(); err != nil {
slog.Error("failed to set cache value in Redis", "key", redisKey, "error", err)
return
}
slog.Debug("cache value set in Redis", "key", redisKey, "ttl", ttl)
}
// Get retrieves a value from the cache.
@ -111,22 +107,18 @@ func (r *RedisCache) Get(ctx context.Context, key string) (any, bool) {
data, err := r.client.Get(ctx, redisKey).Bytes()
if err != nil {
if err == redis.Nil {
// Key not found
return nil, false
}
slog.Error("failed to get cache value from Redis", "key", redisKey, "error", err)
return nil, false
}
// We need to unmarshal to interface{} since we don't know the original type
// The caller should know what type to expect and can cast accordingly
var value any
if err := json.Unmarshal(data, &value); err != nil {
slog.Error("failed to unmarshal cache value", "key", redisKey, "error", err)
return nil, false
}
slog.Debug("cache value retrieved from Redis", "key", redisKey)
return value, true
}
@ -135,10 +127,7 @@ func (r *RedisCache) Delete(ctx context.Context, key string) {
redisKey := r.buildKey(key)
if err := r.client.Del(ctx, redisKey).Err(); err != nil {
slog.Error("failed to delete cache value from Redis", "key", redisKey, "error", err)
return
}
slog.Debug("cache value deleted from Redis", "key", redisKey)
}
// Clear removes all values from the cache with the configured prefix.
@ -161,9 +150,7 @@ func (r *RedisCache) Clear(ctx context.Context) {
if len(keys) > 0 {
if err := r.client.Del(ctx, keys...).Err(); err != nil {
slog.Error("failed to delete Redis keys", "pattern", pattern, "error", err)
return
}
slog.Debug("cleared cache keys from Redis", "pattern", pattern, "count", len(keys))
}
}
@ -248,4 +235,3 @@ type CacheEvent struct {
Timestamp time.Time `json:"timestamp"` // when the event occurred
Source string `json:"source"` // identifier of the pod that generated the event
}