You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-07-03 12:59:59 +02:00
updated Store.GetOrSet to lock first with RLock/RUnlock
This commit is contained in:
@ -151,17 +151,19 @@ func (s *Store[T]) Set(key string, value T) {
|
||||
// GetOrSet retrieves a single existing value for the provided key
|
||||
// or stores a new one if it doesn't exist.
|
||||
func (s *Store[T]) GetOrSet(key string, setFunc func() T) T {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[string]T)
|
||||
}
|
||||
|
||||
// lock only reads to minimize locks contention
|
||||
s.mu.RLock()
|
||||
v, ok := s.data[key]
|
||||
s.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
s.mu.Lock()
|
||||
v = setFunc()
|
||||
if s.data == nil {
|
||||
s.data = make(map[string]T)
|
||||
}
|
||||
s.data[key] = v
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
return v
|
||||
|
Reference in New Issue
Block a user