cache.go 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * @desc:缓存处理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/3/9 11:15
  6. */
  7. package service
  8. import (
  9. "github.com/gogf/gf/v2/frame/g"
  10. "github.com/gogf/gf/v2/os/gctx"
  11. "github.com/tiger1103/gfast-cache/cache"
  12. "github.com/tiger1103/gfast/v3/internal/app/common/consts"
  13. "sync"
  14. )
  15. type ICache interface {
  16. cache.IGCache
  17. }
  18. type cacheImpl struct {
  19. *cache.GfCache
  20. prefix string
  21. }
  22. var (
  23. c = cacheImpl{}
  24. cacheContainer *cache.GfCache
  25. lock = &sync.Mutex{}
  26. )
  27. func Cache() ICache {
  28. var (
  29. ch = c
  30. ctx = gctx.New()
  31. )
  32. prefix := g.Cfg().MustGet(ctx, "system.cache.prefix").String()
  33. model := g.Cfg().MustGet(ctx, "system.cache.model").String()
  34. if cacheContainer == nil {
  35. lock.Lock()
  36. if cacheContainer == nil {
  37. if model == consts.CacheModelRedis {
  38. // redis
  39. cacheContainer = cache.NewRedis(prefix)
  40. } else {
  41. // memory
  42. cacheContainer = cache.New(prefix)
  43. }
  44. }
  45. lock.Unlock()
  46. }
  47. ch.GfCache = cacheContainer
  48. return &ch
  49. }