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
817 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. Items map[string]interface{}
  10. }
  11. func NewConcurrentReadMap() *ConcurrentReadMap {
  12. return &ConcurrentReadMap{Items: make(map[string]interface{})}
  13. }
  14. func (m *ConcurrentReadMap) initMapEntry(key string, newEntry func() interface{}) (value interface{}) {
  15. m.rmutex.Lock()
  16. defer m.rmutex.Unlock()
  17. if value, ok := m.Items[key]; ok {
  18. return value
  19. }
  20. value = newEntry()
  21. m.Items[key] = value
  22. return value
  23. }
  24. func (m *ConcurrentReadMap) Get(key string, newEntry func() interface{}) interface{} {
  25. m.rmutex.RLock()
  26. if value, ok := m.Items[key]; ok {
  27. m.rmutex.RUnlock()
  28. return value
  29. }
  30. m.rmutex.RUnlock()
  31. return m.initMapEntry(key, newEntry)
  32. }