cache.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. return gcache.SetIfNotExist(key, value, duration)
  56. }
  57. // Sets batch sets cache with tagKey-value pairs by <data>, which is expired after <duration>.
  58. //
  59. // It does not expire if <duration> <= 0.
  60. func (c *CacheTagService) Sets(data map[interface{}]interface{}, duration time.Duration, tag interface{}) {
  61. tagSetMux.Lock()
  62. if tag != nil {
  63. for k, _ := range data {
  64. c.cacheTagKey(k, tag)
  65. }
  66. gcache.Sets(data, duration)
  67. } else {
  68. gcache.Sets(data, duration)
  69. }
  70. tagSetMux.Unlock()
  71. }
  72. // Get returns the value of <tagKey>.
  73. // It returns nil if it does not exist or its value is nil.
  74. func (c *CacheTagService) Get(key interface{}) interface{} {
  75. return gcache.Get(key)
  76. }
  77. // GetOrSet returns the value of <tagKey>,
  78. // or sets <tagKey>-<value> pair and returns <value> if <tagKey> does not exist in the cache.
  79. // The tagKey-value pair expires after <duration>.
  80. //
  81. // It does not expire if <duration> <= 0.
  82. func (c *CacheTagService) GetOrSet(key interface{}, value interface{}, duration time.Duration, tag interface{}) interface{} {
  83. tagSetMux.Lock()
  84. defer tagSetMux.Unlock()
  85. c.cacheTagKey(key, tag)
  86. return gcache.GetOrSet(key, value, duration)
  87. }
  88. // GetOrSetFunc returns the value of <tagKey>, or sets <tagKey> with result of function <f>
  89. // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
  90. // after <duration>. It does not expire if <duration> <= 0.
  91. func (c *CacheTagService) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} {
  92. tagSetMux.Lock()
  93. defer tagSetMux.Unlock()
  94. c.cacheTagKey(key, tag)
  95. return gcache.GetOrSetFunc(key, f, duration)
  96. }
  97. // GetOrSetFuncLock returns the value of <tagKey>, or sets <tagKey> with result of function <f>
  98. // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
  99. // after <duration>. It does not expire if <duration> <= 0.
  100. //
  101. // Note that the function <f> is executed within writing mutex lock.
  102. func (c *CacheTagService) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} {
  103. tagSetMux.Lock()
  104. defer tagSetMux.Unlock()
  105. c.cacheTagKey(key, tag)
  106. return gcache.GetOrSetFuncLock(key, f, duration)
  107. }
  108. // Contains returns true if <tagKey> exists in the cache, or else returns false.
  109. func (c *CacheTagService) Contains(key interface{}) bool {
  110. return gcache.Contains(key)
  111. }
  112. // Remove deletes the <tagKey> in the cache, and returns its value.
  113. func (c *CacheTagService) Remove(key interface{}) interface{} {
  114. return gcache.Remove(key)
  115. }
  116. // Removes deletes <keys> in the cache.
  117. func (c *CacheTagService) Removes(keys []interface{}) {
  118. gcache.Removes(keys)
  119. }
  120. // Remove deletes the <tag> in the cache, and returns its value.
  121. func (c *CacheTagService) RemoveByTag(tag interface{}) {
  122. tagSetMux.Lock()
  123. c.setTagKey(tag)
  124. //删除tagKey 对应的 key和值
  125. keys := c.Get(c.tagKey)
  126. if keys != nil {
  127. ks := gconv.SliceAny(keys)
  128. c.Removes(ks)
  129. }
  130. c.Remove(c.tagKey)
  131. tagSetMux.Unlock()
  132. }
  133. // Removes deletes <tags> in the cache.
  134. func (c *CacheTagService) RemoveByTags(tag []interface{}) {
  135. for _, v := range tag {
  136. c.RemoveByTag(v)
  137. }
  138. }
  139. // Data returns a copy of all tagKey-value pairs in the cache as map type.
  140. func (c *CacheTagService) Data() map[interface{}]interface{} {
  141. return gcache.Data()
  142. }
  143. // Keys returns all keys in the cache as slice.
  144. func (c *CacheTagService) Keys() []interface{} {
  145. return gcache.Keys()
  146. }
  147. // KeyStrings returns all keys in the cache as string slice.
  148. func (c *CacheTagService) KeyStrings() []string {
  149. return gcache.KeyStrings()
  150. }
  151. // Values returns all values in the cache as slice.
  152. func (c *CacheTagService) Values() []interface{} {
  153. return gcache.Values()
  154. }
  155. // Size returns the size of the cache.
  156. func (c *CacheTagService) Size() int {
  157. return gcache.Size()
  158. }