Skip to content

Commit

Permalink
refactor(storage): Redis configuration handling and initialization (#783
Browse files Browse the repository at this point in the history
)

- 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 <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jun 16, 2024
1 parent c28058d commit 2d98064
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 6 additions & 1 deletion status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 23 additions & 14 deletions storage/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
})
}

Expand Down
26 changes: 17 additions & 9 deletions storage/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2d98064

Please sign in to comment.