Skip to content

Commit

Permalink
feat: add support for multiple redis databases
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSmokeGun committed Jul 3, 2024
1 parent 74e994a commit 3d135b2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 25 deletions.
13 changes: 7 additions & 6 deletions deploy/kubernetes/values.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ configMap:
##################### redis #####################

# redis:
# addr: "localhost:6379"
# password: ""
# database: 0
# poolSize: 20 # the maximum number of the connection pool
# minIdleConns: 10 # minimum number of idle connections
# idleTimeout: 600 # amount of time after which client closes idle connections.
# default:
# addr: "localhost:6379"
# password: ""
# database: 0
# poolSize: 20 # the maximum number of the connection pool
# minIdleConns: 10 # minimum number of idle connections
# idleTimeout: 600 # amount of time after which client closes idle connections.

##################### redis #####################

Expand Down
13 changes: 7 additions & 6 deletions etc/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ services:
##################### redis #####################

# redis:
# addr: "localhost:6379"
# password: ""
# database: 0
# poolSize: 20 # the maximum number of the connection pool
# minIdleConns: 10 # minimum number of idle connections
# idleTimeout: 600 # amount of time after which client closes idle connections.
# default:
# addr: "localhost:6379"
# password: ""
# database: 0
# poolSize: 20 # the maximum number of the connection pool
# minIdleConns: 10 # minimum number of idle connections
# idleTimeout: 600 # amount of time after which client closes idle connections.

##################### redis #####################

Expand Down
12 changes: 8 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var ProviderSet = wire.NewSet(
GetServices,
GetDiscovery,
GetDefaultDatabase,
GetRedis,
GetDefaultRedis,
GetExampleKafka,
GetTrace,
)
Expand All @@ -36,7 +36,7 @@ type Config struct {
Services *Services `json:"services"`
Discovery *Discovery `json:"discovery"`
Database *DatabaseGroup `json:"database"`
Redis *Redis `json:"redis"`
Redis *RedisGroup `json:"redis"`
Kafka *KafkaGroup `json:"kafka"`
Trace *Trace `json:"trace"`
}
Expand Down Expand Up @@ -100,8 +100,12 @@ func GetDefaultDatabase() (DefaultDatabase, error) {
return getEntry(databasesConfig.Default)
}

func GetRedis() (Redis, error) {
return getEntry(config.Redis)
func GetDefaultRedis() (DefaultRedis, error) {
redisConfig, err := getEntry(config.Redis)
if err != nil {
return DefaultRedis{}, err
}
return getEntry(redisConfig.Default)
}

func GetExampleKafka() (ExampleKafka, error) {
Expand Down
19 changes: 15 additions & 4 deletions internal/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ package config

import "time"

type RedisGroup struct {
Default *DefaultRedis `json:"default"`
}

func (RedisGroup) GetName() string {
return "redis"
}

// DefaultRedis default redis config
type DefaultRedis = Redis

func (DefaultRedis) GetName() string {
return "redis.default"
}

// Redis is redis config
type Redis struct {
Addr string `json:"addr"`
Expand All @@ -21,7 +36,3 @@ type Redis struct {
IdleTimeout time.Duration `json:"idleTimeout"`
IdleCheckFrequency time.Duration `json:"idleCheckFrequency"`
}

func (Redis) GetName() string {
return "redis"
}
2 changes: 1 addition & 1 deletion internal/pkg/ent/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type DefaultClient = ent.Client

// ProvideDefault db client
// ProvideDefault default db client
func ProvideDefault(ctx context.Context, env config.Env, conf config.DefaultDatabase, logger *slog.Logger) (*DefaultClient, func(), error) {
sdb, err := db.New(ctx, conf.DatabaseConn)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/gorm/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type DefaultDB = gorm.DB

// ProvideDefault gorm
// ProvideDefault default gorm
func ProvideDefault(ctx context.Context, conf config.DefaultDatabase, logger *slog.Logger) (db *DefaultDB, cleanup func(), err error) {
db, err = New(ctx, conf, logger)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ var ProviderSet = wire.NewSet(
discovery.Provide,
ent.ProvideDefault,
gorm.ProvideDefault,
redis.Provide,
redis.ProvideDefault,
uid.Provide,
)
6 changes: 4 additions & 2 deletions internal/pkg/redis/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"go-scaffold/internal/config"
)

// Provide redis client
func Provide(ctx context.Context, conf config.Redis) (*redis.Client, func(), error) {
type DefaultRedis = redis.Client

// ProvideDefault default redis client
func ProvideDefault(ctx context.Context, conf config.Redis) (*DefaultRedis, func(), error) {
client, err := New(ctx, conf)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 3d135b2

Please sign in to comment.