[#2272] fixed panic on list.ExistInSliceWithRegex cache
This commit is contained in:
+8
-4
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user