mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-06 07:45:47 +08:00
ttlcache支持泛型
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/re"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
|
||||
"github.com/cespare/xxhash"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var cache = ttlcache.NewCache()
|
||||
var cache = ttlcache.NewCache[int8]()
|
||||
|
||||
// MatchStringCache 正则表达式匹配字符串,并缓存结果
|
||||
func MatchStringCache(regex *re.Regexp, s string) bool {
|
||||
@@ -26,7 +25,7 @@ func MatchStringCache(regex *re.Regexp, s string) bool {
|
||||
var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10)
|
||||
var item = cache.Read(key)
|
||||
if item != nil {
|
||||
return types.Int8(item.Value) == 1
|
||||
return item.Value == 1
|
||||
}
|
||||
var b = regex.MatchString(s)
|
||||
if b {
|
||||
@@ -52,10 +51,10 @@ func MatchBytesCache(regex *re.Regexp, byteSlice []byte) bool {
|
||||
var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10)
|
||||
var item = cache.Read(key)
|
||||
if item != nil {
|
||||
return types.Int8(item.Value) == 1
|
||||
return item.Value == 1
|
||||
}
|
||||
if item != nil {
|
||||
return types.Int8(item.Value) == 1
|
||||
return item.Value == 1
|
||||
}
|
||||
var b = regex.Match(byteSlice)
|
||||
if b {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package utils
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/re"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"runtime"
|
||||
@@ -13,16 +14,16 @@ import (
|
||||
|
||||
func TestMatchStringCache(t *testing.T) {
|
||||
regex := re.MustCompile(`\d+`)
|
||||
t.Log(MatchStringCache(regex, "123"))
|
||||
t.Log(MatchStringCache(regex, "123"))
|
||||
t.Log(MatchStringCache(regex, "123"))
|
||||
t.Log(utils.MatchStringCache(regex, "123"))
|
||||
t.Log(utils.MatchStringCache(regex, "123"))
|
||||
t.Log(utils.MatchStringCache(regex, "123"))
|
||||
}
|
||||
|
||||
func TestMatchBytesCache(t *testing.T) {
|
||||
regex := re.MustCompile(`\d+`)
|
||||
t.Log(MatchBytesCache(regex, []byte("123")))
|
||||
t.Log(MatchBytesCache(regex, []byte("123")))
|
||||
t.Log(MatchBytesCache(regex, []byte("123")))
|
||||
t.Log(utils.MatchBytesCache(regex, []byte("123")))
|
||||
t.Log(utils.MatchBytesCache(regex, []byte("123")))
|
||||
t.Log(utils.MatchBytesCache(regex, []byte("123")))
|
||||
}
|
||||
|
||||
func TestMatchRemoteCache(t *testing.T) {
|
||||
@@ -55,10 +56,10 @@ func BenchmarkMatchStringCache(b *testing.B) {
|
||||
var data = strings.Repeat("HELLO", 512)
|
||||
var regex = re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
|
||||
//b.Log(regex.Keywords())
|
||||
_ = MatchStringCache(regex, data)
|
||||
_ = utils.MatchStringCache(regex, data)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = MatchStringCache(regex, data)
|
||||
_ = utils.MatchStringCache(regex, data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user