fix: [CODE-2833]: Ignore Redis related cache errors when get/set key (#3059)

* Ignore Redis related cache errors when get/set key
pull/3597/head
Darko Draskovic 2024-11-28 18:19:12 +00:00 committed by Harness
parent 91aad4cf96
commit f0d58cdda7
2 changed files with 17 additions and 5 deletions

13
cache/redis_cache.go vendored
View File

@ -23,6 +23,8 @@ import (
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
type LogErrFn func(context.Context, error)
type Redis[K any, V any] struct { type Redis[K any, V any] struct {
client redis.UniversalClient client redis.UniversalClient
duration time.Duration duration time.Duration
@ -31,6 +33,7 @@ type Redis[K any, V any] struct {
codec Codec[V] codec Codec[V]
countHit int64 countHit int64
countMiss int64 countMiss int64
logErrFn LogErrFn
} }
type Encoder[V any] interface { type Encoder[V any] interface {
@ -52,6 +55,7 @@ func NewRedis[K any, V any](
keyEncoder func(K) string, keyEncoder func(K) string,
codec Codec[V], codec Codec[V],
duration time.Duration, duration time.Duration,
logErrFn LogErrFn,
) *Redis[K, V] { ) *Redis[K, V] {
return &Redis[K, V]{ return &Redis[K, V]{
client: client, client: client,
@ -61,6 +65,7 @@ func NewRedis[K any, V any](
codec: codec, codec: codec,
countHit: 0, countHit: 0,
countMiss: 0, countMiss: 0,
logErrFn: logErrFn,
} }
} }
@ -82,8 +87,8 @@ func (c *Redis[K, V]) Get(ctx context.Context, key K) (V, error) {
c.countHit++ c.countHit++
return value, nil return value, nil
} }
} else if !errors.Is(err, redis.Nil) { } else if !errors.Is(err, redis.Nil) && c.logErrFn != nil {
return nothing, err c.logErrFn(ctx, err)
} }
c.countMiss++ c.countMiss++
@ -94,8 +99,8 @@ func (c *Redis[K, V]) Get(ctx context.Context, key K) (V, error) {
} }
err = c.client.Set(ctx, strKey, c.codec.Encode(item), c.duration).Err() err = c.client.Set(ctx, strKey, c.codec.Encode(item), c.duration).Err()
if err != nil { if err != nil && c.logErrFn != nil {
return nothing, err c.logErrFn(ctx, err)
} }
return item, nil return item, nil

View File

@ -28,6 +28,7 @@ import (
"github.com/harness/gitness/git/sha" "github.com/harness/gitness/git/sha"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/rs/zerolog/log"
) )
func NewInMemoryLastCommitCache( func NewInMemoryLastCommitCache(
@ -38,6 +39,10 @@ func NewInMemoryLastCommitCache(
cacheDuration) cacheDuration)
} }
func logCacheErrFn(ctx context.Context, err error) {
log.Ctx(ctx).Warn().Msgf("failed to use cache: %s", err.Error())
}
func NewRedisLastCommitCache( func NewRedisLastCommitCache(
redisClient redis.UniversalClient, redisClient redis.UniversalClient,
cacheDuration time.Duration, cacheDuration time.Duration,
@ -55,7 +60,9 @@ func NewRedisLastCommitCache(
return "last_commit:" + hex.EncodeToString(h.Sum(nil)) return "last_commit:" + hex.EncodeToString(h.Sum(nil))
}, },
commitValueCodec{}, commitValueCodec{},
cacheDuration), nil cacheDuration,
logCacheErrFn,
), nil
} }
func NoLastCommitCache() cache.Cache[CommitEntryKey, *Commit] { func NoLastCommitCache() cache.Cache[CommitEntryKey, *Commit] {