Skip to content

Commit 6f294f5

Browse files
committed
缓存条件支持反向匹配
1 parent 8889c87 commit 6f294f5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

pkg/serverconfigs/http_cache_ref.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type HTTPCacheRef struct {
2626

2727
CachePolicy *HTTPCachePolicy `yaml:"cachePolicy" json:"cachePolicy"`
2828

29+
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否为反向条件,反向条件的不缓存
30+
2931
lifeSeconds int64
3032
maxSize int64
3133
uppercaseSkipCacheControlValues []string

pkg/serverconfigs/shared/http_request_cond.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type HTTPRequestCond struct {
2626
// ${arg.name}, ${requestPath}
2727
Param string `yaml:"param" json:"param"`
2828

29-
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
30-
Value string `yaml:"value" json:"value"` // 对比值
29+
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
30+
Value string `yaml:"value" json:"value"` // 对比值
31+
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
3132

3233
isInt bool
3334
isFloat bool
@@ -136,6 +137,14 @@ func (this *HTTPRequestCond) Init() error {
136137

137138
// Match 将此条件应用于请求,检查是否匹配
138139
func (this *HTTPRequestCond) Match(formatter func(source string) string) bool {
140+
b := this.match(formatter)
141+
if this.IsReverse {
142+
return !b
143+
}
144+
return b
145+
}
146+
147+
func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
139148
paramValue := formatter(this.Param)
140149
switch this.Operator {
141150
case RequestCondOperatorRegexp:

pkg/serverconfigs/shared/http_request_conds_config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package shared
22

3-
// 条件配置
3+
// HTTPRequestCondsConfig 条件配置
44
// 数据结构:conds -> []groups -> []cond
55
type HTTPRequestCondsConfig struct {
66
IsOn bool `yaml:"isOn" json:"isOn"`
@@ -11,7 +11,7 @@ type HTTPRequestCondsConfig struct {
1111
hasResponseConds bool
1212
}
1313

14-
// 初始化
14+
// Init 初始化
1515
func (this *HTTPRequestCondsConfig) Init() error {
1616
if len(this.Connector) == 0 {
1717
this.Connector = "or"
@@ -39,7 +39,7 @@ func (this *HTTPRequestCondsConfig) Init() error {
3939
return nil
4040
}
4141

42-
// 判断请求是否匹配
42+
// MatchRequest 判断请求是否匹配
4343
func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string) bool {
4444
if !this.IsOn && len(this.Groups) == 0 {
4545
return true
@@ -61,7 +61,7 @@ func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string
6161
return ok
6262
}
6363

64-
// 判断响应是否匹配
64+
// MatchResponse 判断响应是否匹配
6565
func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) string) bool {
6666
if !this.IsOn && len(this.Groups) == 0 {
6767
return true
@@ -83,12 +83,12 @@ func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) strin
8383
return ok
8484
}
8585

86-
// 判断是否有请求条件
86+
// HasRequestConds 判断是否有请求条件
8787
func (this *HTTPRequestCondsConfig) HasRequestConds() bool {
8888
return this.hasRequestConds
8989
}
9090

91-
// 判断是否有响应条件
91+
// HasResponseConds 判断是否有响应条件
9292
func (this *HTTPRequestCondsConfig) HasResponseConds() bool {
9393
return this.hasResponseConds
9494
}

0 commit comments

Comments
 (0)