[#2272] fixed panic on list.ExistInSliceWithRegex cache

This commit is contained in:
Gani Georgiev
2023-04-12 10:27:22 +03:00
parent b7407edcae
commit 25c1db40ac
31 changed files with 49 additions and 40 deletions
+8 -4
View File
@@ -5,10 +5,11 @@ import (
"regexp"
"strings"
"github.com/pocketbase/pocketbase/tools/store"
"github.com/spf13/cast"
)
var cachedPatterns = map[string]*regexp.Regexp{}
var cachedPatterns = store.New[*regexp.Regexp](nil)
// SubtractSlice returns a new slice with only the "base" elements
// that don't exist in "subtract".
@@ -56,15 +57,18 @@ func ExistInSliceWithRegex(str string, list []string) bool {
}
// check for regex match
pattern, ok := cachedPatterns[field]
if !ok {
pattern := cachedPatterns.Get(field)
if pattern == nil {
var err error
pattern, err = regexp.Compile(field)
if err != nil {
continue
}
// "cache" the pattern to avoid compiling it every time
cachedPatterns[field] = pattern
// (the limit size is arbitrary and it is there to prevent the cache growing too big)
//
// @todo consider replacing with TTL or LRU type cache
cachedPatterns.SetIfLessThanLimit(field, pattern, 5000)
}
if pattern != nil && pattern.MatchString(str) {