优化用户模型和模块租户检查逻辑,新增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

28
nats/nats_publish.go Normal file
View File

@@ -0,0 +1,28 @@
package nats
import (
"context"
"encoding/json"
"fmt"
)
// publish 发布消息到指定主题
func publish(ctx context.Context, subject string, data any) (err error) {
if !IsConnected() {
return fmt.Errorf("NATS 未连接")
}
// 序列化数据
dataBytes, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("序列化数据失败: %w", err)
}
// 发布消息
metrics.PublishCount.Add(1)
_, err = js.Publish(ctx, subject, dataBytes)
if err != nil {
metrics.PublishError.Add(1)
return fmt.Errorf("发布消息失败: %w", err)
}
return
}