dao.cache.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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: -sync=true -batch=10 -max_group=10 -batch_err=break -nullcache=&api.VideoExtension{Svid:-1} -check_null_code=$==nil||$.Svid==-1
  7. VideoExtension(c context.Context, ids []int64) (map[int64]*api.VideoExtension, error)
  8. // cache: -sync=true -batch=10 -max_group=10 -batch_err=break -nullcache=&api.TopicInfo{TopicId:-1} -check_null_code=$==nil||$.TopicId==-1
  9. TopicInfo(c context.Context, ids []int64) (map[int64]*api.TopicInfo, error)
  10. }
  11. */
  12. package dao
  13. import (
  14. "context"
  15. "sync"
  16. "go-common/app/service/bbq/topic/api"
  17. "go-common/library/stat/prom"
  18. "go-common/library/sync/errgroup"
  19. )
  20. var _ _cache
  21. // VideoExtension get data from cache if miss will call source method, then add to cache.
  22. func (d *Dao) VideoExtension(c context.Context, keys []int64) (res map[int64]*api.VideoExtension, err error) {
  23. if len(keys) == 0 {
  24. return
  25. }
  26. addCache := true
  27. if res, err = d.CacheVideoExtension(c, keys); err != nil {
  28. addCache = false
  29. res = nil
  30. err = nil
  31. }
  32. var miss []int64
  33. for _, key := range keys {
  34. if (res == nil) || (res[key] == nil) {
  35. miss = append(miss, key)
  36. }
  37. }
  38. prom.CacheHit.Add("VideoExtension", int64(len(keys)-len(miss)))
  39. for k, v := range res {
  40. if v == nil || v.Svid == -1 {
  41. delete(res, k)
  42. }
  43. }
  44. missLen := len(miss)
  45. if missLen == 0 {
  46. return
  47. }
  48. missData := make(map[int64]*api.VideoExtension, missLen)
  49. prom.CacheMiss.Add("VideoExtension", int64(missLen))
  50. var mutex sync.Mutex
  51. group, ctx := errgroup.WithContext(c)
  52. if missLen > 10 {
  53. group.GOMAXPROCS(10)
  54. }
  55. var run = func(ms []int64) {
  56. group.Go(func() (err error) {
  57. data, err := d.RawVideoExtension(ctx, ms)
  58. mutex.Lock()
  59. for k, v := range data {
  60. missData[k] = v
  61. }
  62. mutex.Unlock()
  63. return
  64. })
  65. }
  66. var (
  67. i int
  68. n = missLen / 10
  69. )
  70. for i = 0; i < n; i++ {
  71. run(miss[i*n : (i+1)*n])
  72. }
  73. if len(miss[i*n:]) > 0 {
  74. run(miss[i*n:])
  75. }
  76. err = group.Wait()
  77. if res == nil {
  78. res = make(map[int64]*api.VideoExtension, len(keys))
  79. }
  80. for k, v := range missData {
  81. res[k] = v
  82. }
  83. if err != nil {
  84. return
  85. }
  86. for _, key := range miss {
  87. if res[key] == nil {
  88. missData[key] = &api.VideoExtension{Svid: -1}
  89. }
  90. }
  91. if !addCache {
  92. return
  93. }
  94. d.AddCacheVideoExtension(c, missData)
  95. return
  96. }
  97. // TopicInfo get data from cache if miss will call source method, then add to cache.
  98. func (d *Dao) TopicInfo(c context.Context, keys []int64) (res map[int64]*api.TopicInfo, err error) {
  99. if len(keys) == 0 {
  100. return
  101. }
  102. addCache := true
  103. if res, err = d.CacheTopicInfo(c, keys); err != nil {
  104. addCache = false
  105. res = nil
  106. err = nil
  107. }
  108. var miss []int64
  109. for _, key := range keys {
  110. if (res == nil) || (res[key] == nil) {
  111. miss = append(miss, key)
  112. }
  113. }
  114. prom.CacheHit.Add("TopicInfo", int64(len(keys)-len(miss)))
  115. for k, v := range res {
  116. if v == nil || v.TopicId == -1 {
  117. delete(res, k)
  118. }
  119. }
  120. missLen := len(miss)
  121. if missLen == 0 {
  122. return
  123. }
  124. missData := make(map[int64]*api.TopicInfo, missLen)
  125. prom.CacheMiss.Add("TopicInfo", int64(missLen))
  126. var mutex sync.Mutex
  127. group, ctx := errgroup.WithContext(c)
  128. if missLen > 10 {
  129. group.GOMAXPROCS(10)
  130. }
  131. var run = func(ms []int64) {
  132. group.Go(func() (err error) {
  133. data, err := d.RawTopicInfo(ctx, ms)
  134. mutex.Lock()
  135. for k, v := range data {
  136. missData[k] = v
  137. }
  138. mutex.Unlock()
  139. return
  140. })
  141. }
  142. var (
  143. i int
  144. n = missLen / 10
  145. )
  146. for i = 0; i < n; i++ {
  147. run(miss[i*n : (i+1)*n])
  148. }
  149. if len(miss[i*n:]) > 0 {
  150. run(miss[i*n:])
  151. }
  152. err = group.Wait()
  153. if res == nil {
  154. res = make(map[int64]*api.TopicInfo, len(keys))
  155. }
  156. for k, v := range missData {
  157. res[k] = v
  158. }
  159. if err != nil {
  160. return
  161. }
  162. for _, key := range miss {
  163. if res[key] == nil {
  164. missData[key] = &api.TopicInfo{TopicId: -1}
  165. }
  166. }
  167. if !addCache {
  168. return
  169. }
  170. d.AddCacheTopicInfo(c, missData)
  171. return
  172. }