dao.cache.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.VipInfoDB{VipType:0} -check_null_code=$!=nil
  7. Infos(c context.Context, keys []int64) (map[int64]*model.VipInfoDB, error)
  8. // cache: -nullcache=&model.VipInfoDB{VipType:0} -check_null_code=$!=nil
  9. Info(c context.Context, key int64) (*model.VipInfoDB, error)
  10. }
  11. */
  12. package dao
  13. import (
  14. "context"
  15. "sync"
  16. "go-common/app/service/main/vipinfo/model"
  17. "go-common/library/net/metadata"
  18. "go-common/library/stat/prom"
  19. "go-common/library/sync/errgroup"
  20. )
  21. var _ _cache
  22. // Infos get data from cache if miss will call source method, then add to cache.
  23. func (d *Dao) Infos(c context.Context, keys []int64) (res map[int64]*model.VipUserInfo, err error) {
  24. if len(keys) == 0 {
  25. return
  26. }
  27. addCache := true
  28. res, err = d.CacheInfos(c, keys)
  29. if err != nil {
  30. addCache = false
  31. res = nil
  32. err = nil
  33. }
  34. var miss []int64
  35. for _, key := range keys {
  36. if (res == nil) || (res[key] == nil) {
  37. miss = append(miss, key)
  38. }
  39. }
  40. prom.CacheHit.Add("Infos", int64(len(keys)-len(miss)))
  41. if len(miss) == 0 {
  42. return
  43. }
  44. var missData map[int64]*model.VipUserInfo
  45. missLen := len(miss)
  46. prom.CacheMiss.Add("Infos", int64(missLen))
  47. mutex := sync.Mutex{}
  48. for i := 0; i < missLen; i += 50 * 10 {
  49. var subKeys []int64
  50. group := &errgroup.Group{}
  51. ctx := c
  52. if (i + 50*10) > missLen {
  53. subKeys = miss[i:]
  54. } else {
  55. subKeys = miss[i : i+50*10]
  56. }
  57. missSubLen := len(subKeys)
  58. for j := 0; j < missSubLen; j += 50 {
  59. var ks []int64
  60. if (j + 50) > missSubLen {
  61. ks = subKeys[j:]
  62. } else {
  63. ks = subKeys[j : j+50]
  64. }
  65. group.Go(func() (err error) {
  66. data, err := d.RawInfos(ctx, ks)
  67. mutex.Lock()
  68. for k, v := range data {
  69. if missData == nil {
  70. missData = make(map[int64]*model.VipUserInfo, len(keys))
  71. }
  72. missData[k] = v
  73. }
  74. mutex.Unlock()
  75. return
  76. })
  77. }
  78. err1 := group.Wait()
  79. if err1 != nil {
  80. err = err1
  81. }
  82. }
  83. if res == nil {
  84. res = make(map[int64]*model.VipUserInfo)
  85. }
  86. for k, v := range missData {
  87. res[k] = v
  88. }
  89. if err != nil {
  90. return
  91. }
  92. for _, key := range keys {
  93. if res[key] == nil {
  94. if missData == nil {
  95. missData = make(map[int64]*model.VipUserInfo, len(keys))
  96. }
  97. missData[key] = &model.VipUserInfo{VipType: 0, VipStatus: 0}
  98. res[key] = missData[key]
  99. }
  100. }
  101. if !addCache {
  102. return
  103. }
  104. d.cache.Save(func() {
  105. d.AddCacheInfos(metadata.WithContext(c), missData)
  106. })
  107. return
  108. }
  109. // Info get data from cache if miss will call source method, then add to cache.
  110. func (d *Dao) Info(c context.Context, id int64) (res *model.VipUserInfo, err error) {
  111. addCache := true
  112. res, err = d.CacheInfo(c, id)
  113. if err != nil {
  114. addCache = false
  115. err = nil
  116. }
  117. if res != nil {
  118. prom.CacheHit.Incr("Info")
  119. return
  120. }
  121. prom.CacheMiss.Incr("Info")
  122. res, err = d.RawInfo(c, id)
  123. if err != nil {
  124. return
  125. }
  126. if res == nil {
  127. res = &model.VipUserInfo{VipType: 0, VipStatus: 0}
  128. }
  129. if !addCache {
  130. return
  131. }
  132. d.cache.Save(func() {
  133. d.AddCacheInfo(metadata.WithContext(c), id, res)
  134. })
  135. return
  136. }