From b0b4c37ed6c88e8b8f95faff77183ad3525de39b Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Mon, 15 Dec 2025 18:15:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9-redis=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8D=95=E6=9D=A1=E6=9F=A5=E8=AF=A2=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consts/redis_key.go | 2 +- mongo/mongo.go | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) 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 }