synced with master

This commit is contained in:
Gani Georgiev 2023-04-12 14:27:17 +03:00
commit 6a69a035a7
2 changed files with 13 additions and 4 deletions

View File

@ -5,6 +5,11 @@
- Fixed typo in `Record.WithUnkownData()` -> `Record.WithUnknownData()`. - Fixed typo in `Record.WithUnkownData()` -> `Record.WithUnknownData()`.
## v0.14.4
- Fixed concurrent map write pannic on `list.ExistInSliceWithRegex()` cache ([#2272](https://github.com/pocketbase/pocketbase/issues/2272)).
## v0.14.3 ## v0.14.3
- Fixed Admin UI Logs `meta` visualization in Firefox ([#2221](https://github.com/pocketbase/pocketbase/issues/2221)). - Fixed Admin UI Logs `meta` visualization in Firefox ([#2221](https://github.com/pocketbase/pocketbase/issues/2221)).

View File

@ -5,10 +5,11 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/pocketbase/pocketbase/tools/store"
"github.com/spf13/cast" "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 // SubtractSlice returns a new slice with only the "base" elements
// that don't exist in "subtract". // that don't exist in "subtract".
@ -56,15 +57,18 @@ func ExistInSliceWithRegex(str string, list []string) bool {
} }
// check for regex match // check for regex match
pattern, ok := cachedPatterns[field] pattern := cachedPatterns.Get(field)
if !ok { if pattern == nil {
var err error var err error
pattern, err = regexp.Compile(field) pattern, err = regexp.Compile(field)
if err != nil { if err != nil {
continue continue
} }
// "cache" the pattern to avoid compiling it every time // "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) { if pattern != nil && pattern.MatchString(str) {