You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
834 B

  1. package util
  2. import (
  3. "sync"
  4. )
  5. // A mostly for read map, which can thread-safely
  6. // initialize the map entries.
  7. type ConcurrentReadMap struct {
  8. rmutex sync.RWMutex
  9. mutex sync.Mutex
  10. Items map[string]interface{}
  11. }
  12. func NewConcurrentReadMap() *ConcurrentReadMap {
  13. return &ConcurrentReadMap{Items: make(map[string]interface{})}
  14. }
  15. func (m *ConcurrentReadMap) initMapEntry(key string, newEntry func() interface{}) (value interface{}) {
  16. m.mutex.Lock()
  17. defer m.mutex.Unlock()
  18. if value, ok := m.Items[key]; ok {
  19. return value
  20. }
  21. value = newEntry()
  22. m.Items[key] = value
  23. return value
  24. }
  25. func (m *ConcurrentReadMap) Get(key string, newEntry func() interface{}) interface{} {
  26. m.rmutex.RLock()
  27. if value, ok := m.Items[key]; ok {
  28. m.rmutex.RUnlock()
  29. return value
  30. }
  31. m.rmutex.RUnlock()
  32. return m.initMapEntry(key, newEntry)
  33. }