cache.go 5.1 KB

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