common版本更新v0.2.6;数据库查询接口增加是否从缓存中查询数据开关

This commit is contained in:
2025-12-29 15:27:00 +08:00
parent a27be3de8d
commit bb731cfb9c
8 changed files with 105 additions and 47 deletions

View File

@@ -9,25 +9,33 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Strategy = &strategyDao{}
var Strategy = &strategyDao{
NoCache: true,
}
type strategyDao struct{}
type strategyDao struct {
NoCache bool
}
func (d *strategyDao) SetNoCache() {
Strategy.NoCache = true
}
// GetByName 根据名称获取策略
func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, bson.M{"name": name}, &strategy, "strategies")
err = mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &strategy, "strategies")
return
}
// GetByID 根据ID获取策略
func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, bson.M{"_id": id}, &strategy, "strategies")
err = mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &strategy, "strategies")
return
}
// GetByTenantLevel 根据租户级别获取策略
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
err = mongo.FindOne(ctx, d.NoCache, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
return
}
@@ -75,14 +83,14 @@ func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel,
}
// 获取总数
total, err = mongo.Count(ctx, filter, "strategies")
total, err = mongo.Count(ctx, d.NoCache, filter, "strategies")
if err != nil {
return
}
// 分页查询
offset := (page - 1) * size
err = mongo.Find(ctx, filter, &list, "strategies",
err = mongo.Find(ctx, d.NoCache, filter, &list, "strategies",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))