cache.go 521 B

123456789101112131415161718192021
  1. package utility
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func QueryCacheArrMap(ctx context.Context, query func() []map[string]interface{}, key string) []map[string]interface{} {
  7. cacheValue, err := g.Redis().Get(ctx, key)
  8. if err == nil && !cacheValue.IsNil() {
  9. return cacheValue.Maps()
  10. }
  11. res := query()
  12. if res == nil || len(res) == 0 {
  13. res = []map[string]interface{}{}
  14. }
  15. if len(res) > 0 {
  16. _ = g.Redis().SetEX(ctx, key, res, g.Cfg().MustGet(ctx, "cacheSeconds", 60*5).Int64())
  17. }
  18. return res
  19. }