cache.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * @desc:缓存处理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu<yxh669@qq.com>
  5. * @Date: 2023/2/1 18:14
  6. */
  7. package controller
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/container/gvar"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. "github.com/tiger1103/gfast/v3/api/v1/system"
  14. commonConsts "github.com/tiger1103/gfast/v3/internal/app/common/consts"
  15. "github.com/tiger1103/gfast/v3/internal/app/common/service"
  16. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  17. )
  18. var Cache = new(cacheController)
  19. type cacheController struct {
  20. BaseController
  21. }
  22. func (c *cacheController) Remove(ctx context.Context, req *system.CacheRemoveReq) (res *system.CacheRemoveRes, err error) {
  23. service.Cache().RemoveByTag(ctx, commonConsts.CacheSysDictTag)
  24. service.Cache().RemoveByTag(ctx, commonConsts.CacheSysConfigTag)
  25. service.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag)
  26. cacheRedis := g.Cfg().MustGet(ctx, "system.cache.model").String()
  27. if cacheRedis == commonConsts.CacheModelRedis {
  28. cursor := 0
  29. cachePrefix := g.Cfg().MustGet(ctx, "system.cache.prefix").String()
  30. for {
  31. var v *gvar.Var
  32. v, err = g.Redis().Do(ctx, "scan", cursor, "match", cachePrefix+"*", "count", "100")
  33. if err != nil {
  34. return
  35. }
  36. data := gconv.SliceAny(v)
  37. var dataSlice []string
  38. err = gconv.Structs(data[1], &dataSlice)
  39. if err != nil {
  40. return
  41. }
  42. for _, d := range dataSlice {
  43. _, err = g.Redis().Do(ctx, "del", d)
  44. if err != nil {
  45. return
  46. }
  47. }
  48. cursor = gconv.Int(data[0])
  49. if cursor == 0 {
  50. break
  51. }
  52. }
  53. }
  54. return
  55. }