string.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package store
  2. import "sync"
  3. type StringCallCreate func(key string) (value interface{}, created bool)
  4. type StringCallCreateMulti func(key string) (map[string]interface{}, bool)
  5. type StringCallCheck func(key string, value interface{}, exists bool) (rKey string, rValue interface{}, created bool)
  6. func StringFromMap(m map[string]interface{}) *StoreString {
  7. return &StoreString{
  8. locker: new(sync.RWMutex),
  9. items: m,
  10. }
  11. }
  12. func StringNew() *StoreString {
  13. return &StoreString{
  14. locker: new(sync.RWMutex),
  15. items: make(map[string]interface{}),
  16. }
  17. }
  18. type StoreString struct {
  19. locker *sync.RWMutex
  20. items map[string]interface{}
  21. }
  22. func (s *StoreString) delete(key string) {
  23. delete(s.items, key)
  24. }
  25. func (s *StoreString) Delete(key string) {
  26. s.locker.Lock()
  27. delete(s.items, key)
  28. s.locker.Unlock()
  29. }
  30. func (s *StoreString) DeleteMulti(keys []string) {
  31. s.locker.Lock()
  32. for _, key := range keys {
  33. delete(s.items, key)
  34. }
  35. s.locker.Unlock()
  36. }
  37. func (s *StoreString) set(key string, val interface{}) {
  38. s.items[key] = val
  39. }
  40. func (s *StoreString) setMulti(m map[string]interface{}) {
  41. for key, val := range m {
  42. s.set(key, val)
  43. }
  44. }
  45. func (s *StoreString) Set(key string, val interface{}) {
  46. s.locker.Lock()
  47. s.set(key, val)
  48. s.locker.Unlock()
  49. }
  50. func (s *StoreString) SetMulti(m map[string]interface{}) {
  51. s.locker.Lock()
  52. s.setMulti(m)
  53. s.locker.Unlock()
  54. }
  55. func (s *StoreString) get(key string) (val interface{}, check bool) {
  56. val, check = s.items[key]
  57. return
  58. }
  59. func (s *StoreString) Get(key string) (val interface{}, check bool) {
  60. s.locker.RLock()
  61. val, check = s.get(key)
  62. s.locker.RUnlock()
  63. return
  64. }
  65. func (s *StoreString) GetCreate(key string, mCreate StringCallCreate) (res interface{}, check bool) {
  66. if res, check = s.Get(key); !check {
  67. s.locker.Lock()
  68. if res, check = s.get(key); check {
  69. s.locker.Unlock()
  70. return
  71. }
  72. if res, check = mCreate(key); check {
  73. s.set(key, res)
  74. }
  75. s.locker.Unlock()
  76. }
  77. return
  78. }
  79. func (s *StoreString) GetCreateMulti(key string, mCreateMulti StringCallCreateMulti) (res interface{}, check bool) {
  80. if res, check = s.Get(key); !check {
  81. s.locker.Lock()
  82. if res, check = s.get(key); check {
  83. s.locker.Unlock()
  84. return
  85. }
  86. var m map[string]interface{}
  87. if m, check = mCreateMulti(key); check {
  88. s.setMulti(m)
  89. res, check = s.items[key]
  90. }
  91. s.locker.Unlock()
  92. }
  93. return
  94. }
  95. func (s *StoreString) GetCheck(key string, mCheck StringCallCheck) (res interface{}, check bool) {
  96. s.locker.Lock()
  97. res, check = s.get(key)
  98. if rKey, rVal, rCheck := mCheck(key, res, check); rCheck {
  99. s.set(rKey, rVal)
  100. key, res, check = rKey, rVal, true
  101. }
  102. s.locker.Unlock()
  103. return
  104. }
  105. // Each implements a map bypass for each key using the callback function. If the callback function returns false, then the cycle stops
  106. func (s *StoreString) Each(callback func(string, interface{}) bool) {
  107. s.locker.RLock()
  108. for key, val := range s.items {
  109. if !callback(key, val) {
  110. s.locker.RUnlock()
  111. return
  112. }
  113. }
  114. s.locker.RUnlock()
  115. }
  116. func (s *StoreString) Len() (res int) {
  117. s.locker.RLock()
  118. res = len(s.items)
  119. s.locker.RUnlock()
  120. return
  121. }
  122. func (s *StoreString) Keys() (res []string) {
  123. s.locker.RLock()
  124. res = make([]string, 0, len(s.items))
  125. for key := range s.items {
  126. res = append(res, key)
  127. }
  128. s.locker.RUnlock()
  129. return
  130. }
  131. func (s *StoreString) Clear() {
  132. s.locker.Lock()
  133. s.items = make(map[string]interface{})
  134. s.locker.Unlock()
  135. }
  136. func (s *StoreString) Map() (res map[string]interface{}) {
  137. res = make(map[string]interface{})
  138. s.locker.RLock()
  139. for key, val := range s.items {
  140. res[key] = val
  141. }
  142. s.locker.RUnlock()
  143. return
  144. }