string.go 3.6 KB

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