dao.cache.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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: -nullcache=&model.List{ID:-1} -check_null_code=$!=nil&&$.ID==-1 -singleflight=true
  7. List(c context.Context, id int64) (*model.List, error)
  8. // cache: -batch=100 -max_group=10 -nullcache=&model.List{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  9. Lists(c context.Context, keys []int64) (map[int64]*model.List, error)
  10. // cache: -singleflight=true -nullcache=[]*model.ListArtMeta{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
  11. ListArts(c context.Context, id int64) ([]*model.ListArtMeta, error)
  12. // cache: -nullcache=[]*model.ListArtMeta{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
  13. ListsArts(c context.Context, ids []int64) (map[int64][]*model.ListArtMeta, error)
  14. // cache: -nullcache=-1 -batch=100 -max_group=10
  15. ArtsListID(c context.Context, keys []int64) (map[int64]int64, error)
  16. // cache: -nullcache=[]int64{-1} -check_null_code=len($)==1&&$[0]==-1 -singleflight=true
  17. UpLists(c context.Context, mid int64) ([]int64, error)
  18. // cache: -nullcache=&model.AuthorLimit{Limit:-1} -check_null_code=$!=nil&&$.Limit==-1
  19. Author(c context.Context, mid int64) (*model.AuthorLimit, error)
  20. }
  21. */
  22. package dao
  23. import (
  24. "context"
  25. "sync"
  26. "go-common/app/interface/openplatform/article/model"
  27. "go-common/library/net/metadata"
  28. "go-common/library/stat/prom"
  29. "go-common/library/sync/errgroup"
  30. "golang.org/x/sync/singleflight"
  31. )
  32. var _ _cache
  33. var cacheSingleFlights = [3]*singleflight.Group{{}, {}, {}}
  34. // List get data from cache if miss will call source method, then add to cache.
  35. func (d *Dao) List(c context.Context, id int64) (res *model.List, err error) {
  36. addCache := true
  37. res, err = d.CacheList(c, id)
  38. if err != nil {
  39. addCache = false
  40. err = nil
  41. }
  42. defer func() {
  43. if res != nil && res.ID == -1 {
  44. res = nil
  45. }
  46. }()
  47. if res != nil {
  48. prom.CacheHit.Incr("List")
  49. return
  50. }
  51. var rr interface{}
  52. sf := d.cacheSFList(id)
  53. rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
  54. prom.CacheMiss.Incr("List")
  55. r, e = d.RawList(c, id)
  56. return
  57. })
  58. res = rr.(*model.List)
  59. if err != nil {
  60. return
  61. }
  62. miss := res
  63. if miss == nil {
  64. miss = &model.List{ID: -1}
  65. }
  66. if !addCache {
  67. return
  68. }
  69. d.cache.Save(func() {
  70. d.AddCacheList(metadata.WithContext(c), id, miss)
  71. })
  72. return
  73. }
  74. // Lists get data from cache if miss will call source method, then add to cache.
  75. func (d *Dao) Lists(c context.Context, keys []int64) (res map[int64]*model.List, err error) {
  76. if len(keys) == 0 {
  77. return
  78. }
  79. addCache := true
  80. res, err = d.CacheLists(c, keys)
  81. if err != nil {
  82. addCache = false
  83. res = nil
  84. err = nil
  85. }
  86. var miss []int64
  87. for _, key := range keys {
  88. if (res == nil) || (res[key] == nil) {
  89. miss = append(miss, key)
  90. }
  91. }
  92. prom.CacheHit.Add("Lists", int64(len(keys)-len(miss)))
  93. defer func() {
  94. for k, v := range res {
  95. if v != nil && v.ID == -1 {
  96. delete(res, k)
  97. }
  98. }
  99. }()
  100. if len(miss) == 0 {
  101. return
  102. }
  103. var missData map[int64]*model.List
  104. missLen := len(miss)
  105. prom.CacheMiss.Add("Lists", int64(missLen))
  106. mutex := sync.Mutex{}
  107. for i := 0; i < missLen; i += 100 * 10 {
  108. var subKeys []int64
  109. group, ctx := errgroup.WithContext(c)
  110. if (i + 100*10) > missLen {
  111. subKeys = miss[i:]
  112. } else {
  113. subKeys = miss[i : i+100*10]
  114. }
  115. missSubLen := len(subKeys)
  116. for j := 0; j < missSubLen; j += 100 {
  117. var ks []int64
  118. if (j + 100) > missSubLen {
  119. ks = subKeys[j:]
  120. } else {
  121. ks = subKeys[j : j+100]
  122. }
  123. group.Go(func() (err error) {
  124. data, err := d.RawLists(ctx, ks)
  125. mutex.Lock()
  126. for k, v := range data {
  127. if missData == nil {
  128. missData = make(map[int64]*model.List, len(keys))
  129. }
  130. missData[k] = v
  131. }
  132. mutex.Unlock()
  133. return
  134. })
  135. }
  136. err1 := group.Wait()
  137. if err1 != nil {
  138. err = err1
  139. break
  140. }
  141. }
  142. if res == nil {
  143. res = make(map[int64]*model.List)
  144. }
  145. for k, v := range missData {
  146. res[k] = v
  147. }
  148. if err != nil {
  149. return
  150. }
  151. for _, key := range keys {
  152. if res[key] == nil {
  153. if missData == nil {
  154. missData = make(map[int64]*model.List, len(keys))
  155. }
  156. missData[key] = &model.List{ID: -1}
  157. }
  158. }
  159. if !addCache {
  160. return
  161. }
  162. d.cache.Save(func() {
  163. d.AddCacheLists(metadata.WithContext(c), missData)
  164. })
  165. return
  166. }
  167. // ListArts get data from cache if miss will call source method, then add to cache.
  168. func (d *Dao) ListArts(c context.Context, id int64) (res []*model.ListArtMeta, err error) {
  169. addCache := true
  170. res, err = d.CacheListArts(c, id)
  171. if err != nil {
  172. addCache = false
  173. err = nil
  174. }
  175. defer func() {
  176. if len(res) == 1 && res[0].ID == -1 {
  177. res = nil
  178. }
  179. }()
  180. if len(res) != 0 {
  181. prom.CacheHit.Incr("ListArts")
  182. return
  183. }
  184. var rr interface{}
  185. sf := d.cacheSFListArts(id)
  186. rr, err, _ = cacheSingleFlights[1].Do(sf, func() (r interface{}, e error) {
  187. prom.CacheMiss.Incr("ListArts")
  188. r, e = d.RawListArts(c, id)
  189. return
  190. })
  191. res = rr.([]*model.ListArtMeta)
  192. if err != nil {
  193. return
  194. }
  195. miss := res
  196. if len(miss) == 0 {
  197. miss = []*model.ListArtMeta{{ID: -1}}
  198. }
  199. if !addCache {
  200. return
  201. }
  202. d.cache.Save(func() {
  203. d.AddCacheListArts(metadata.WithContext(c), id, miss)
  204. })
  205. return
  206. }
  207. // ListsArts get data from cache if miss will call source method, then add to cache.
  208. func (d *Dao) ListsArts(c context.Context, keys []int64) (res map[int64][]*model.ListArtMeta, err error) {
  209. if len(keys) == 0 {
  210. return
  211. }
  212. addCache := true
  213. res, err = d.CacheListsArts(c, keys)
  214. if err != nil {
  215. addCache = false
  216. res = nil
  217. err = nil
  218. }
  219. var miss []int64
  220. for _, key := range keys {
  221. if (res == nil) || (len(res[key]) == 0) {
  222. miss = append(miss, key)
  223. }
  224. }
  225. prom.CacheHit.Add("ListsArts", int64(len(keys)-len(miss)))
  226. defer func() {
  227. for k, v := range res {
  228. if len(v) == 1 && v[0].ID == -1 {
  229. delete(res, k)
  230. }
  231. }
  232. }()
  233. if len(miss) == 0 {
  234. return
  235. }
  236. var missData map[int64][]*model.ListArtMeta
  237. prom.CacheMiss.Add("ListsArts", int64(len(miss)))
  238. missData, err = d.RawListsArts(c, miss)
  239. if res == nil {
  240. res = make(map[int64][]*model.ListArtMeta)
  241. }
  242. for k, v := range missData {
  243. res[k] = v
  244. }
  245. if err != nil {
  246. return
  247. }
  248. for _, key := range keys {
  249. if len(res[key]) == 0 {
  250. if missData == nil {
  251. missData = make(map[int64][]*model.ListArtMeta, len(keys))
  252. }
  253. missData[key] = []*model.ListArtMeta{{ID: -1}}
  254. }
  255. }
  256. if !addCache {
  257. return
  258. }
  259. d.cache.Save(func() {
  260. d.AddCacheListsArts(metadata.WithContext(c), missData)
  261. })
  262. return
  263. }
  264. // ArtsListID get data from cache if miss will call source method, then add to cache.
  265. func (d *Dao) ArtsListID(c context.Context, keys []int64) (res map[int64]int64, err error) {
  266. if len(keys) == 0 {
  267. return
  268. }
  269. addCache := true
  270. res, err = d.CacheArtsListID(c, keys)
  271. if err != nil {
  272. addCache = false
  273. res = nil
  274. err = nil
  275. }
  276. var miss []int64
  277. for _, key := range keys {
  278. if _, ok := res[key]; !ok {
  279. miss = append(miss, key)
  280. }
  281. }
  282. prom.CacheHit.Add("ArtsListID", int64(len(keys)-len(miss)))
  283. defer func() {
  284. for k, v := range res {
  285. if v == -1 {
  286. delete(res, k)
  287. }
  288. }
  289. }()
  290. if len(miss) == 0 {
  291. return
  292. }
  293. var missData map[int64]int64
  294. missLen := len(miss)
  295. prom.CacheMiss.Add("ArtsListID", int64(missLen))
  296. mutex := sync.Mutex{}
  297. for i := 0; i < missLen; i += 100 * 10 {
  298. var subKeys []int64
  299. group, ctx := errgroup.WithContext(c)
  300. if (i + 100*10) > missLen {
  301. subKeys = miss[i:]
  302. } else {
  303. subKeys = miss[i : i+100*10]
  304. }
  305. missSubLen := len(subKeys)
  306. for j := 0; j < missSubLen; j += 100 {
  307. var ks []int64
  308. if (j + 100) > missSubLen {
  309. ks = subKeys[j:]
  310. } else {
  311. ks = subKeys[j : j+100]
  312. }
  313. group.Go(func() (err error) {
  314. data, err := d.RawArtsListID(ctx, ks)
  315. mutex.Lock()
  316. for k, v := range data {
  317. if missData == nil {
  318. missData = make(map[int64]int64, len(keys))
  319. }
  320. missData[k] = v
  321. }
  322. mutex.Unlock()
  323. return
  324. })
  325. }
  326. err1 := group.Wait()
  327. if err1 != nil {
  328. err = err1
  329. break
  330. }
  331. }
  332. if res == nil {
  333. res = make(map[int64]int64)
  334. }
  335. for k, v := range missData {
  336. res[k] = v
  337. }
  338. if err != nil {
  339. return
  340. }
  341. for _, key := range keys {
  342. if res[key] == 0 {
  343. if missData == nil {
  344. missData = make(map[int64]int64, len(keys))
  345. }
  346. missData[key] = -1
  347. }
  348. }
  349. if !addCache {
  350. return
  351. }
  352. d.cache.Save(func() {
  353. d.AddCacheArtsListID(metadata.WithContext(c), missData)
  354. })
  355. return
  356. }
  357. // UpLists get data from cache if miss will call source method, then add to cache.
  358. func (d *Dao) UpLists(c context.Context, id int64) (res []int64, err error) {
  359. addCache := true
  360. res, err = d.CacheUpLists(c, id)
  361. if err != nil {
  362. addCache = false
  363. err = nil
  364. }
  365. defer func() {
  366. if len(res) == 1 && res[0] == -1 {
  367. res = nil
  368. }
  369. }()
  370. if len(res) != 0 {
  371. prom.CacheHit.Incr("UpLists")
  372. return
  373. }
  374. var rr interface{}
  375. sf := d.cacheSFUpLists(id)
  376. rr, err, _ = cacheSingleFlights[2].Do(sf, func() (r interface{}, e error) {
  377. prom.CacheMiss.Incr("UpLists")
  378. r, e = d.RawUpLists(c, id)
  379. return
  380. })
  381. res = rr.([]int64)
  382. if err != nil {
  383. return
  384. }
  385. miss := res
  386. if len(miss) == 0 {
  387. miss = []int64{-1}
  388. }
  389. if !addCache {
  390. return
  391. }
  392. d.cache.Save(func() {
  393. d.AddCacheUpLists(metadata.WithContext(c), id, miss)
  394. })
  395. return
  396. }
  397. // Author get data from cache if miss will call source method, then add to cache.
  398. func (d *Dao) Author(c context.Context, id int64) (res *model.AuthorLimit, err error) {
  399. addCache := true
  400. res, err = d.CacheAuthor(c, id)
  401. if err != nil {
  402. addCache = false
  403. err = nil
  404. }
  405. defer func() {
  406. if res != nil && res.Limit == -1 {
  407. res = nil
  408. }
  409. }()
  410. if res != nil {
  411. prom.CacheHit.Incr("Author")
  412. return
  413. }
  414. prom.CacheMiss.Incr("Author")
  415. res, err = d.RawAuthor(c, id)
  416. if err != nil {
  417. return
  418. }
  419. miss := res
  420. if miss == nil {
  421. miss = &model.AuthorLimit{Limit: -1}
  422. }
  423. if !addCache {
  424. return
  425. }
  426. d.cache.Save(func() {
  427. d.AddCacheAuthor(metadata.WithContext(c), id, miss)
  428. })
  429. return
  430. }