123456789101112131415161718192021 |
- package utility
- import (
- "context"
- "github.com/gogf/gf/v2/frame/g"
- )
- func QueryCacheArrMap(ctx context.Context, query func() []map[string]interface{}, key string) []map[string]interface{} {
- cacheValue, err := g.Redis().Get(ctx, key)
- if err == nil && !cacheValue.IsNil() {
- return cacheValue.Maps()
- }
- res := query()
- if res == nil || len(res) == 0 {
- res = []map[string]interface{}{}
- }
- if len(res) > 0 {
- _ = g.Redis().SetEX(ctx, key, res, g.Cfg().MustGet(ctx, "cacheSeconds", 60*5).Int64())
- }
- return res
- }
|