cache.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/encoding/gbase64"
  12. "github.com/gogf/gf/v2/frame/g"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. "github.com/tiger1103/gfast/v3/api/v1/system"
  15. commonConsts "github.com/tiger1103/gfast/v3/internal/app/common/consts"
  16. "github.com/tiger1103/gfast/v3/internal/app/common/service"
  17. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  18. )
  19. var Cache = new(cacheController)
  20. type cacheController struct {
  21. BaseController
  22. }
  23. func (c *cacheController) Remove(ctx context.Context, req *system.CacheRemoveReq) (res *system.CacheRemoveRes, err error) {
  24. service.Cache().RemoveByTag(ctx, commonConsts.CacheSysDictTag)
  25. service.Cache().RemoveByTag(ctx, commonConsts.CacheSysConfigTag)
  26. service.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag)
  27. cacheRedis := g.Cfg().MustGet(ctx, "system.cache.model").String()
  28. if cacheRedis == commonConsts.CacheModelRedis {
  29. cursor := 0
  30. cachePrefix := g.Cfg().MustGet(ctx, "system.cache.prefix").String()
  31. for {
  32. var v *gvar.Var
  33. v, err = g.Redis().Do(ctx, "scan", cursor, "match", cachePrefix+"*", "count", "100")
  34. if err != nil {
  35. return
  36. }
  37. data := gconv.SliceAny(v)
  38. var dataSlice []string
  39. err = gconv.Structs(data[1], &dataSlice)
  40. if err != nil {
  41. return
  42. }
  43. for _, d := range dataSlice {
  44. dk := gbase64.MustDecodeToString(d)
  45. _, err = g.Redis().Do(ctx, "del", dk)
  46. if err != nil {
  47. return
  48. }
  49. }
  50. cursor = gconv.Int(data[0])
  51. if cursor == 0 {
  52. break
  53. }
  54. }
  55. }
  56. return
  57. }