Skip to content

Commit 86a22da

Browse files
lvan100lianghuan
authored andcommitted
feat(redis): extend redis client using ioc
1 parent deeb641 commit 86a22da

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

example/example.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ import (
2626
"github.com/go-spring/spring-core/gs"
2727
"github.com/redis/go-redis/v9"
2828

29-
_ "github.com/go-spring/starter-go-redis"
29+
StarterGoRedis "github.com/go-spring/starter-go-redis"
3030
)
3131

3232
type Service struct {
3333
Redis *redis.Client `autowire:""`
3434
}
3535

36+
// AnotherRedisFactory is a custom implementation of the Factory interface.
37+
type AnotherRedisFactory struct{}
38+
39+
func (AnotherRedisFactory) CreateClient(c StarterGoRedis.Config) (*redis.Client, error) {
40+
return redis.NewClient(&redis.Options{
41+
Addr: c.Addr,
42+
Password: c.Password,
43+
}), nil
44+
}
45+
3646
func main() {
3747

48+
// Register a custom Factory bean to replace the default one.
49+
gs.Provide(func() StarterGoRedis.Factory {
50+
return &AnotherRedisFactory{}
51+
})
52+
3853
// Here `s` is not referenced by any other object,
3954
// so we need to register it as a root object.
4055
s := &Service{}
4156
gs.Root(gs.Object(s))
4257

58+
// Define a handler to GET a Redis key value.
4359
http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
4460
str, err := s.Redis.Get(r.Context(), "key").Result()
4561
if err != nil {
@@ -49,6 +65,7 @@ func main() {
4965
_, _ = w.Write([]byte(str))
5066
})
5167

68+
// Define a handler to SET a Redis key value.
5269
http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
5370
str, err := s.Redis.Set(r.Context(), "key", "value", 0).Result()
5471
if err != nil {

starter.go

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,61 @@
1717
package StarterGoRedis
1818

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

25+
// Config defines Redis connection configuration.
2426
type Config struct {
2527
Addr string `value:"${addr}"`
2628
Password string `value:"${password:=}"`
2729
}
2830

31+
// Factory defines an interface for creating Redis clients.
32+
type Factory interface {
33+
CreateClient(c Config) (*redis.Client, error)
34+
}
35+
36+
type DefaultFactory struct{}
37+
38+
// CreateClient creates a new Redis client based on the provided configuration.
39+
func (DefaultFactory) CreateClient(c Config) (*redis.Client, error) {
40+
return redis.NewClient(&redis.Options{
41+
Addr: c.Addr,
42+
Password: c.Password,
43+
}), nil
44+
}
45+
2946
func init() {
30-
gs.Group("${spring.go-redis}",
31-
func(c Config) (*redis.Client, error) { // init
32-
return redis.NewClient(&redis.Options{
33-
Addr: c.Addr,
34-
Password: c.Password,
35-
}), nil
36-
},
37-
func(client *redis.Client) error { // destroy
38-
return client.Close()
39-
})
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+
})
4077
}

0 commit comments

Comments
 (0)