dao.cache.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package dao is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // cache: -batch=50 -max_group=10 -batch_err=continue -nullcache=&model.UserEquip{CardID:-1} -check_null_code=$!=nil&&$.CardID==-1
  7. Equips(c context.Context, keys []int64) (map[int64]*model.UserEquip, error)
  8. // cache: -nullcache=&model.UserEquip{CardID:-1} -check_null_code=$!=nil&&$.CardID==-1 -singleflight=true
  9. Equip(c context.Context, key int64) (*model.UserEquip, error)
  10. }
  11. */
  12. package dao
  13. import (
  14. "context"
  15. "sync"
  16. "go-common/app/service/main/card/model"
  17. "go-common/library/net/metadata"
  18. "go-common/library/stat/prom"
  19. "go-common/library/sync/errgroup"
  20. "golang.org/x/sync/singleflight"
  21. )
  22. var _ _cache
  23. var cacheSingleFlights = [1]*singleflight.Group{{}}
  24. // Equips get data from cache if miss will call source method, then add to cache.
  25. func (d *Dao) Equips(c context.Context, keys []int64) (res map[int64]*model.UserEquip, err error) {
  26. if len(keys) == 0 {
  27. return
  28. }
  29. addCache := true
  30. res, err = d.CacheEquips(c, keys)
  31. if err != nil {
  32. addCache = false
  33. res = nil
  34. err = nil
  35. }
  36. var miss []int64
  37. for _, key := range keys {
  38. if (res == nil) || (res[key] == nil) {
  39. miss = append(miss, key)
  40. }
  41. }
  42. prom.CacheHit.Add("Equips", int64(len(keys)-len(miss)))
  43. defer func() {
  44. for k, v := range res {
  45. if v != nil && v.CardID == -1 {
  46. delete(res, k)
  47. }
  48. }
  49. }()
  50. if len(miss) == 0 {
  51. return
  52. }
  53. var missData map[int64]*model.UserEquip
  54. missLen := len(miss)
  55. prom.CacheMiss.Add("Equips", int64(missLen))
  56. mutex := sync.Mutex{}
  57. for i := 0; i < missLen; i += 50 * 10 {
  58. var subKeys []int64
  59. group := &errgroup.Group{}
  60. ctx := c
  61. if (i + 50*10) > missLen {
  62. subKeys = miss[i:]
  63. } else {
  64. subKeys = miss[i : i+50*10]
  65. }
  66. missSubLen := len(subKeys)
  67. for j := 0; j < missSubLen; j += 50 {
  68. var ks []int64
  69. if (j + 50) > missSubLen {
  70. ks = subKeys[j:]
  71. } else {
  72. ks = subKeys[j : j+50]
  73. }
  74. group.Go(func() (err error) {
  75. data, err := d.RawEquips(ctx, ks)
  76. mutex.Lock()
  77. for k, v := range data {
  78. if missData == nil {
  79. missData = make(map[int64]*model.UserEquip, len(keys))
  80. }
  81. missData[k] = v
  82. }
  83. mutex.Unlock()
  84. return
  85. })
  86. }
  87. err1 := group.Wait()
  88. if err1 != nil {
  89. err = err1
  90. }
  91. }
  92. if res == nil {
  93. res = make(map[int64]*model.UserEquip)
  94. }
  95. for k, v := range missData {
  96. res[k] = v
  97. }
  98. if err != nil {
  99. return
  100. }
  101. for _, key := range keys {
  102. if res[key] == nil {
  103. if missData == nil {
  104. missData = make(map[int64]*model.UserEquip, len(keys))
  105. }
  106. missData[key] = &model.UserEquip{CardID: -1}
  107. }
  108. }
  109. if !addCache {
  110. return
  111. }
  112. d.cache.Save(func() {
  113. d.AddCacheEquips(metadata.WithContext(c), missData)
  114. })
  115. return
  116. }
  117. // Equip get data from cache if miss will call source method, then add to cache.
  118. func (d *Dao) Equip(c context.Context, id int64) (res *model.UserEquip, err error) {
  119. addCache := true
  120. res, err = d.CacheEquip(c, id)
  121. if err != nil {
  122. addCache = false
  123. err = nil
  124. }
  125. defer func() {
  126. if res != nil && res.CardID == -1 {
  127. res = nil
  128. }
  129. }()
  130. if res != nil {
  131. prom.CacheHit.Incr("Equip")
  132. return
  133. }
  134. var rr interface{}
  135. sf := d.cacheSFEquip(id)
  136. rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
  137. prom.CacheMiss.Incr("Equip")
  138. r, e = d.RawEquip(c, id)
  139. return
  140. })
  141. res = rr.(*model.UserEquip)
  142. if err != nil {
  143. return
  144. }
  145. miss := res
  146. if miss == nil {
  147. miss = &model.UserEquip{CardID: -1}
  148. }
  149. if !addCache {
  150. return
  151. }
  152. d.cache.Save(func() {
  153. d.AddCacheEquip(metadata.WithContext(c), id, miss)
  154. })
  155. return
  156. }