dao.cache.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package manager is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // cache: -nullcache=&upgrpc.UpSpecial{GroupIDs:[]int64{-1}} -check_null_code=$!=nil&&len($.GroupIDs)>0&&$.GroupIDs[0]==-1
  7. UpSpecial(c context.Context, mid int64) (us *upgrpc.UpSpecial, err error)
  8. // cache: -batch=100 -max_group=1 -batch_err=break -nullcache=&upgrpc.UpSpecial{GroupIDs:[]int64{-1}} -check_null_code=$!=nil&&len($.GroupIDs)>0&&$.GroupIDs[0]==-1
  9. UpsSpecial(c context.Context, mids []int64) (map[int64]*upgrpc.UpSpecial, error)
  10. }
  11. */
  12. package manager
  13. import (
  14. "context"
  15. "sync"
  16. upgrpc "go-common/app/service/main/up/api/v1"
  17. "go-common/library/stat/prom"
  18. "go-common/library/sync/errgroup"
  19. )
  20. var _ _cache
  21. // UpSpecial get data from cache if miss will call source method, then add to cache.
  22. func (d *Dao) UpSpecial(c context.Context, id int64) (res *upgrpc.UpSpecial, err error) {
  23. addCache := true
  24. res, err = d.CacheUpSpecial(c, id)
  25. if err != nil {
  26. addCache = false
  27. err = nil
  28. }
  29. defer func() {
  30. if res != nil && len(res.GroupIDs) > 0 && res.GroupIDs[0] == -1 {
  31. res = nil
  32. }
  33. }()
  34. if res != nil {
  35. prom.CacheHit.Incr("UpSpecial")
  36. return
  37. }
  38. prom.CacheMiss.Incr("UpSpecial")
  39. res, err = d.RawUpSpecial(c, id)
  40. if err != nil {
  41. return
  42. }
  43. miss := res
  44. if miss == nil {
  45. miss = &upgrpc.UpSpecial{GroupIDs: []int64{-1}}
  46. }
  47. if !addCache {
  48. return
  49. }
  50. d.cache.Do(c, func(c context.Context) {
  51. d.AddCacheUpSpecial(c, id, miss)
  52. })
  53. return
  54. }
  55. // UpsSpecial get data from cache if miss will call source method, then add to cache.
  56. func (d *Dao) UpsSpecial(c context.Context, keys []int64) (res map[int64]*upgrpc.UpSpecial, err error) {
  57. if len(keys) == 0 {
  58. return
  59. }
  60. addCache := true
  61. if res, err = d.CacheUpsSpecial(c, keys); err != nil {
  62. addCache = false
  63. res = nil
  64. err = nil
  65. }
  66. var miss []int64
  67. for _, key := range keys {
  68. if (res == nil) || (res[key] == nil) {
  69. miss = append(miss, key)
  70. }
  71. }
  72. prom.CacheHit.Add("UpsSpecial", int64(len(keys)-len(miss)))
  73. for k, v := range res {
  74. if v != nil && len(v.GroupIDs) > 0 && v.GroupIDs[0] == -1 {
  75. delete(res, k)
  76. }
  77. }
  78. missLen := len(miss)
  79. if missLen == 0 {
  80. return
  81. }
  82. missData := make(map[int64]*upgrpc.UpSpecial, missLen)
  83. prom.CacheMiss.Add("UpsSpecial", int64(missLen))
  84. var mutex sync.Mutex
  85. group, ctx := errgroup.WithContext(c)
  86. if missLen > 1 {
  87. group.GOMAXPROCS(1)
  88. }
  89. var run = func(ms []int64) {
  90. group.Go(func() (err error) {
  91. data, err := d.RawUpsSpecial(ctx, ms)
  92. mutex.Lock()
  93. for k, v := range data {
  94. missData[k] = v
  95. }
  96. mutex.Unlock()
  97. return
  98. })
  99. }
  100. var (
  101. i int
  102. n = missLen / 100
  103. )
  104. for i = 0; i < n; i++ {
  105. run(miss[i*n : (i+1)*n])
  106. }
  107. if len(miss[i*n:]) > 0 {
  108. run(miss[i*n:])
  109. }
  110. err = group.Wait()
  111. if res == nil {
  112. res = make(map[int64]*upgrpc.UpSpecial, len(keys))
  113. }
  114. for k, v := range missData {
  115. res[k] = v
  116. }
  117. if err != nil {
  118. return
  119. }
  120. for _, key := range miss {
  121. if res[key] == nil {
  122. missData[key] = &upgrpc.UpSpecial{GroupIDs: []int64{-1}}
  123. }
  124. }
  125. if !addCache {
  126. return
  127. }
  128. d.cache.Do(c, func(c context.Context) {
  129. d.AddCacheUpsSpecial(c, missData)
  130. })
  131. return
  132. }