dao.cache.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. Info(c context.Context, key int64) (*v1.Info, error)
  7. //cache: -batch=50 -max_group=10 -batch_err=continue
  8. Infos(c context.Context, keys []int64) (map[int64]*v1.Info, error)
  9. Card(c context.Context, key int64) (*v1.Card, error)
  10. //cache: -batch=50 -max_group=10 -batch_err=continue
  11. Cards(c context.Context, keys []int64) (map[int64]*v1.Card, error)
  12. Vip(c context.Context, key int64) (*v1.VipInfo, error)
  13. //cache: -batch=50 -max_group=10 -batch_err=continue
  14. Vips(c context.Context, keys []int64) (map[int64]*v1.VipInfo, error)
  15. Profile(c context.Context, key int64) (*v1.Profile, error)
  16. }
  17. */
  18. package dao
  19. import (
  20. "context"
  21. "sync"
  22. v1 "go-common/app/service/main/account/api"
  23. "go-common/library/stat/prom"
  24. "go-common/library/sync/errgroup"
  25. )
  26. var _ _cache
  27. // Info get data from cache if miss will call source method, then add to cache.
  28. func (d *Dao) Info(c context.Context, id int64) (res *v1.Info, err error) {
  29. addCache := true
  30. res, err = d.CacheInfo(c, id)
  31. if err != nil {
  32. addCache = false
  33. err = nil
  34. }
  35. if res != nil {
  36. prom.CacheHit.Incr("Info")
  37. return
  38. }
  39. prom.CacheMiss.Incr("Info")
  40. res, err = d.RawInfo(c, id)
  41. if err != nil {
  42. return
  43. }
  44. miss := res
  45. if !addCache {
  46. return
  47. }
  48. d.cache.Do(c, func(ctx context.Context) {
  49. d.AddCacheInfo(ctx, id, miss)
  50. })
  51. return
  52. }
  53. // Infos get data from cache if miss will call source method, then add to cache.
  54. func (d *Dao) Infos(c context.Context, keys []int64) (res map[int64]*v1.Info, err error) {
  55. if len(keys) == 0 {
  56. return
  57. }
  58. addCache := true
  59. res, err = d.CacheInfos(c, keys)
  60. if err != nil {
  61. addCache = false
  62. res = nil
  63. err = nil
  64. }
  65. var miss []int64
  66. for _, key := range keys {
  67. if (res == nil) || (res[key] == nil) {
  68. miss = append(miss, key)
  69. }
  70. }
  71. prom.CacheHit.Add("Infos", int64(len(keys)-len(miss)))
  72. if len(miss) == 0 {
  73. return
  74. }
  75. var missData map[int64]*v1.Info
  76. missLen := len(miss)
  77. prom.CacheMiss.Add("Infos", int64(missLen))
  78. mutex := sync.Mutex{}
  79. for i := 0; i < missLen; i += 50 * 10 {
  80. var subKeys []int64
  81. group := &errgroup.Group{}
  82. ctx := c
  83. if (i + 50*10) > missLen {
  84. subKeys = miss[i:]
  85. } else {
  86. subKeys = miss[i : i+50*10]
  87. }
  88. missSubLen := len(subKeys)
  89. for j := 0; j < missSubLen; j += 50 {
  90. var ks []int64
  91. if (j + 50) > missSubLen {
  92. ks = subKeys[j:]
  93. } else {
  94. ks = subKeys[j : j+50]
  95. }
  96. group.Go(func() (err error) {
  97. data, err := d.RawInfos(ctx, ks)
  98. mutex.Lock()
  99. for k, v := range data {
  100. if missData == nil {
  101. missData = make(map[int64]*v1.Info, len(keys))
  102. }
  103. missData[k] = v
  104. }
  105. mutex.Unlock()
  106. return
  107. })
  108. }
  109. err1 := group.Wait()
  110. if err1 != nil {
  111. err = err1
  112. }
  113. }
  114. if res == nil {
  115. res = make(map[int64]*v1.Info)
  116. }
  117. for k, v := range missData {
  118. res[k] = v
  119. }
  120. if err != nil {
  121. return
  122. }
  123. if !addCache {
  124. return
  125. }
  126. d.cache.Do(c, func(ctx context.Context) {
  127. d.AddCacheInfos(ctx, missData)
  128. })
  129. return
  130. }
  131. // Card cache: -batch=50 -max_group=10 -batch_err=continue
  132. func (d *Dao) Card(c context.Context, id int64) (res *v1.Card, err error) {
  133. addCache := true
  134. res, err = d.CacheCard(c, id)
  135. if err != nil {
  136. addCache = false
  137. err = nil
  138. }
  139. if res != nil {
  140. prom.CacheHit.Incr("Card")
  141. return
  142. }
  143. prom.CacheMiss.Incr("Card")
  144. res, err = d.RawCard(c, id)
  145. if err != nil {
  146. return
  147. }
  148. miss := res
  149. if !addCache {
  150. return
  151. }
  152. d.cache.Do(c, func(ctx context.Context) {
  153. d.AddCacheCard(ctx, id, miss)
  154. })
  155. return
  156. }
  157. // Cards get data from cache if miss will call source method, then add to cache.
  158. func (d *Dao) Cards(c context.Context, keys []int64) (res map[int64]*v1.Card, err error) {
  159. if len(keys) == 0 {
  160. return
  161. }
  162. addCache := true
  163. res, err = d.CacheCards(c, keys)
  164. if err != nil {
  165. addCache = false
  166. res = nil
  167. err = nil
  168. }
  169. var miss []int64
  170. for _, key := range keys {
  171. if (res == nil) || (res[key] == nil) {
  172. miss = append(miss, key)
  173. }
  174. }
  175. prom.CacheHit.Add("Cards", int64(len(keys)-len(miss)))
  176. if len(miss) == 0 {
  177. return
  178. }
  179. var missData map[int64]*v1.Card
  180. missLen := len(miss)
  181. prom.CacheMiss.Add("Cards", int64(missLen))
  182. mutex := sync.Mutex{}
  183. for i := 0; i < missLen; i += 50 * 10 {
  184. var subKeys []int64
  185. group := &errgroup.Group{}
  186. ctx := c
  187. if (i + 50*10) > missLen {
  188. subKeys = miss[i:]
  189. } else {
  190. subKeys = miss[i : i+50*10]
  191. }
  192. missSubLen := len(subKeys)
  193. for j := 0; j < missSubLen; j += 50 {
  194. var ks []int64
  195. if (j + 50) > missSubLen {
  196. ks = subKeys[j:]
  197. } else {
  198. ks = subKeys[j : j+50]
  199. }
  200. group.Go(func() (err error) {
  201. data, err := d.RawCards(ctx, ks)
  202. mutex.Lock()
  203. for k, v := range data {
  204. if missData == nil {
  205. missData = make(map[int64]*v1.Card, len(keys))
  206. }
  207. missData[k] = v
  208. }
  209. mutex.Unlock()
  210. return
  211. })
  212. }
  213. err1 := group.Wait()
  214. if err1 != nil {
  215. err = err1
  216. }
  217. }
  218. if res == nil {
  219. res = make(map[int64]*v1.Card)
  220. }
  221. for k, v := range missData {
  222. res[k] = v
  223. }
  224. if err != nil {
  225. return
  226. }
  227. if !addCache {
  228. return
  229. }
  230. d.cache.Do(c, func(ctx context.Context) {
  231. d.AddCacheCards(ctx, missData)
  232. })
  233. return
  234. }
  235. // Vip cache: -batch=50 -max_group=10 -batch_err=continue
  236. func (d *Dao) Vip(c context.Context, id int64) (res *v1.VipInfo, err error) {
  237. addCache := true
  238. res, err = d.CacheVip(c, id)
  239. if err != nil {
  240. addCache = false
  241. err = nil
  242. }
  243. if res != nil {
  244. prom.CacheHit.Incr("Vip")
  245. return
  246. }
  247. prom.CacheMiss.Incr("Vip")
  248. res, err = d.RawVip(c, id)
  249. if err != nil {
  250. return
  251. }
  252. miss := res
  253. if !addCache {
  254. return
  255. }
  256. d.cache.Do(c, func(ctx context.Context) {
  257. d.AddCacheVip(ctx, id, miss)
  258. })
  259. return
  260. }
  261. // Vips get data from cache if miss will call source method, then add to cache.
  262. func (d *Dao) Vips(c context.Context, keys []int64) (res map[int64]*v1.VipInfo, err error) {
  263. if len(keys) == 0 {
  264. return
  265. }
  266. addCache := true
  267. res, err = d.CacheVips(c, keys)
  268. if err != nil {
  269. addCache = false
  270. res = nil
  271. err = nil
  272. }
  273. var miss []int64
  274. for _, key := range keys {
  275. if (res == nil) || (res[key] == nil) {
  276. miss = append(miss, key)
  277. }
  278. }
  279. prom.CacheHit.Add("Vips", int64(len(keys)-len(miss)))
  280. if len(miss) == 0 {
  281. return
  282. }
  283. var missData map[int64]*v1.VipInfo
  284. missLen := len(miss)
  285. prom.CacheMiss.Add("Vips", int64(missLen))
  286. mutex := sync.Mutex{}
  287. for i := 0; i < missLen; i += 50 * 10 {
  288. var subKeys []int64
  289. group := &errgroup.Group{}
  290. ctx := c
  291. if (i + 50*10) > missLen {
  292. subKeys = miss[i:]
  293. } else {
  294. subKeys = miss[i : i+50*10]
  295. }
  296. missSubLen := len(subKeys)
  297. for j := 0; j < missSubLen; j += 50 {
  298. var ks []int64
  299. if (j + 50) > missSubLen {
  300. ks = subKeys[j:]
  301. } else {
  302. ks = subKeys[j : j+50]
  303. }
  304. group.Go(func() (err error) {
  305. data, err := d.RawVips(ctx, ks)
  306. mutex.Lock()
  307. for k, v := range data {
  308. if missData == nil {
  309. missData = make(map[int64]*v1.VipInfo, len(keys))
  310. }
  311. missData[k] = v
  312. }
  313. mutex.Unlock()
  314. return
  315. })
  316. }
  317. err1 := group.Wait()
  318. if err1 != nil {
  319. err = err1
  320. }
  321. }
  322. if res == nil {
  323. res = make(map[int64]*v1.VipInfo)
  324. }
  325. for k, v := range missData {
  326. res[k] = v
  327. }
  328. if err != nil {
  329. return
  330. }
  331. if !addCache {
  332. return
  333. }
  334. d.cache.Do(c, func(ctx context.Context) {
  335. d.AddCacheVips(ctx, missData)
  336. })
  337. return
  338. }
  339. // Profile cache: -batch=50 -max_group=10 -batch_err=continue
  340. func (d *Dao) Profile(c context.Context, id int64) (res *v1.Profile, err error) {
  341. addCache := true
  342. res, err = d.CacheProfile(c, id)
  343. if err != nil {
  344. addCache = false
  345. err = nil
  346. }
  347. if res != nil {
  348. prom.CacheHit.Incr("Profile")
  349. return
  350. }
  351. prom.CacheMiss.Incr("Profile")
  352. res, err = d.RawProfile(c, id)
  353. if err != nil {
  354. return
  355. }
  356. miss := res
  357. if !addCache {
  358. return
  359. }
  360. d.cache.Do(c, func(ctx context.Context) {
  361. d.AddCacheProfile(ctx, id, miss)
  362. })
  363. return
  364. }