diff --git a/consts/redis_key.go b/consts/redis_key.go index aef970f..183aa69 100644 --- a/consts/redis_key.go +++ b/consts/redis_key.go @@ -4,4 +4,4 @@ const CleanList = "list:tenantId-%v:collection-%s:*" const CleanCount = "count:tenantId-%v:collection-%s:*" const List = "list:tenantId-%v:collection-%s:filter:%s:options:%s" const Count = "count:tenantId-%v:collection-%s:filter:%s" -const One = "one:tenantId-%v:collection-%s:filter:%s:*" +const One = "one:tenantId-%v:collection-%s:filter:%s" diff --git a/mongo/mongo.go b/mongo/mongo.go index cc1f8bc..336a5ad 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -184,8 +184,7 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection } filter["isDeleted"] = false filterMap := utils.OrderMap(filter) - optsMap := oneOptionsToMap(ctx, opts...) - redisKey := fmt.Sprintf(consts.One, user.TenantId, collection, gconv.String(filterMap), gconv.String(optsMap)) + redisKey := fmt.Sprintf(consts.One, user.TenantId, collection, gconv.String(filterMap)) resultStr, err := redis.RedisClient.Get(ctx, redisKey) if err != nil { return @@ -209,7 +208,7 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection } return } -func cleanRedis(ctx context.Context, tenantId interface{}, collection string) (err error) { +func cleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) { listKeys := fmt.Sprintf(consts.CleanList, tenantId, collection) keys, err := redis.RedisClient.Keys(ctx, listKeys) if err != nil { @@ -232,7 +231,11 @@ func cleanRedis(ctx context.Context, tenantId interface{}, collection string) (e return } } - oneKey := fmt.Sprintf(consts.One, tenantId, collection) + filter["isDeleted"] = false + delete(filter, "tenantId") + filterMap := utils.OrderMap(filter) + oneKey := fmt.Sprintf(consts.One, tenantId, collection, gconv.String(filterMap)) + fmt.Println(gconv.String(filterMap)) _, err = redis.RedisClient.Del(ctx, oneKey) if err != nil { return @@ -256,7 +259,7 @@ func Delete(ctx context.Context, filter bson.M, collection string, opts ...optio return } count = r.DeletedCount - err = cleanRedis(ctx, user.TenantId, collection) + err = cleanRedis(ctx, filter, user.TenantId, collection) return } @@ -280,7 +283,7 @@ func Update(ctx context.Context, filter bson.M, update bson.M, collection string if err != nil { return } - err = cleanRedis(ctx, user.TenantId, collection) + err = cleanRedis(ctx, filter, user.TenantId, collection) return } @@ -307,7 +310,7 @@ func Insert(ctx context.Context, documents []interface{}, collection string, opt return } ids = r.InsertedIDs - err = cleanRedis(ctx, user.TenantId, collection) + err = cleanRedis(ctx, bson.M{}, user.TenantId, collection) return }