更新redis调用
This commit is contained in:
@@ -123,7 +123,7 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
|
||||
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||
if !m.noCache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
|
||||
}
|
||||
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||
if !m.noCache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -169,7 +169,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||
if !m.noCache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -229,7 +229,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
return
|
||||
}
|
||||
if !m.noCache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -261,7 +261,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
redisKey := fmt.Sprintf(redis.One, user.TenantId, collection, filterKey)
|
||||
if !m.noCache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -283,7 +283,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
err = nil
|
||||
}
|
||||
if !m.noCache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -304,23 +304,23 @@ func (m *MongoDB) getDeletedData(ctx context.Context, filter bson.M, collection
|
||||
|
||||
func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) {
|
||||
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
||||
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
||||
keys, err := redis.RedisClient().Keys(ctx, listKeys)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, key := range keys {
|
||||
_, err = redis.RedisClient.Del(ctx, key)
|
||||
_, err = redis.RedisClient().Del(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
||||
keys, err = redis.RedisClient.Keys(ctx, countKeys)
|
||||
keys, err = redis.RedisClient().Keys(ctx, countKeys)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, key := range keys {
|
||||
_, err = redis.RedisClient.Del(ctx, key)
|
||||
_, err = redis.RedisClient().Del(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -329,7 +329,7 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
||||
delete(filter, "tenantId")
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
||||
_, err = redis.RedisClient.Del(ctx, oneKey)
|
||||
_, err = redis.RedisClient().Del(ctx, oneKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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