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.

37 lines
841 B

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