package service import ( "context" "dashoo.cn/micro/app/common/global" "fmt" "github.com/gogf/gcache-adapter/adapter" "github.com/gogf/gf/crypto/gmd5" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gcache" "github.com/gogf/gf/util/gconv" "reflect" "sync" "time" ) type cache struct { } type cacheTagService struct { tagKey interface{} cache *gcache.Cache tagSetMux *sync.Mutex } var ( Cache = new(cache) userRedis = g.Cfg().GetBool("redis.open") gChe = gcache.New() ) func (s *cache) New() *cacheTagService { gChe.Ctx(context.Background()) if userRedis { adapter := adapter.NewRedis(g.Redis()) gChe.SetAdapter(adapter) } return &cacheTagService{ cache: gChe, tagSetMux: new(sync.Mutex), } } // 设置tag缓存的keys func (c *cacheTagService) cacheTagKey(key interface{}, tag interface{}) { c.setTagKey(tag) if c.tagKey != nil { tagValue := []interface{}{key} value, _ := c.cache.Get(c.tagKey) if value != nil { var keyValue []interface{} //若是字符串 if kStr, ok := value.(string); ok { js, err := gjson.DecodeToJson(kStr) if err != nil { g.Log().Error(err) return } keyValue = gconv.SliceAny(js.Value()) } else { keyValue = gconv.SliceAny(value) } for _, v := range keyValue { if !reflect.DeepEqual(key, v) { tagValue = append(tagValue, v) } } } c.cache.Set(c.tagKey, tagValue, 0) } } // 获取带标签的键名 func (c *cacheTagService) setTagKey(tag interface{}) { if tag != nil { c.tagKey = interface{}(fmt.Sprintf("%s_tag_%s", global.CachePrefix, gmd5.MustEncryptString(gconv.String(tag)))) } } // Set sets cache with - pair, which is expired after . // It does not expire if <= 0. func (c *cacheTagService) Set(key interface{}, value interface{}, duration time.Duration, tag ...interface{}) { c.tagSetMux.Lock() if len(tag) > 0 { c.cacheTagKey(key, tag[0]) } err := c.cache.Set(key, value, duration) if err != nil { g.Log().Error(err) } c.tagSetMux.Unlock() } // SetIfNotExist sets cache with - pair if does not exist in the cache, // which is expired after . It does not expire if <= 0. func (c *cacheTagService) SetIfNotExist(key interface{}, value interface{}, duration time.Duration, tag interface{}) bool { c.tagSetMux.Lock() defer c.tagSetMux.Unlock() c.cacheTagKey(key, tag) v, _ := c.cache.SetIfNotExist(key, value, duration) return v } // Sets batch sets cache with tagKey-value pairs by , which is expired after . // // It does not expire if <= 0. func (c *cacheTagService) Sets(data map[interface{}]interface{}, duration time.Duration, tag interface{}) { c.tagSetMux.Lock() if tag != nil { for k, _ := range data { c.cacheTagKey(k, tag) } c.cache.Sets(data, duration) } else { c.cache.Sets(data, duration) } c.tagSetMux.Unlock() } // Get returns the value of . // It returns nil if it does not exist or its value is nil. func (c *cacheTagService) Get(key interface{}) interface{} { v, err := c.cache.Get(key) if err != nil { g.Log().Error(err) } return v } // GetOrSet returns the value of , // or sets - pair and returns if does not exist in the cache. // The tagKey-value pair expires after . // // It does not expire if <= 0. func (c *cacheTagService) GetOrSet(key interface{}, value interface{}, duration time.Duration, tag interface{}) interface{} { c.tagSetMux.Lock() defer c.tagSetMux.Unlock() c.cacheTagKey(key, tag) v, _ := c.cache.GetOrSet(key, value, duration) return v } // GetOrSetFunc returns the value of , or sets with result of function // and returns its result if does not exist in the cache. The tagKey-value pair expires // after . It does not expire if <= 0. func (c *cacheTagService) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration, tag interface{}) interface{} { c.tagSetMux.Lock() defer c.tagSetMux.Unlock() c.cacheTagKey(key, tag) v, _ := c.cache.GetOrSetFunc(key, f, duration) return v } // GetOrSetFuncLock returns the value of , or sets with result of function // and returns its result if does not exist in the cache. The tagKey-value pair expires // after . It does not expire if <= 0. // // Note that the function is executed within writing mutex lock. func (c *cacheTagService) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration, tag interface{}) interface{} { c.tagSetMux.Lock() defer c.tagSetMux.Unlock() c.cacheTagKey(key, tag) v, _ := c.cache.GetOrSetFuncLock(key, f, duration) return v } // Contains returns true if exists in the cache, or else returns false. func (c *cacheTagService) Contains(key interface{}) bool { v, _ := c.cache.Contains(key) return v } // Remove deletes the in the cache, and returns its value. func (c *cacheTagService) Remove(key interface{}) interface{} { v, _ := c.cache.Remove(key) return v } // Removes deletes in the cache. func (c *cacheTagService) Removes(keys []interface{}) { c.cache.Remove(keys...) } // Remove deletes the in the cache, and returns its value. func (c *cacheTagService) RemoveByTag(tag interface{}) { c.tagSetMux.Lock() c.setTagKey(tag) //删除tagKey 对应的 key和值 keys := c.Get(c.tagKey) if keys != nil { //如果是字符串 if kStr, ok := keys.(string); ok { js, err := gjson.DecodeToJson(kStr) if err != nil { g.Log().Error(err) return } ks := gconv.SliceAny(js.Value()) c.Removes(ks) } else { ks := gconv.SliceAny(keys) c.Removes(ks) } } c.Remove(c.tagKey) c.tagSetMux.Unlock() } // Removes deletes in the cache. func (c *cacheTagService) RemoveByTags(tag []interface{}) { for _, v := range tag { c.RemoveByTag(v) } } // Data returns a copy of all tagKey-value pairs in the cache as map type. func (c *cacheTagService) Data() map[interface{}]interface{} { v, _ := c.cache.Data() return v } // Keys returns all keys in the cache as slice. func (c *cacheTagService) Keys() []interface{} { v, _ := c.cache.Keys() return v } // KeyStrings returns all keys in the cache as string slice. func (c *cacheTagService) KeyStrings() []string { v, _ := c.cache.KeyStrings() return v } // Values returns all values in the cache as slice. func (c *cacheTagService) Values() []interface{} { v, _ := c.cache.Values() return v } // Size returns the size of the cache. func (c *cacheTagService) Size() int { v, _ := c.cache.Size() return v }