concurrent_read_map.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. 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.Lock()
  16. defer m.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.RLock()
  26. if value, ok := m.items[key]; ok {
  27. m.RUnlock()
  28. return value
  29. }
  30. m.RUnlock()
  31. return m.initMapEntry(key, newEntry)
  32. }
  33. func (m *ConcurrentReadMap) Find(key string) (interface{}, bool) {
  34. m.RLock()
  35. value, ok := m.items[key]
  36. m.RUnlock()
  37. return value, ok
  38. }
  39. func (m *ConcurrentReadMap) Items() (itemsCopy []interface{}) {
  40. m.RLock()
  41. for _, i := range m.items {
  42. itemsCopy = append(itemsCopy, i)
  43. }
  44. m.RUnlock()
  45. return itemsCopy
  46. }
  47. func (m *ConcurrentReadMap) Delete(key string) {
  48. m.Lock()
  49. delete(m.items, key)
  50. m.Unlock()
  51. }