Skip to content

Commit e28fa5f

Browse files
lvan100lianghuan
authored andcommitted
feat(redis): support configurable redis driver
1 parent 86a22da commit e28fa5f

File tree

3 files changed

+64
-50
lines changed

3 files changed

+64
-50
lines changed

example/conf/app.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
spring.go-redis.main.addr=127.0.0.1:6379
1+
spring.go-redis.main.addr=127.0.0.1:6379
2+
spring.go-redis.main.driver=AnotherRedisDriver

example/example.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,42 @@
1717
package main
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"net/http"
2223
"os"
2324
"path/filepath"
2425
"runtime"
2526

27+
"github.com/go-spring/log"
2628
"github.com/go-spring/spring-core/gs"
2729
"github.com/redis/go-redis/v9"
2830

2931
StarterGoRedis "github.com/go-spring/starter-go-redis"
3032
)
3133

32-
type Service struct {
33-
Redis *redis.Client `autowire:""`
34+
func init() {
35+
StarterGoRedis.RegisterDriver("AnotherRedisDriver", &AnotherRedisDriver{})
3436
}
3537

36-
// AnotherRedisFactory is a custom implementation of the Factory interface.
37-
type AnotherRedisFactory struct{}
38+
// AnotherRedisDriver is a custom implementation of the Driver interface.
39+
type AnotherRedisDriver struct{}
3840

39-
func (AnotherRedisFactory) CreateClient(c StarterGoRedis.Config) (*redis.Client, error) {
41+
func (AnotherRedisDriver) CreateClient(c StarterGoRedis.Config) (*redis.Client, error) {
42+
log.Infof(context.Background(), log.TagAppDef, "AnotherRedisDriver::CreateClient")
4043
return redis.NewClient(&redis.Options{
4144
Addr: c.Addr,
4245
Password: c.Password,
4346
}), nil
4447
}
4548

46-
func main() {
49+
type Service struct {
50+
Redis *redis.Client `autowire:""`
51+
}
4752

48-
// Register a custom Factory bean to replace the default one.
49-
gs.Provide(func() StarterGoRedis.Factory {
50-
return &AnotherRedisFactory{}
51-
})
53+
func main() {
54+
// You can change the `driver` property in the configuration file
55+
// and check the used Redis driver via logs.
5256

5357
// Here `s` is not referenced by any other object,
5458
// so we need to register it as a root object.

starter.go

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,70 @@
1717
package StarterGoRedis
1818

1919
import (
20-
"github.com/go-spring/spring-core/conf"
20+
"fmt"
21+
2122
"github.com/go-spring/spring-core/gs"
2223
"github.com/redis/go-redis/v9"
2324
)
2425

2526
// Config defines Redis connection configuration.
2627
type Config struct {
27-
Addr string `value:"${addr}"`
28+
29+
// Addr is the Redis server address.
30+
Addr string `value:"${addr}"`
31+
32+
// Password is the Redis server password, default is empty.
2833
Password string `value:"${password:=}"`
34+
35+
// Driver specifies which Redis driver to use, defaults to DefaultDriver.
36+
Driver string `value:"${driver:=DefaultDriver}"`
37+
}
38+
39+
func init() {
40+
// Register a group of beans under the key "${spring.go-redis}".
41+
// This group manages the lifecycle of Redis clients.
42+
gs.Group("${spring.go-redis}",
43+
// create function creates a new Redis client
44+
func(c Config) (*redis.Client, error) {
45+
d, ok := driverRegistry[c.Driver]
46+
if !ok {
47+
return nil, fmt.Errorf("redis driver not found: %s", c.Driver)
48+
}
49+
return d.CreateClient(c)
50+
},
51+
// destroy function closes the Redis client
52+
func(client *redis.Client) error {
53+
return client.Close()
54+
})
55+
}
56+
57+
var driverRegistry = map[string]Driver{}
58+
59+
func init() {
60+
RegisterDriver("DefaultDriver", DefaultDriver{})
2961
}
3062

31-
// Factory defines an interface for creating Redis clients.
32-
type Factory interface {
63+
// Driver interface defines how to create a Redis client.
64+
type Driver interface {
3365
CreateClient(c Config) (*redis.Client, error)
3466
}
3567

36-
type DefaultFactory struct{}
68+
// RegisterDriver registers a Redis driver with the given name.
69+
// It panics if the driver name has already been registered.
70+
func RegisterDriver(name string, driver Driver) {
71+
if _, ok := driverRegistry[name]; ok {
72+
panic("redis driver already registered: " + name)
73+
}
74+
driverRegistry[name] = driver
75+
}
76+
77+
// DefaultDriver is the default implementation of the Driver interface.
78+
type DefaultDriver struct{}
3779

3880
// CreateClient creates a new Redis client based on the provided configuration.
39-
func (DefaultFactory) CreateClient(c Config) (*redis.Client, error) {
81+
func (DefaultDriver) CreateClient(c Config) (*redis.Client, error) {
4082
return redis.NewClient(&redis.Options{
4183
Addr: c.Addr,
4284
Password: c.Password,
4385
}), nil
4486
}
45-
46-
func init() {
47-
const key = "spring.go-redis"
48-
49-
// Register a module that initializes Redis clients
50-
gs.Module([]gs.ConditionOnProperty{
51-
gs.OnProperty(key),
52-
}, func(p conf.Properties) error {
53-
54-
// Bind configuration into a map of name -> Config
55-
var m map[string]Config
56-
if err := p.Bind(&m, "${"+key+"}"); err != nil {
57-
return err
58-
}
59-
60-
// Register DefaultFactory as a bean implementing Factory,
61-
// but only if no other Factory bean has been provided.
62-
gs.Object(&DefaultFactory{}).
63-
Condition(gs.OnMissingBean[Factory]()).
64-
Export(gs.As[Factory]())
65-
66-
// For each Redis configuration entry,
67-
// create and register a Redis client bean.
68-
for name, c := range m {
69-
gs.Provide(func(factory Factory) (*redis.Client, error) { // create
70-
return factory.CreateClient(c)
71-
}).Destroy(func(client *redis.Client) error { // destroy
72-
return client.Close()
73-
}).Name(name)
74-
}
75-
return nil
76-
})
77-
}

0 commit comments

Comments
 (0)