- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
52 lines
950 B
Go
52 lines
950 B
Go
package redis
|
||
|
||
import (
|
||
"carrotskin/pkg/config"
|
||
"fmt"
|
||
"sync"
|
||
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
var (
|
||
// clientInstance 全局Redis客户端实例
|
||
clientInstance *Client
|
||
// once 确保只初始化一次
|
||
once sync.Once
|
||
// initError 初始化错误
|
||
initError error
|
||
)
|
||
|
||
// Init 初始化Redis客户端(线程安全,只会执行一次)
|
||
func Init(cfg config.RedisConfig, logger *zap.Logger) error {
|
||
once.Do(func() {
|
||
clientInstance, initError = New(cfg, logger)
|
||
if initError != nil {
|
||
return
|
||
}
|
||
})
|
||
return initError
|
||
}
|
||
|
||
// GetClient 获取Redis客户端实例(线程安全)
|
||
func GetClient() (*Client, error) {
|
||
if clientInstance == nil {
|
||
return nil, fmt.Errorf("Redis客户端未初始化,请先调用 redis.Init()")
|
||
}
|
||
return clientInstance, nil
|
||
}
|
||
|
||
// MustGetClient 获取Redis客户端实例,如果未初始化则panic
|
||
func MustGetClient() *Client {
|
||
client, err := GetClient()
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
return client
|
||
}
|
||
|
||
|
||
|
||
|
||
|