MatchBytesCache 正则表达式匹配字节slice,并缓存结果
(regex *re.Regexp, byteSlice []byte, cacheLife CacheLife)
| 68 | |
| 69 | // MatchBytesCache 正则表达式匹配字节slice,并缓存结果 |
| 70 | func 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类型 |
| 100 | func ComposeIPType(setId int64, req requests.Request) string { |