cache.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package cache_service
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/crypto/gmd5"
  5. "github.com/gogf/gf/os/gcache"
  6. "github.com/gogf/gf/util/gconv"
  7. "reflect"
  8. "sync"
  9. "time"
  10. )
  11. var tagSetMux sync.Mutex
  12. type CacheTagService struct {
  13. tagKey interface{}
  14. }
  15. func New() *CacheTagService {
  16. return &CacheTagService{}
  17. }
  18. //设置tag缓存的keys
  19. func (c *CacheTagService) cacheTagKey(key interface{}, tag interface{}) {
  20. c.setTagKey(tag)
  21. if c.tagKey != nil {
  22. tagValue := []interface{}{key}
  23. value, _ := gcache.Get(c.tagKey)
  24. if value != nil {
  25. keyValue := gconv.SliceAny(value)
  26. for _, v := range keyValue {
  27. if !reflect.DeepEqual(key, v) {
  28. tagValue = append(tagValue, v)
  29. }
  30. }
  31. }
  32. gcache.Set(c.tagKey, tagValue, 0)
  33. }
  34. }
  35. //获取带标签的键名
  36. func (c *CacheTagService) setTagKey(tag interface{}) {
  37. if tag != nil {
  38. c.tagKey = interface{}(fmt.Sprintf("tag_%s", gmd5.MustEncryptString(gconv.String(tag))))
  39. }
  40. }
  41. // Set sets cache with <tagKey>-<value> pair, which is expired after <duration>.
  42. // It does not expire if <duration> <= 0.
  43. func (c *CacheTagService) Set(key interface{}, value interface{}, duration time.Duration, tag interface{}) {
  44. tagSetMux.Lock()
  45. c.cacheTagKey(key, tag)
  46. gcache.Set(key, value, duration)
  47. tagSetMux.Unlock()
  48. }
  49. // SetIfNotExist sets cache with <tagKey>-<value> pair if <tagKey> does not exist in the cache,
  50. // which is expired after <duration>. It does not expire if <duration> <= 0.
  51. func (c *CacheTagService) SetIfNotExist(key interface{}, value interface{}, duration time.Duration, tag interface{}) bool {
  52. tagSetMux.Lock()
  53. defer tagSetMux.Unlock()
  54. c.cacheTagKey(key, tag)
  55. v, _ := gcache.SetIfNotExist(key, value, duration)
  56. return v
  57. }
  58. // Sets batch sets cache with tagKey-value pairs by <data>, which is expired after <duration>.
  59. //
  60. // It does not expire if <duration> <= 0.
  61. func (c *CacheTagService) Sets(data map[interface{}]interface{}, duration time.Duration, tag interface{}) {
  62. tagSetMux.Lock()
  63. if tag != nil {
  64. for k, _ := range data {
  65. c.cacheTagKey(k, tag)
  66. }
  67. gcache.Sets(data, duration)
  68. } else {
  69. gcache.Sets(data, duration)
  70. }
  71. tagSetMux.Unlock()
  72. }
  73. // Get returns the value of <tagKey>.
  74. // It returns nil if it does not exist or its value is nil.
  75. func (c *CacheTagService) Get(key interface{}) interface{} {
  76. v, _ := gcache.Get(key)
  77. return v
  78. }
  79. // GetOrSet returns the value of <tagKey>,
  80. // or sets <tagKey>-<value> pair and returns <value> if <tagKey> does not exist in the cache.
  81. // The tagKey-value pair expires after <duration>.
  82. //
  83. // It does not expire if <duration> <= 0.
  84. func (c *CacheTagService) GetOrSet(key interface{}, value interface{}, duration time.Duration, tag interface{}) interface{} {
  85. tagSetMux.Lock()
  86. defer tagSetMux.Unlock()
  87. c.cacheTagKey(key, tag)
  88. v, _ := gcache.GetOrSet(key, value, duration)
  89. return v
  90. }
  91. // GetOrSetFunc returns the value of <tagKey>, or sets <tagKey> with result of function <f>
  92. // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
  93. // after <duration>. It does not expire if <duration> <= 0.
  94. func (c *CacheTagService) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration, tag interface{}) interface{} {
  95. tagSetMux.Lock()
  96. defer tagSetMux.Unlock()
  97. c.cacheTagKey(key, tag)
  98. v, _ := gcache.GetOrSetFunc(key, f, duration)
  99. return v
  100. }
  101. // GetOrSetFuncLock returns the value of <tagKey>, or sets <tagKey> with result of function <f>
  102. // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
  103. // after <duration>. It does not expire if <duration> <= 0.
  104. //
  105. // Note that the function <f> is executed within writing mutex lock.
  106. func (c *CacheTagService) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration, tag interface{}) interface{} {
  107. tagSetMux.Lock()
  108. defer tagSetMux.Unlock()
  109. c.cacheTagKey(key, tag)
  110. v, _ := gcache.GetOrSetFuncLock(key, f, duration)
  111. return v
  112. }
  113. // Contains returns true if <tagKey> exists in the cache, or else returns false.
  114. func (c *CacheTagService) Contains(key interface{}) bool {
  115. v, _ := gcache.Contains(key)
  116. return v
  117. }
  118. // Remove deletes the <tagKey> in the cache, and returns its value.
  119. func (c *CacheTagService) Remove(key interface{}) interface{} {
  120. v, _ := gcache.Remove(key)
  121. return v
  122. }
  123. // Removes deletes <keys> in the cache.
  124. func (c *CacheTagService) Removes(keys []interface{}) {
  125. gcache.Remove(keys...)
  126. }
  127. // Remove deletes the <tag> in the cache, and returns its value.
  128. func (c *CacheTagService) RemoveByTag(tag interface{}) {
  129. tagSetMux.Lock()
  130. c.setTagKey(tag)
  131. //删除tagKey 对应的 key和值
  132. keys := c.Get(c.tagKey)
  133. if keys != nil {
  134. ks := gconv.SliceAny(keys)
  135. c.Removes(ks)
  136. }
  137. c.Remove(c.tagKey)
  138. tagSetMux.Unlock()
  139. }
  140. // Removes deletes <tags> in the cache.
  141. func (c *CacheTagService) RemoveByTags(tag []interface{}) {
  142. for _, v := range tag {
  143. c.RemoveByTag(v)
  144. }
  145. }
  146. // Data returns a copy of all tagKey-value pairs in the cache as map type.
  147. func (c *CacheTagService) Data() map[interface{}]interface{} {
  148. v, _ := gcache.Data()
  149. return v
  150. }
  151. // Keys returns all keys in the cache as slice.
  152. func (c *CacheTagService) Keys() []interface{} {
  153. v, _ := gcache.Keys()
  154. return v
  155. }
  156. // KeyStrings returns all keys in the cache as string slice.
  157. func (c *CacheTagService) KeyStrings() []string {
  158. v, _ := gcache.KeyStrings()
  159. return v
  160. }
  161. // Values returns all values in the cache as slice.
  162. func (c *CacheTagService) Values() []interface{} {
  163. v, _ := gcache.Values()
  164. return v
  165. }
  166. // Size returns the size of the cache.
  167. func (c *CacheTagService) Size() int {
  168. v, _ := gcache.Size()
  169. return v
  170. }