| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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 <tagKey>-<value> pair, which is expired after <duration>.
- // It does not expire if <duration> <= 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 <tagKey>-<value> pair if <tagKey> does not exist in the cache,
- // which is expired after <duration>. It does not expire if <duration> <= 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 <data>, which is expired after <duration>.
- //
- // It does not expire if <duration> <= 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 <tagKey>.
- // 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 <tagKey>,
- // or sets <tagKey>-<value> pair and returns <value> if <tagKey> does not exist in the cache.
- // The tagKey-value pair expires after <duration>.
- //
- // It does not expire if <duration> <= 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 <tagKey>, or sets <tagKey> with result of function <f>
- // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
- // after <duration>. It does not expire if <duration> <= 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 <tagKey>, or sets <tagKey> with result of function <f>
- // and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
- // after <duration>. It does not expire if <duration> <= 0.
- //
- // Note that the function <f> 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 <tagKey> 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 <tagKey> in the cache, and returns its value.
- func (c *cacheTagService) Remove(key interface{}) interface{} {
- v, _ := c.cache.Remove(key)
- return v
- }
- // Removes deletes <keys> in the cache.
- func (c *cacheTagService) Removes(keys []interface{}) {
- c.cache.Remove(keys...)
- }
- // Remove deletes the <tag> 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 <tags> 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
- }
|