github.com/argoproj/argo-events@v1.9.1/common/string_keyed_map.go (about)

     1  package common
     2  
     3  import "sync"
     4  
     5  // Concurrent Safe String keyed map
     6  type StringKeyedMap[T any] struct {
     7  	items map[string]T
     8  	lock  *sync.RWMutex
     9  }
    10  
    11  func NewStringKeyedMap[T any]() StringKeyedMap[T] {
    12  	return StringKeyedMap[T]{
    13  		items: make(map[string]T, 0),
    14  		lock:  &sync.RWMutex{},
    15  	}
    16  }
    17  
    18  func (sm *StringKeyedMap[T]) Store(key string, item T) {
    19  	sm.lock.Lock()
    20  	defer sm.lock.Unlock()
    21  	sm.items[key] = item
    22  }
    23  
    24  func (sm *StringKeyedMap[T]) Load(key string) (T, bool) {
    25  	sm.lock.RLock()
    26  	defer sm.lock.RUnlock()
    27  	ok, item := sm.items[key]
    28  	return ok, item
    29  }
    30  
    31  func (sm *StringKeyedMap[T]) Delete(key string) {
    32  	sm.lock.Lock()
    33  	defer sm.lock.Unlock()
    34  	delete(sm.items, key)
    35  }