You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-07-03 04:56: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
|
// GetOrSet retrieves a single existing value for the provided key
|
||||||
// or stores a new one if it doesn't exist.
|
// or stores a new one if it doesn't exist.
|
||||||
func (s *Store[T]) GetOrSet(key string, setFunc func() T) T {
|
func (s *Store[T]) GetOrSet(key string, setFunc func() T) T {
|
||||||
s.mu.Lock()
|
// lock only reads to minimize locks contention
|
||||||
defer s.mu.Unlock()
|
s.mu.RLock()
|
||||||
|
|
||||||
if s.data == nil {
|
|
||||||
s.data = make(map[string]T)
|
|
||||||
}
|
|
||||||
|
|
||||||
v, ok := s.data[key]
|
v, ok := s.data[key]
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
s.mu.Lock()
|
||||||
v = setFunc()
|
v = setFunc()
|
||||||
|
if s.data == nil {
|
||||||
|
s.data = make(map[string]T)
|
||||||
|
}
|
||||||
s.data[key] = v
|
s.data[key] = v
|
||||||
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
return v
|
return v
|
||||||
|
Reference in New Issue
Block a user