|
@@ -7,15 +7,50 @@ import (
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
"log"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-func GetInstance(config string) (redisCache *RedisCache, err error) {
|
|
|
- if config == "" {
|
|
|
- log.Fatalf("创建redis失败")
|
|
|
+var (
|
|
|
+ redisCache *RedisCache
|
|
|
+ once sync.Once
|
|
|
+)
|
|
|
+
|
|
|
+func GetInstance() *RedisCache {
|
|
|
+ once.Do(func() {
|
|
|
+ config := redisConfig.GetConfig("redis")
|
|
|
+ // 检查是否成功获取到RedisConfig实例
|
|
|
+ redisInstance, ok := config.(*redisConfig.RedisConfig)
|
|
|
+ if !ok {
|
|
|
+ // 处理错误情况,比如记录日志或返回错误信息
|
|
|
+ logger.Info("加载redis配置失败")
|
|
|
+ return // 或者采取其他适当的错误处理措施
|
|
|
+ }
|
|
|
+ // 获取Redis配置选项,这里假设GetConfig方法返回(*RedisOpts, error),需要检查错误
|
|
|
+ opts := redisInstance.GetConfig().(redisConfig.RedisOpts)
|
|
|
+ // 检查Host是否已设置,然后初始化或执行相应操作
|
|
|
+ if opts.Host != "" {
|
|
|
+ logger.Info("初始化redis")
|
|
|
+ // 这里可以添加初始化Redis的逻辑
|
|
|
+ redisCache = newRedis(opts)
|
|
|
+ } else {
|
|
|
+ logger.Info("Redis configuration is incomplete: Host is missing")
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return redisCache
|
|
|
+}
|
|
|
+func newRedis(opts redisConfig.RedisOpts) *RedisCache {
|
|
|
+ if &opts == nil || opts.Host == "" {
|
|
|
+ log.Fatalf("redis 连接失败")
|
|
|
}
|
|
|
- // redisCache = NewRedis(redisCommon.ParseFromStr(""))
|
|
|
- return
|
|
|
+ redisTemplate := redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
+ Addrs: strings.Split(opts.Host, ";"),
|
|
|
+ DB: opts.Database,
|
|
|
+ Password: opts.Password,
|
|
|
+ IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
|
|
+ MinIdleConns: opts.MaxIdle,
|
|
|
+ })
|
|
|
+ return &RedisCache{redisTemplate: redisTemplate}
|
|
|
}
|
|
|
|
|
|
// RedisCache
|
|
@@ -24,22 +59,24 @@ type RedisCache struct {
|
|
|
redisTemplate redis.UniversalClient
|
|
|
}
|
|
|
|
|
|
-// Get 获取一个值
|
|
|
+// GetString 获取一个值
|
|
|
func (r *RedisCache) GetString(key string) string {
|
|
|
return r.GetStringWithContext(context.Background(), key)
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+// GetStringWithContext 获取一个值
|
|
|
func (r *RedisCache) GetStringWithContext(ctx context.Context, key string) (value string) {
|
|
|
value, _ = r.redisTemplate.Get(ctx, key).Result()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// SetContext 设置一个值
|
|
|
+// SetString 设置一个值
|
|
|
func (r *RedisCache) SetString(key string, val string, timeout time.Duration) error {
|
|
|
return r.redisTemplate.SetEX(context.Background(), key, val, timeout).Err()
|
|
|
}
|
|
|
|
|
|
-// SetContext 设置一个值
|
|
|
+// SetStringWithContext 设置一个值
|
|
|
func (r *RedisCache) SetStringWithContext(ctx context.Context, key string, val string, timeout time.Duration) error {
|
|
|
return r.redisTemplate.SetEX(ctx, key, val, timeout).Err()
|
|
|
}
|
|
@@ -49,7 +86,7 @@ func (r *RedisCache) IsExist(key string) bool {
|
|
|
return r.IsExistWithContext(context.Background(), key)
|
|
|
}
|
|
|
|
|
|
-// IsExistContext 判断key是否存在
|
|
|
+// IsExistWithContext 判断key是否存在
|
|
|
func (r *RedisCache) IsExistWithContext(ctx context.Context, key string) bool {
|
|
|
result, _ := r.redisTemplate.Exists(ctx, key).Result()
|
|
|
return result > 0
|
|
@@ -60,53 +97,19 @@ func (r *RedisCache) Delete(key string) error {
|
|
|
return r.DeleteWithContext(context.Background(), key)
|
|
|
}
|
|
|
|
|
|
-// DeleteContext 删除
|
|
|
+// DeleteWithContext 删除
|
|
|
func (r *RedisCache) DeleteWithContext(ctx context.Context, key string) error {
|
|
|
return r.redisTemplate.Del(ctx, key).Err()
|
|
|
}
|
|
|
+
|
|
|
+// Do 执行指令
|
|
|
func (r *RedisCache) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
|
|
return r.DoWithContext(context.Background(), commandName, args...)
|
|
|
}
|
|
|
|
|
|
+// DoWithContext 执行指令
|
|
|
func (r *RedisCache) DoWithContext(ctx context.Context, commandName string, args ...interface{}) (reply interface{}, err error) {
|
|
|
newArgs := []interface{}{commandName}
|
|
|
newArgs = append(newArgs, args...)
|
|
|
return r.redisTemplate.Do(ctx, newArgs...).Result()
|
|
|
}
|
|
|
-
|
|
|
-func NewRedis(opts redisConfig.RedisOpts) *RedisCache {
|
|
|
- if &opts == nil || opts.Host == "" {
|
|
|
- log.Fatalf("redis 连接失败")
|
|
|
- }
|
|
|
- redisTemplate := redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
- Addrs: strings.Split(opts.Host, ";"),
|
|
|
- DB: opts.Database,
|
|
|
- Password: opts.Password,
|
|
|
- IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
|
|
- MinIdleConns: opts.MaxIdle,
|
|
|
- })
|
|
|
- return &RedisCache{redisTemplate: redisTemplate}
|
|
|
-}
|
|
|
-
|
|
|
-func init() {
|
|
|
- config := redisConfig.GetConfig("redis")
|
|
|
- // 检查是否成功获取到RedisConfig实例
|
|
|
- redisInstance, ok := config.(*redisConfig.RedisConfig)
|
|
|
- if !ok {
|
|
|
- // 处理错误情况,比如记录日志或返回错误信息
|
|
|
- logger.Info("Failed to get RedisConfig instance")
|
|
|
- return // 或者采取其他适当的错误处理措施
|
|
|
- }
|
|
|
-
|
|
|
- // 获取Redis配置选项,这里假设GetConfig方法返回(*RedisOpts, error),需要检查错误
|
|
|
- opts := redisInstance.GetConfig().(redisConfig.RedisOpts)
|
|
|
-
|
|
|
- // 检查Host是否已设置,然后初始化或执行相应操作
|
|
|
- if opts.Host != "" {
|
|
|
- logger.Info("初始化redis")
|
|
|
- // 这里可以添加初始化Redis的逻辑
|
|
|
- NewRedis(opts)
|
|
|
- } else {
|
|
|
- logger.Info("Redis configuration is incomplete: Host is missing")
|
|
|
- }
|
|
|
-}
|