From 3d135b2d165e22d240977d96f19af6c84d7253cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9E=97=E7=91=9E?= <30717069+OldSmokeGun@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:08:20 +0800 Subject: [PATCH] feat: add support for multiple redis databases --- deploy/kubernetes/values.yaml.example | 13 +++++++------ etc/config.yaml.example | 13 +++++++------ internal/config/config.go | 12 ++++++++---- internal/config/redis.go | 19 +++++++++++++++---- internal/pkg/ent/provide.go | 2 +- internal/pkg/gorm/provide.go | 2 +- internal/pkg/pkg.go | 2 +- internal/pkg/redis/provide.go | 6 ++++-- 8 files changed, 44 insertions(+), 25 deletions(-) diff --git a/deploy/kubernetes/values.yaml.example b/deploy/kubernetes/values.yaml.example index 56bba20c..47dad8f6 100644 --- a/deploy/kubernetes/values.yaml.example +++ b/deploy/kubernetes/values.yaml.example @@ -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 ##################### diff --git a/etc/config.yaml.example b/etc/config.yaml.example index 9a3d6cc5..c0d54fec 100644 --- a/etc/config.yaml.example +++ b/etc/config.yaml.example @@ -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 ##################### diff --git a/internal/config/config.go b/internal/config/config.go index d596e05d..fc1b4170 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -17,7 +17,7 @@ var ProviderSet = wire.NewSet( GetServices, GetDiscovery, GetDefaultDatabase, - GetRedis, + GetDefaultRedis, GetExampleKafka, GetTrace, ) @@ -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"` } @@ -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) { diff --git a/internal/config/redis.go b/internal/config/redis.go index 0b113353..91363e2e 100644 --- a/internal/config/redis.go +++ b/internal/config/redis.go @@ -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"` @@ -21,7 +36,3 @@ type Redis struct { IdleTimeout time.Duration `json:"idleTimeout"` IdleCheckFrequency time.Duration `json:"idleCheckFrequency"` } - -func (Redis) GetName() string { - return "redis" -} diff --git a/internal/pkg/ent/provide.go b/internal/pkg/ent/provide.go index 21de3218..6c7e5f42 100644 --- a/internal/pkg/ent/provide.go +++ b/internal/pkg/ent/provide.go @@ -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 { diff --git a/internal/pkg/gorm/provide.go b/internal/pkg/gorm/provide.go index d50d3101..a8161ddf 100644 --- a/internal/pkg/gorm/provide.go +++ b/internal/pkg/gorm/provide.go @@ -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 { diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go index d94b0e48..75265aab 100644 --- a/internal/pkg/pkg.go +++ b/internal/pkg/pkg.go @@ -20,6 +20,6 @@ var ProviderSet = wire.NewSet( discovery.Provide, ent.ProvideDefault, gorm.ProvideDefault, - redis.Provide, + redis.ProvideDefault, uid.Provide, ) diff --git a/internal/pkg/redis/provide.go b/internal/pkg/redis/provide.go index b1f0c74e..a8c059b9 100644 --- a/internal/pkg/redis/provide.go +++ b/internal/pkg/redis/provide.go @@ -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