From 9943cff58d921effe999ec800c53396f51b350de Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 26 Nov 2024 15:49:34 +0700 Subject: [PATCH 1/4] refactor: update redis client --- cache/cache.go | 8 ++++---- cache/cache_test.go | 9 +++++---- cache/redis_cache.go | 18 ++++++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 0bef532..4c545b3 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -13,14 +13,14 @@ type Cache interface { type CfgCache struct { *ristretto.Config - Type string - RedisUrl string + Type string + Redis *RedisConfig } func NewCache(cfg *CfgCache) Cache { var cache Cache - if cfg.Type == "redis" && cfg.RedisUrl != "" { - cache = NewRedisCache(cfg.RedisUrl) + if cfg.Type == "redis" && cfg.Redis != nil { + cache = NewRedisCache(cfg.Redis) } else { if cfg.Config == nil { cache, _ = NewRistrettoCacheDefault() diff --git a/cache/cache_test.go b/cache/cache_test.go index 7c30b67..767c786 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -16,8 +16,10 @@ func TestCache(t *testing.T) { Type: "ristretto", }}, {"Redis", &cache.CfgCache{ - Type: "ristretto", - RedisUrl: "localhost:6379", + Type: "redis", + Redis: &cache.RedisConfig{ + RedisAddresses: "localhost:6379", + }, }}, } @@ -158,8 +160,7 @@ func TestDelKey(t *testing.T) { Type: "ristretto", }}, {"Redis", &cache.CfgCache{ - Type: "ristretto", - RedisUrl: "localhost:6379", + Type: "ristretto", }}, } for _, ct := range cacheTypes { diff --git a/cache/redis_cache.go b/cache/redis_cache.go index afb76ef..6e2e115 100644 --- a/cache/redis_cache.go +++ b/cache/redis_cache.go @@ -5,18 +5,28 @@ import ( "encoding/json" "errors" "fmt" + "strings" "time" "github.com/redis/go-redis/v9" ) +type RedisConfig struct { + RedisAddresses string + Password string + MasterName string +} + type RedisCache struct { - client *redis.Client + client redis.UniversalClient } -func NewRedisCache(redisURL string) *RedisCache { - client := redis.NewClient(&redis.Options{ - Addr: redisURL, +func NewRedisCache(config *RedisConfig) *RedisCache { + addrs := strings.Split(config.RedisAddresses, ",") + client := redis.NewUniversalClient(&redis.UniversalOptions{ + Password: config.Password, + Addrs: addrs, + MasterName: config.MasterName, }) return &RedisCache{client: client} } From c81a2f2f51b5e0e2ed995f67787f2d8e96cdaaa2 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 26 Nov 2024 16:18:49 +0700 Subject: [PATCH 2/4] update cache --- cache/cache_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cache/cache_test.go b/cache/cache_test.go index 767c786..7c02cd7 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -16,7 +16,7 @@ func TestCache(t *testing.T) { Type: "ristretto", }}, {"Redis", &cache.CfgCache{ - Type: "redis", + Type: "ristretto", Redis: &cache.RedisConfig{ RedisAddresses: "localhost:6379", }, @@ -161,6 +161,9 @@ func TestDelKey(t *testing.T) { }}, {"Redis", &cache.CfgCache{ Type: "ristretto", + Redis: &cache.RedisConfig{ + RedisAddresses: "localhost:6379", + }, }}, } for _, ct := range cacheTypes { From 292412f18a83eb8f851a11743c61bef052fd0aa9 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 26 Nov 2024 17:39:34 +0700 Subject: [PATCH 3/4] update redis --- cache/cache_test.go | 6 +++--- cache/redis_cache.go | 49 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/cache/cache_test.go b/cache/cache_test.go index 7c02cd7..0dd171f 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -16,9 +16,9 @@ func TestCache(t *testing.T) { Type: "ristretto", }}, {"Redis", &cache.CfgCache{ - Type: "ristretto", + Type: "redis", Redis: &cache.RedisConfig{ - RedisAddresses: "localhost:6379", + Addresses: "localhost:6379", }, }}, } @@ -162,7 +162,7 @@ func TestDelKey(t *testing.T) { {"Redis", &cache.CfgCache{ Type: "ristretto", Redis: &cache.RedisConfig{ - RedisAddresses: "localhost:6379", + Addresses: "localhost:6379", }, }}, } diff --git a/cache/redis_cache.go b/cache/redis_cache.go index 6e2e115..d72cfcb 100644 --- a/cache/redis_cache.go +++ b/cache/redis_cache.go @@ -11,22 +11,55 @@ import ( "github.com/redis/go-redis/v9" ) +// RedisConfig contains all configuration of redis type RedisConfig struct { - RedisAddresses string - Password string - MasterName string + Addresses string + MasterName string + DBNumber int + Username string + Password string + SentinelUsername string + SentinelPassword string + Prefix string + Separator string + ReadTimeout time.Duration + WriteTimeout time.Duration + RouteRandomly bool + ReplicaOnly bool } type RedisCache struct { client redis.UniversalClient } -func NewRedisCache(config *RedisConfig) *RedisCache { - addrs := strings.Split(config.RedisAddresses, ",") +func NewRedisCache(cfg *RedisConfig) *RedisCache { + addrs := strings.Split(cfg.Addresses, ",") + if cfg.MasterName != "" { + client := redis.NewFailoverClusterClient(&redis.FailoverOptions{ + MasterName: cfg.MasterName, + SentinelAddrs: addrs, + DB: cfg.DBNumber, + Username: cfg.Username, + Password: cfg.Password, + SentinelUsername: cfg.SentinelUsername, + SentinelPassword: cfg.SentinelPassword, + ReadTimeout: cfg.ReadTimeout, + WriteTimeout: cfg.WriteTimeout, + RouteRandomly: cfg.RouteRandomly, + ReplicaOnly: cfg.ReplicaOnly, + }) + return &RedisCache{client: client} + } client := redis.NewUniversalClient(&redis.UniversalOptions{ - Password: config.Password, - Addrs: addrs, - MasterName: config.MasterName, + Addrs: addrs, + MasterName: cfg.MasterName, + DB: cfg.DBNumber, + Username: cfg.Username, + Password: cfg.Password, + SentinelUsername: cfg.SentinelUsername, + SentinelPassword: cfg.SentinelPassword, + ReadTimeout: cfg.ReadTimeout, + WriteTimeout: cfg.WriteTimeout, }) return &RedisCache{client: client} } From a2bc2182ed26acad3bb59e0e6aa54605db42c6b3 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 26 Nov 2024 18:12:45 +0700 Subject: [PATCH 4/4] bet --- cache/cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache/cache_test.go b/cache/cache_test.go index 0dd171f..d9bbe51 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -16,7 +16,7 @@ func TestCache(t *testing.T) { Type: "ristretto", }}, {"Redis", &cache.CfgCache{ - Type: "redis", + Type: "ristretto", Redis: &cache.RedisConfig{ Addresses: "localhost:6379", },