store.go 3.4 KB

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