优化用户模型和模块租户检查逻辑,新增NATS消息配置和MongoDB缓存控制

This commit is contained in:
2026-01-21 16:33:37 +08:00
committed by 张斌
parent f0e6bdd37c
commit b303c1bfaa
16 changed files with 1954 additions and 497 deletions

View File

@@ -34,18 +34,16 @@ import (
// =============================================================================
type MongoDB struct {
Cache bool
noCache bool
dataSource string // 数据源名称,默认为 "default"
noTenantId bool // 是否跳过租户过滤
}
func DB(cache ...bool) *MongoDB {
b := true
if len(cache) > 0 {
b = cache[0]
}
return &MongoDB{
Cache: b,
noCache: false,
dataSource: "default",
noTenantId: false,
}
}
@@ -55,6 +53,18 @@ func (m *MongoDB) WithDataSource(name string) *MongoDB {
return m
}
// NoCache 不使用缓存
func (m *MongoDB) NoCache() *MongoDB {
m.noCache = true
return m
}
// NoTenantId 不使用租户过滤
func (m *MongoDB) NoTenantId() *MongoDB {
m.noTenantId = true
return m
}
// =============================================================================
// 向后兼容的全局变量和方法
// =============================================================================
@@ -111,7 +121,7 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
delete(filter, "tenantId")
filterKey := fmt.Sprintf("%+v", filter)
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
if m.Cache {
if !m.noCache {
var resultStr *gvar.Var
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
if err != nil {
@@ -122,8 +132,12 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
return
}
}
// 如果没有调用 noTenantId则添加 tenantId 过滤
if !m.noTenantId && !g.IsEmpty(user.TenantId) {
filter["tenantId"] = user.TenantId
}
count, err = db.Collection(collection).CountDocuments(ctx, filter)
if m.Cache {
if !m.noCache {
err = redis.RedisClient.SetEX(ctx, redisKey, count, int64(time.Hour))
if err != nil {
return
@@ -153,7 +167,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
filterKey := fmt.Sprintf("%+v", filter)
optionsKey := fmt.Sprintf("%+v%+v", page, orderBy)
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
if m.Cache {
if !m.noCache {
var resultStr *gvar.Var
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
if err != nil {
@@ -167,8 +181,10 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
return
}
}
filter["tenantId"] = user.TenantId
// 如果没有调用 noTenantId则添加 tenantId 过滤
if !m.noTenantId && !g.IsEmpty(user.TenantId) {
filter["tenantId"] = user.TenantId
}
limit := int64(PageSize)
skip := int64(0)
if page != nil && !g.IsEmpty(page.PageNum) && !g.IsEmpty(page.PageSize) {
@@ -213,7 +229,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
if err = cur.All(ctx, result); err != nil {
return
}
if m.Cache {
if !m.noCache {
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
if err != nil {
return
@@ -244,7 +260,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
filter["isDeleted"] = false
filterKey := fmt.Sprintf("%+v", filter)
redisKey := fmt.Sprintf(redis.One, user.TenantId, collection, filterKey)
if m.Cache {
if !m.noCache {
var resultStr *gvar.Var
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
if err != nil {
@@ -258,7 +274,8 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
return
}
}
if !g.IsEmpty(user.TenantId) {
// 如果没有调用 noTenantId则添加 tenantId 过滤
if !m.noTenantId && !g.IsEmpty(user.TenantId) {
filter["tenantId"] = user.TenantId
}
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
@@ -266,7 +283,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
if errors.Is(err, mongo.ErrNoDocuments) {
err = nil
}
if m.Cache {
if !m.noCache {
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
if err != nil {
return err