缓存条件支持反向匹配

This commit is contained in:
GoEdgeLab
2021-06-09 17:15:12 +08:00
parent 8ae0e5fa7e
commit 76bd0abb94
3 changed files with 19 additions and 8 deletions

View File

@@ -26,6 +26,8 @@ type HTTPCacheRef struct {
CachePolicy *HTTPCachePolicy `yaml:"cachePolicy" json:"cachePolicy"`
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否为反向条件,反向条件的不缓存
lifeSeconds int64
maxSize int64
uppercaseSkipCacheControlValues []string

View File

@@ -26,8 +26,9 @@ type HTTPRequestCond struct {
// ${arg.name}, ${requestPath}
Param string `yaml:"param" json:"param"`
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
Value string `yaml:"value" json:"value"` // 对比值
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
Value string `yaml:"value" json:"value"` // 对比值
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
isInt bool
isFloat bool
@@ -136,6 +137,14 @@ func (this *HTTPRequestCond) Init() error {
// Match 将此条件应用于请求,检查是否匹配
func (this *HTTPRequestCond) Match(formatter func(source string) string) bool {
b := this.match(formatter)
if this.IsReverse {
return !b
}
return b
}
func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
paramValue := formatter(this.Param)
switch this.Operator {
case RequestCondOperatorRegexp:

View File

@@ -1,6 +1,6 @@
package shared
// 条件配置
// HTTPRequestCondsConfig 条件配置
// 数据结构conds -> []groups -> []cond
type HTTPRequestCondsConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"`
@@ -11,7 +11,7 @@ type HTTPRequestCondsConfig struct {
hasResponseConds bool
}
// 初始化
// Init 初始化
func (this *HTTPRequestCondsConfig) Init() error {
if len(this.Connector) == 0 {
this.Connector = "or"
@@ -39,7 +39,7 @@ func (this *HTTPRequestCondsConfig) Init() error {
return nil
}
// 判断请求是否匹配
// MatchRequest 判断请求是否匹配
func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string) bool {
if !this.IsOn && len(this.Groups) == 0 {
return true
@@ -61,7 +61,7 @@ func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string
return ok
}
// 判断响应是否匹配
// MatchResponse 判断响应是否匹配
func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) string) bool {
if !this.IsOn && len(this.Groups) == 0 {
return true
@@ -83,12 +83,12 @@ func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) strin
return ok
}
// 判断是否有请求条件
// HasRequestConds 判断是否有请求条件
func (this *HTTPRequestCondsConfig) HasRequestConds() bool {
return this.hasRequestConds
}
// 判断是否有响应条件
// HasResponseConds 判断是否有响应条件
func (this *HTTPRequestCondsConfig) HasResponseConds() bool {
return this.hasResponseConds
}