更新redis调用
This commit is contained in:
@@ -33,8 +33,10 @@ func GetRedisClient() *gredis.Redis {
|
||||
return getClient()
|
||||
}
|
||||
|
||||
// RedisClient 导出的 Redis 客户端(供 mongo.go 使用,兼容旧代码)
|
||||
var RedisClient = getClient()
|
||||
// RedisClient 获取 Redis 客户端(函数式,确保单例正确初始化)
|
||||
func RedisClient() *gredis.Redis {
|
||||
return getClient()
|
||||
}
|
||||
|
||||
// Lock 分布式锁
|
||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
||||
@@ -44,7 +46,8 @@ LOOP:
|
||||
return false, errors.New("锁重试次数耗尽")
|
||||
}
|
||||
limit--
|
||||
if val, err := RedisClient.Set(ctx, key, true, gredis.SetOption{
|
||||
client := getClient()
|
||||
if val, err := client.Set(ctx, key, true, gredis.SetOption{
|
||||
TTLOption: gredis.TTLOption{
|
||||
EX: &expireSeconds,
|
||||
},
|
||||
@@ -53,11 +56,11 @@ LOOP:
|
||||
return false, err
|
||||
} else {
|
||||
if val.Bool() {
|
||||
defer func(RedisClient *gredis.Redis, ctx context.Context, key string) {
|
||||
if _, err = RedisClient.Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "RedisClient.Del error: %v", err)
|
||||
defer func(client *gredis.Redis, ctx context.Context, key string) {
|
||||
if _, err = client.Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "redis client Del error: %v", err)
|
||||
}
|
||||
}(RedisClient, ctx, key)
|
||||
}(client, ctx, key)
|
||||
if err = fn(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user