dao.cache.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package testdata is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // cache: -batch=10 -max_group=10 -batch_err=break -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
  7. Articles(c context.Context, keys []int64) (map[int64]*Article, error)
  8. // cache: -sync=true -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
  9. Article(c context.Context, key int64) (*Article, error)
  10. // cache: -paging=true
  11. Article1(c context.Context, key int64, pn, ps int) (*Article, error)
  12. // cache: -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
  13. None(c context.Context) (*Article, error)
  14. }
  15. */
  16. package testdata
  17. import (
  18. "context"
  19. "sync"
  20. "go-common/library/stat/prom"
  21. "go-common/library/sync/errgroup"
  22. )
  23. var _ _cache
  24. // Articles get data from cache if miss will call source method, then add to cache.
  25. func (d *Dao) Articles(c context.Context, keys []int64) (res map[int64]*Article, err error) {
  26. if len(keys) == 0 {
  27. return
  28. }
  29. addCache := true
  30. if res, err = d.CacheArticles(c, keys); err != nil {
  31. addCache = false
  32. res = nil
  33. err = nil
  34. }
  35. var miss []int64
  36. for _, key := range keys {
  37. if (res == nil) || (res[key] == nil) {
  38. miss = append(miss, key)
  39. }
  40. }
  41. prom.CacheHit.Add("Articles", int64(len(keys)-len(miss)))
  42. for k, v := range res {
  43. if v.ID == -1 {
  44. delete(res, k)
  45. }
  46. }
  47. missLen := len(miss)
  48. if missLen == 0 {
  49. return
  50. }
  51. missData := make(map[int64]*Article, missLen)
  52. prom.CacheMiss.Add("Articles", int64(missLen))
  53. var mutex sync.Mutex
  54. group, ctx := errgroup.WithContext(c)
  55. if missLen > 10 {
  56. group.GOMAXPROCS(10)
  57. }
  58. var run = func(ms []int64) {
  59. group.Go(func() (err error) {
  60. data, err := d.RawArticles(ctx, ms)
  61. mutex.Lock()
  62. for k, v := range data {
  63. missData[k] = v
  64. }
  65. mutex.Unlock()
  66. return
  67. })
  68. }
  69. var (
  70. i int
  71. n = missLen / 10
  72. )
  73. for i = 0; i < n; i++ {
  74. run(miss[i*n : (i+1)*n])
  75. }
  76. if len(miss[i*n:]) > 0 {
  77. run(miss[i*n:])
  78. }
  79. err = group.Wait()
  80. if res == nil {
  81. res = make(map[int64]*Article, len(keys))
  82. }
  83. for k, v := range missData {
  84. res[k] = v
  85. }
  86. if err != nil {
  87. return
  88. }
  89. for _, key := range miss {
  90. if res[key] == nil {
  91. missData[key] = &Article{ID: -1}
  92. }
  93. }
  94. if !addCache {
  95. return
  96. }
  97. d.cache.Do(c, func(c context.Context) {
  98. d.AddCacheArticles(c, missData)
  99. })
  100. return
  101. }
  102. // Article get data from cache if miss will call source method, then add to cache.
  103. func (d *Dao) Article(c context.Context, id int64) (res *Article, err error) {
  104. addCache := true
  105. res, err = d.CacheArticle(c, id)
  106. if err != nil {
  107. addCache = false
  108. err = nil
  109. }
  110. defer func() {
  111. if res.ID == -1 {
  112. res = nil
  113. }
  114. }()
  115. if res != nil {
  116. prom.CacheHit.Incr("Article")
  117. return
  118. }
  119. prom.CacheMiss.Incr("Article")
  120. res, err = d.RawArticle(c, id)
  121. if err != nil {
  122. return
  123. }
  124. miss := res
  125. if miss == nil {
  126. miss = &Article{ID: -1}
  127. }
  128. if !addCache {
  129. return
  130. }
  131. d.AddCacheArticle(c, id, miss)
  132. return
  133. }
  134. // Article1 get data from cache if miss will call source method, then add to cache.
  135. func (d *Dao) Article1(c context.Context, id int64, pn, ps int) (res *Article, err error) {
  136. addCache := true
  137. res, err = d.CacheArticle1(c, id, pn, ps)
  138. if err != nil {
  139. addCache = false
  140. err = nil
  141. }
  142. if res != nil {
  143. prom.CacheHit.Incr("Article1")
  144. return
  145. }
  146. var miss *Article
  147. prom.CacheMiss.Incr("Article1")
  148. res, miss, err = d.RawArticle1(c, id, pn, ps)
  149. if err != nil {
  150. return
  151. }
  152. if !addCache {
  153. return
  154. }
  155. d.cache.Do(c, func(c context.Context) {
  156. d.AddCacheArticle1(c, id, miss, pn, ps)
  157. })
  158. return
  159. }
  160. // None get data from cache if miss will call source method, then add to cache.
  161. func (d *Dao) None(c context.Context) (res *Article, err error) {
  162. addCache := true
  163. res, err = d.CacheNone(c)
  164. if err != nil {
  165. addCache = false
  166. err = nil
  167. }
  168. defer func() {
  169. if res.ID == -1 {
  170. res = nil
  171. }
  172. }()
  173. if res != nil {
  174. prom.CacheHit.Incr("None")
  175. return
  176. }
  177. prom.CacheMiss.Incr("None")
  178. res, err = d.RawNone(c)
  179. if err != nil {
  180. return
  181. }
  182. var miss = res
  183. if miss == nil {
  184. miss = &Article{ID: -1}
  185. }
  186. if !addCache {
  187. return
  188. }
  189. d.cache.Do(c, func(c context.Context) {
  190. d.AddCacheNone(c, miss)
  191. })
  192. return
  193. }