更新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)
|
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||||
if !m.noCache {
|
if !m.noCache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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)
|
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||||
if !m.noCache {
|
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 {
|
if err != nil {
|
||||||
return
|
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)
|
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||||
if !m.noCache {
|
if !m.noCache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -229,7 +229,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !m.noCache {
|
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 {
|
if err != nil {
|
||||||
return
|
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)
|
redisKey := fmt.Sprintf(redis.One, user.TenantId, collection, filterKey)
|
||||||
if !m.noCache {
|
if !m.noCache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -283,7 +283,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
|||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if !m.noCache {
|
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 {
|
if err != nil {
|
||||||
return err
|
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) {
|
func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) {
|
||||||
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
||||||
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
keys, err := redis.RedisClient().Keys(ctx, listKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
_, err = redis.RedisClient.Del(ctx, key)
|
_, err = redis.RedisClient().Del(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
||||||
keys, err = redis.RedisClient.Keys(ctx, countKeys)
|
keys, err = redis.RedisClient().Keys(ctx, countKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
_, err = redis.RedisClient.Del(ctx, key)
|
_, err = redis.RedisClient().Del(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -329,7 +329,7 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
|||||||
delete(filter, "tenantId")
|
delete(filter, "tenantId")
|
||||||
filterKey := fmt.Sprintf("%+v", filter)
|
filterKey := fmt.Sprintf("%+v", filter)
|
||||||
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
||||||
_, err = redis.RedisClient.Del(ctx, oneKey)
|
_, err = redis.RedisClient().Del(ctx, oneKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,10 @@ func GetRedisClient() *gredis.Redis {
|
|||||||
return getClient()
|
return getClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedisClient 导出的 Redis 客户端(供 mongo.go 使用,兼容旧代码)
|
// RedisClient 获取 Redis 客户端(函数式,确保单例正确初始化)
|
||||||
var RedisClient = getClient()
|
func RedisClient() *gredis.Redis {
|
||||||
|
return getClient()
|
||||||
|
}
|
||||||
|
|
||||||
// Lock 分布式锁
|
// Lock 分布式锁
|
||||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
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("锁重试次数耗尽")
|
return false, errors.New("锁重试次数耗尽")
|
||||||
}
|
}
|
||||||
limit--
|
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{
|
TTLOption: gredis.TTLOption{
|
||||||
EX: &expireSeconds,
|
EX: &expireSeconds,
|
||||||
},
|
},
|
||||||
@@ -53,11 +56,11 @@ LOOP:
|
|||||||
return false, err
|
return false, err
|
||||||
} else {
|
} else {
|
||||||
if val.Bool() {
|
if val.Bool() {
|
||||||
defer func(RedisClient *gredis.Redis, ctx context.Context, key string) {
|
defer func(client *gredis.Redis, ctx context.Context, key string) {
|
||||||
if _, err = RedisClient.Del(ctx, key); err != nil {
|
if _, err = client.Del(ctx, key); err != nil {
|
||||||
glog.Errorf(ctx, "RedisClient.Del error: %v", err)
|
glog.Errorf(ctx, "redis client Del error: %v", err)
|
||||||
}
|
}
|
||||||
}(RedisClient, ctx, key)
|
}(client, ctx, key)
|
||||||
if err = fn(ctx); err != nil {
|
if err = fn(ctx); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user