MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / MatchBytesCache

Function MatchBytesCache

internal/waf/utils/utils.go:70–97  ·  view source on GitHub ↗

MatchBytesCache 正则表达式匹配字节slice,并缓存结果

(regex *re.Regexp, byteSlice []byte, cacheLife CacheLife)

Source from the content-addressed store, hash-verified

68
69// MatchBytesCache 正则表达式匹配字节slice,并缓存结果
70func MatchBytesCache(regex *re.Regexp, byteSlice []byte, cacheLife CacheLife) bool {
71 if regex == nil {
72 return false
73 }
74
75 var regIdString = regex.IdString()
76
77 // 如果长度超过一定数量,大概率是不能重用的
78 if cacheLife <= 0 || len(byteSlice) > MaxCacheDataSize || !cacheHits.IsGood(regIdString) {
79 return regex.Match(byteSlice)
80 }
81
82 var hash = xxhash.Sum64(byteSlice)
83 var key = regIdString + "@" + strconv.FormatUint(hash, 10)
84 var item = SharedCache.Read(key)
85 if item != nil {
86 cacheHits.IncreaseHit(regIdString)
87 return item.Value == 1
88 }
89 var b = regex.Match(byteSlice)
90 if b {
91 SharedCache.Write(key, 1, fasttime.Now().Unix()+cacheLife)
92 } else {
93 SharedCache.Write(key, 0, fasttime.Now().Unix()+cacheLife)
94 }
95 cacheHits.IncreaseCached(regIdString)
96 return b
97}
98
99// ComposeIPType 组合IP类型
100func ComposeIPType(setId int64, req requests.Request) string {

Callers 2

TestMethod · 0.92
TestMatchBytesCacheFunction · 0.92

Calls 9

NowFunction · 0.92
IdStringMethod · 0.80
IsGoodMethod · 0.80
UnixMethod · 0.80
IncreaseCachedMethod · 0.80
ReadMethod · 0.65
IncreaseHitMethod · 0.65
WriteMethod · 0.65
MatchMethod · 0.45

Tested by 1

TestMatchBytesCacheFunction · 0.74