mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 01:50:25 +08:00
优化创建缓存任务时域名检查速度
This commit is contained in:
@@ -1594,12 +1594,16 @@ func (this *ServerDAO) FindAllEnabledServersWithDomain(tx *dbs.Tx, domain string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledServerWithDomain 根据域名查找服务集群ID
|
// FindEnabledServerWithDomain 根据域名查找服务集群ID
|
||||||
func (this *ServerDAO) FindEnabledServerWithDomain(tx *dbs.Tx, domain string) (server *Server, err error) {
|
func (this *ServerDAO) FindEnabledServerWithDomain(tx *dbs.Tx, userId int64, domain string) (server *Server, err error) {
|
||||||
if len(domain) == 0 {
|
if len(domain) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
one, err := this.Query(tx).
|
var query = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
one, err := query.
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Where("JSON_CONTAINS(plainServerNames, :domain)").
|
Where("JSON_CONTAINS(plainServerNames, :domain)").
|
||||||
Param("domain", strconv.Quote(domain)).
|
Param("domain", strconv.Quote(domain)).
|
||||||
@@ -1618,7 +1622,11 @@ func (this *ServerDAO) FindEnabledServerWithDomain(tx *dbs.Tx, domain string) (s
|
|||||||
var dotIndex = strings.Index(domain, ".")
|
var dotIndex = strings.Index(domain, ".")
|
||||||
if dotIndex > 0 {
|
if dotIndex > 0 {
|
||||||
var wildcardDomain = "*." + domain[dotIndex+1:]
|
var wildcardDomain = "*." + domain[dotIndex+1:]
|
||||||
one, err = this.Query(tx).
|
var wildcardQuery = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
wildcardQuery.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
one, err = wildcardQuery.
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Where("JSON_CONTAINS(plainServerNames, :domain)").
|
Where("JSON_CONTAINS(plainServerNames, :domain)").
|
||||||
Param("domain", strconv.Quote(wildcardDomain)).
|
Param("domain", strconv.Quote(wildcardDomain)).
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ func (this *HTTPCacheTaskService) CreateHTTPCacheTask(ctx context.Context, req *
|
|||||||
// 查询所在集群
|
// 查询所在集群
|
||||||
server, ok := domainMap[domain]
|
server, ok := domainMap[domain]
|
||||||
if !ok {
|
if !ok {
|
||||||
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, domain)
|
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, userId, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ func (this *HTTPCacheTaskKeyService) ValidateHTTPCacheTaskKeys(ctx context.Conte
|
|||||||
}
|
}
|
||||||
|
|
||||||
var pbFailResults = []*pb.ValidateHTTPCacheTaskKeysResponse_FailKey{}
|
var pbFailResults = []*pb.ValidateHTTPCacheTaskKeysResponse_FailKey{}
|
||||||
var domainMap = map[string]*models.Server{} // domain name => *Server
|
var foundDomainMap = map[string]*models.Server{} // domain name => *Server
|
||||||
|
var missingDomainMap = map[string]bool{} // domain name => true
|
||||||
for _, key := range req.Keys {
|
for _, key := range req.Keys {
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
|
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
|
||||||
@@ -55,21 +56,31 @@ func (this *HTTPCacheTaskKeyService) ValidateHTTPCacheTaskKeys(ctx context.Conte
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询所在集群
|
// 是否不存在
|
||||||
server, ok := domainMap[domain]
|
if missingDomainMap[domain] {
|
||||||
if !ok {
|
|
||||||
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, domain)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if server == nil {
|
|
||||||
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
|
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
|
||||||
Key: key,
|
Key: key,
|
||||||
ReasonCode: "requireServer",
|
ReasonCode: "requireServer",
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
domainMap[domain] = server
|
|
||||||
|
// 查询所在集群
|
||||||
|
server, ok := foundDomainMap[domain]
|
||||||
|
if !ok {
|
||||||
|
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, userId, domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if server == nil {
|
||||||
|
missingDomainMap[domain] = true
|
||||||
|
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
|
||||||
|
Key: key,
|
||||||
|
ReasonCode: "requireServer",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
foundDomainMap[domain] = server
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查用户
|
// 检查用户
|
||||||
|
|||||||
@@ -2010,7 +2010,7 @@ func (this *ServerService) PurgeServerCache(ctx context.Context, req *pb.PurgeSe
|
|||||||
// 查询所在集群
|
// 查询所在集群
|
||||||
server, ok := domainMap[domain]
|
server, ok := domainMap[domain]
|
||||||
if !ok {
|
if !ok {
|
||||||
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, domain)
|
server, err = models.SharedServerDAO.FindEnabledServerWithDomain(tx, 0, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user