123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package main
- var _multiTemplate = `
- // NAME {{or .Comment "get data from cache if miss will call source method, then add to cache."}}
- func (d *Dao) NAME(c context.Context, keys []KEY{{.ExtraArgsType}}) (res map[KEY]VALUE, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- if res, err = CACHEFUNC(c, keys {{.ExtraCacheArgs}});err != nil {
- addCache = false
- res = nil
- err = nil
- }
- var miss []KEY
- for _, key := range keys {
- {{if .GoValue}}
- if (res == nil) || (len(res[key]) == 0) {
- {{else}}
- {{if .NumberValue}}
- if _, ok := res[key]; !ok {
- {{else}}
- if (res == nil) || (res[key] == {{.ZeroValue}}) {
- {{end}}
- {{end}}
- miss = append(miss, key)
- }
- }
- prom.CacheHit.Add("NAME", int64(len(keys) - len(miss)))
- {{if .EnableNullCache}}
- for k, v := range res {
- {{if .SimpleValue}} if v == {{.NullCache}} { {{else}} if {{.CheckNullCode}} { {{end}}
- delete(res, k)
- }
- }
- {{end}}
- missLen := len(miss)
- if missLen == 0 {
- return
- }
- {{if .EnableBatch}}
- missData := make(map[KEY]VALUE, missLen)
- {{else}}
- var missData map[KEY]VALUE
- {{end}}
- {{if .EnableSingleFlight}}
- var rr interface{}
- sf := d.cacheSFNAME(keys {{.ExtraArgs}})
- rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) {
- prom.CacheMiss.Add("NAME", int64(len(miss)))
- r, e = RAWFUNC(c, miss {{.ExtraRawArgs}})
- return
- })
- missData = rr.(map[KEY]VALUE)
- {{else}}
- {{if .EnableBatch}}
- prom.CacheMiss.Add("NAME", int64(missLen))
- var mutex sync.Mutex
- {{if .BatchErrBreak}}
- group, ctx := errgroup.WithContext(c)
- {{else}}
- group := &errgroup.Group{}
- ctx := c
- {{end}}
- if missLen > MAXGROUP {
- group.GOMAXPROCS(MAXGROUP)
- }
- var run = func(ms []KEY) {
- group.Go(func() (err error) {
- data, err := RAWFUNC(ctx, ms {{.ExtraRawArgs}})
- mutex.Lock()
- for k, v := range data {
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- var (
- i int
- n = missLen/GROUPSIZE
- )
- for i=0; i< n; i++{
- run(miss[i*n:(i+1)*n])
- }
- if len(miss[i*n:]) > 0 {
- run(miss[i*n:])
- }
- err = group.Wait()
- {{else}}
- prom.CacheMiss.Add("NAME", int64(len(miss)))
- missData, err = RAWFUNC(c, miss {{.ExtraRawArgs}})
- {{end}}
- {{end}}
- if res == nil {
- res = make(map[KEY]VALUE, len(keys))
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- {{if .EnableNullCache}}
- for _, key := range miss {
- {{if .GoValue}}
- if len(res[key]) == 0 {
- {{else}}
- if res[key] == {{.ZeroValue}} {
- {{end}}
- missData[key] = {{.NullCache}}
- }
- }
- {{end}}
- if !addCache {
- return
- }
- {{if .Sync}}
- ADDCACHEFUNC(c, missData {{.ExtraAddCacheArgs}})
- {{else}}
- d.cache.Do(c, func(c context.Context) {
- ADDCACHEFUNC(c, missData {{.ExtraAddCacheArgs}})
- })
- {{end}}
- return
- }
- `
|