|
17 | 17 | package StarterGoRedis |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "github.com/go-spring/spring-core/conf" |
| 20 | + "fmt" |
| 21 | + |
21 | 22 | "github.com/go-spring/spring-core/gs" |
22 | 23 | "github.com/redis/go-redis/v9" |
23 | 24 | ) |
24 | 25 |
|
25 | 26 | // Config defines Redis connection configuration. |
26 | 27 | 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. |
28 | 33 | 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{}) |
29 | 61 | } |
30 | 62 |
|
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 { |
33 | 65 | CreateClient(c Config) (*redis.Client, error) |
34 | 66 | } |
35 | 67 |
|
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{} |
37 | 79 |
|
38 | 80 | // 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) { |
40 | 82 | return redis.NewClient(&redis.Options{ |
41 | 83 | Addr: c.Addr, |
42 | 84 | Password: c.Password, |
43 | 85 | }), nil |
44 | 86 | } |
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