From 2d980643d5319f8748bbae9e9e96381abbf20e8b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 16 Jun 2024 10:37:05 +0800 Subject: [PATCH] refactor(storage): Redis configuration handling and initialization (#783) - Update `go-redis` dependency from v9.5.1 to v9.5.3 - Modify `InitAppStatus` to pass individual Redis configuration parameters instead of the entire config object - Refactor `New` function in `redis.go` to accept individual parameters for Redis configuration - Remove `config` dependency from `redis.go` - Add new fields to `Storage` struct for Redis configuration parameters - Update `Init` method in `Storage` to use new Redis configuration fields - Refactor tests in `redis_test.go` to use new `New` function signature - Add assertions in `TestRedisEngine` to verify key reset and value retrieval Signed-off-by: appleboy --- go.mod | 2 +- go.sum | 4 ++-- status/status.go | 7 ++++++- storage/redis/redis.go | 37 +++++++++++++++++++++++-------------- storage/redis/redis_test.go | 26 +++++++++++++++++--------- 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 4809eb41..b3803869 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/mattn/go-isatty v0.0.20 github.com/mitchellh/mapstructure v1.5.0 github.com/prometheus/client_golang v1.19.0 - github.com/redis/go-redis/v9 v9.5.1 + github.com/redis/go-redis/v9 v9.5.3 github.com/rs/zerolog v1.32.0 github.com/sideshow/apns2 v0.23.0 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 44d28a8d..8bf2015a 100644 --- a/go.sum +++ b/go.sum @@ -278,8 +278,8 @@ github.com/prometheus/common v0.50.0 h1:YSZE6aa9+luNa2da6/Tik0q0A5AbR+U003TItK57 github.com/prometheus/common v0.50.0/go.mod h1:wHFBCEVWVmHMUpg7pYcOm2QUR/ocQdYSJVQJKnHc3xQ= github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGKX7o= github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g= -github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= -github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU= +github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= diff --git a/status/status.go b/status/status.go index 3bdcf644..4c2c0e80 100644 --- a/status/status.go +++ b/status/status.go @@ -63,7 +63,12 @@ func InitAppStatus(conf *config.ConfYaml) error { case "memory": store = memory.New() case "redis": - store = redis.New(conf) + store = redis.New( + conf.Stat.Redis.Addr, + conf.Stat.Redis.Password, + conf.Stat.Redis.DB, + conf.Stat.Redis.Cluster, + ) case "boltdb": store = boltdb.New( conf.Stat.BoltDB.Path, diff --git a/storage/redis/redis.go b/storage/redis/redis.go index c33259fe..4ec3295b 100644 --- a/storage/redis/redis.go +++ b/storage/redis/redis.go @@ -7,24 +7,33 @@ import ( "strconv" "strings" - "github.com/appleboy/gorush/config" - "github.com/redis/go-redis/v9" ) // New func implements the storage interface for gorush (https://github.com/appleboy/gorush) -func New(config *config.ConfYaml) *Storage { +func New( + addr string, + password string, + db int, + isCluster bool, +) *Storage { return &Storage{ - ctx: context.Background(), - config: config, + ctx: context.Background(), + addr: addr, + password: password, + db: db, + isCluster: isCluster, } } // Storage is interface structure type Storage struct { - ctx context.Context - config *config.ConfYaml - client redis.Cmdable + ctx context.Context + client redis.Cmdable + addr string + password string + db int + isCluster bool } func (s *Storage) Add(key string, count int64) { @@ -43,16 +52,16 @@ func (s *Storage) Get(key string) int64 { // Init client storage. func (s *Storage) Init() error { - if s.config.Stat.Redis.Cluster { + if s.isCluster { s.client = redis.NewClusterClient(&redis.ClusterOptions{ - Addrs: strings.Split(s.config.Stat.Redis.Addr, ","), - Password: s.config.Stat.Redis.Password, + Addrs: strings.Split(s.addr, ","), + Password: s.password, }) } else { s.client = redis.NewClient(&redis.Options{ - Addr: s.config.Stat.Redis.Addr, - Password: s.config.Stat.Redis.Password, - DB: s.config.Stat.Redis.DB, + Addr: s.addr, + Password: s.password, + DB: s.db, }) } diff --git a/storage/redis/redis_test.go b/storage/redis/redis_test.go index f1ea7c04..f9883378 100644 --- a/storage/redis/redis_test.go +++ b/storage/redis/redis_test.go @@ -4,17 +4,18 @@ import ( "sync" "testing" - "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" "github.com/stretchr/testify/assert" ) func TestRedisServerError(t *testing.T) { - cfg, _ := config.LoadConf() - cfg.Stat.Redis.Addr = "redis:6370" - - redis := New(cfg) + redis := New( + "redis:6370", // addr + "", // password + 0, // db + false, // cluster + ) err := redis.Init() assert.Error(t, err) @@ -23,13 +24,20 @@ func TestRedisServerError(t *testing.T) { func TestRedisEngine(t *testing.T) { var val int64 - cfg, _ := config.LoadConf() - cfg.Stat.Redis.Addr = "redis:6379" - - redis := New(cfg) + redis := New( + "redis:6379", // addr + "", // password + 0, // db + false, // cluster + ) err := redis.Init() assert.Nil(t, err) + // reset the value of the key to 0 + redis.Set(core.HuaweiSuccessKey, 0) + val = redis.Get(core.HuaweiSuccessKey) + assert.Equal(t, int64(0), val) + redis.Add(core.HuaweiSuccessKey, 10) val = redis.Get(core.HuaweiSuccessKey) assert.Equal(t, int64(10), val)