dao.cache.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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.Promotion{PromoID:-1} -check_null_code=$!=nil&&$.PromoID==-1
  7. Promo(c context.Context, promoID int64) (*model.Promotion, error)
  8. // cache: -nullcache=&model.PromotionGroup{GroupID:-1} -check_null_code=$!=nil&&$.GroupID==-1
  9. PromoGroup(c context.Context, groupID int64) (*model.PromotionGroup, error)
  10. // cache: -nullcache=&model.PromotionOrder{OrderID:-1} -check_null_code=$!=nil&&$.OrderID==-1
  11. PromoOrder(c context.Context, orderID int64) (*model.PromotionOrder, error)
  12. // cache: -nullcache=[]*model.PromotionOrder{{GroupID:-1}} -check_null_code=len($)==1&&$[0].GroupID==-1
  13. PromoOrders(c context.Context, groupID int64) ([]*model.PromotionOrder, error)
  14. // cache: -nullcache=[]*model.OrderMain{{OrderID:-1}} -check_null_code=len($)==1&&$[0].OrderID==-1
  15. Orders(context.Context, *model.OrderMainQuerier) ([]*model.OrderMain, error)
  16. // cache: -nullcache=-1 -check_null_code=$==-1
  17. OrderCount(context.Context, *model.OrderMainQuerier) (int64, error)
  18. // cache: -nullcache=&model.OrderDetail{OrderID:0} -check_null_code=$!=nil&&$.OrderID==0
  19. OrderDetails(context.Context, []int64) (map[int64]*model.OrderDetail, error)
  20. // cache: -nullcache=[]*model.OrderSKU{{OrderID:-1}} -check_null_code=len($)==1&&$[0].OrderID==-1
  21. OrderSKUs(context.Context, []int64) (map[int64][]*model.OrderSKU, error)
  22. // cache: -nullcache=&model.OrderPayCharge{ChargeID:""} -check_null_code=$!=nil&&$.ChargeID==""
  23. OrderPayCharges(context.Context, []int64) (map[int64]*model.OrderPayCharge, error)
  24. // cache:
  25. SkuByItemID(c context.Context, itemID int64) (map[string]*model.SKUStock, error)
  26. // cache:
  27. GetSKUs(c context.Context, skuIds []int64, withNewStock bool) (map[int64]*model.SKUStock, error)
  28. // cache:
  29. Stocks(c context.Context, keys []int64, isLocked bool) (res map[int64]int64, err error)
  30. // cache: -nullcache=[]*model.Ticket{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
  31. TicketsByOrderID(c context.Context, orderID int64) (res []*model.Ticket, err error)
  32. // cache: -nullcache=[]*model.Ticket{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
  33. TicketsByScreen(c context.Context, screenID int64, UID int64) (res []*model.Ticket, err error)
  34. // cache: -nullcache=&model.Ticket{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  35. TicketsByID(c context.Context, id []int64) (res map[int64]*model.Ticket, err error)
  36. // cache: -nullcache=&model.TicketSend{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  37. TicketSend(c context.Context, SendTID []int64, TIDType string) (res map[int64]*model.TicketSend, err error)
  38. }
  39. */
  40. package dao
  41. import (
  42. "context"
  43. "go-common/app/service/openplatform/ticket-sales/model"
  44. "go-common/library/net/metadata"
  45. "go-common/library/stat/prom"
  46. )
  47. var _ _cache
  48. // Promo get data from cache if miss will call source method, then add to cache.
  49. func (d *Dao) Promo(c context.Context, id int64) (res *model.Promotion, err error) {
  50. addCache := true
  51. res, err = d.CachePromo(c, id)
  52. if err != nil {
  53. addCache = false
  54. err = nil
  55. }
  56. defer func() {
  57. if res != nil && res.PromoID == -1 {
  58. res = nil
  59. }
  60. }()
  61. if res != nil {
  62. prom.CacheHit.Incr("Promo")
  63. return
  64. }
  65. prom.CacheMiss.Incr("Promo")
  66. res, err = d.RawPromo(c, id)
  67. if err != nil {
  68. return
  69. }
  70. miss := res
  71. if miss == nil {
  72. miss = &model.Promotion{PromoID: -1}
  73. }
  74. if !addCache {
  75. return
  76. }
  77. d.cache.Save(func() {
  78. d.AddCachePromo(metadata.WithContext(c), id, miss)
  79. })
  80. return
  81. }
  82. // PromoGroup get data from cache if miss will call source method, then add to cache.
  83. func (d *Dao) PromoGroup(c context.Context, id int64) (res *model.PromotionGroup, err error) {
  84. addCache := true
  85. res, err = d.CachePromoGroup(c, id)
  86. if err != nil {
  87. addCache = false
  88. err = nil
  89. }
  90. defer func() {
  91. if res != nil && res.GroupID == -1 {
  92. res = nil
  93. }
  94. }()
  95. if res != nil {
  96. prom.CacheHit.Incr("PromoGroup")
  97. return
  98. }
  99. prom.CacheMiss.Incr("PromoGroup")
  100. res, err = d.RawPromoGroup(c, id)
  101. if err != nil {
  102. return
  103. }
  104. miss := res
  105. if miss == nil {
  106. miss = &model.PromotionGroup{GroupID: -1}
  107. }
  108. if !addCache {
  109. return
  110. }
  111. d.cache.Save(func() {
  112. d.AddCachePromoGroup(metadata.WithContext(c), id, miss)
  113. })
  114. return
  115. }
  116. // PromoOrder get data from cache if miss will call source method, then add to cache.
  117. func (d *Dao) PromoOrder(c context.Context, id int64) (res *model.PromotionOrder, err error) {
  118. addCache := true
  119. res, err = d.CachePromoOrder(c, id)
  120. if err != nil {
  121. addCache = false
  122. err = nil
  123. }
  124. defer func() {
  125. if res != nil && res.OrderID == -1 {
  126. res = nil
  127. }
  128. }()
  129. if res != nil {
  130. prom.CacheHit.Incr("PromoOrder")
  131. return
  132. }
  133. prom.CacheMiss.Incr("PromoOrder")
  134. res, err = d.RawPromoOrder(c, id)
  135. if err != nil {
  136. return
  137. }
  138. miss := res
  139. if miss == nil {
  140. miss = &model.PromotionOrder{OrderID: -1}
  141. }
  142. if !addCache {
  143. return
  144. }
  145. d.cache.Save(func() {
  146. d.AddCachePromoOrder(metadata.WithContext(c), id, miss)
  147. })
  148. return
  149. }
  150. // PromoOrders get data from cache if miss will call source method, then add to cache.
  151. func (d *Dao) PromoOrders(c context.Context, id int64) (res []*model.PromotionOrder, err error) {
  152. addCache := true
  153. res, err = d.CachePromoOrders(c, id)
  154. if err != nil {
  155. addCache = false
  156. err = nil
  157. }
  158. defer func() {
  159. if len(res) == 1 && res[0].GroupID == -1 {
  160. res = nil
  161. }
  162. }()
  163. if len(res) != 0 {
  164. prom.CacheHit.Incr("PromoOrders")
  165. return
  166. }
  167. prom.CacheMiss.Incr("PromoOrders")
  168. res, err = d.RawPromoOrders(c, id)
  169. if err != nil {
  170. return
  171. }
  172. miss := res
  173. if len(miss) == 0 {
  174. miss = []*model.PromotionOrder{{GroupID: -1}}
  175. }
  176. if !addCache {
  177. return
  178. }
  179. d.cache.Save(func() {
  180. d.AddCachePromoOrders(metadata.WithContext(c), id, miss)
  181. })
  182. return
  183. }
  184. // Orders get data from cache if miss will call source method, then add to cache.
  185. func (d *Dao) Orders(c context.Context, id *model.OrderMainQuerier) (res []*model.OrderMain, err error) {
  186. addCache := true
  187. res, err = d.CacheOrders(c, id)
  188. if err != nil {
  189. addCache = false
  190. err = nil
  191. }
  192. defer func() {
  193. if len(res) == 1 && res[0].OrderID == -1 {
  194. res = nil
  195. }
  196. }()
  197. if len(res) != 0 {
  198. prom.CacheHit.Incr("Orders")
  199. return
  200. }
  201. prom.CacheMiss.Incr("Orders")
  202. res, err = d.RawOrders(c, id)
  203. if err != nil {
  204. return
  205. }
  206. miss := res
  207. if len(miss) == 0 {
  208. miss = []*model.OrderMain{{OrderID: -1}}
  209. }
  210. if !addCache {
  211. return
  212. }
  213. d.cache.Save(func() {
  214. d.AddCacheOrders(metadata.WithContext(c), id, miss)
  215. })
  216. return
  217. }
  218. // OrderCount get data from cache if miss will call source method, then add to cache.
  219. func (d *Dao) OrderCount(c context.Context, id *model.OrderMainQuerier) (res int64, err error) {
  220. addCache := true
  221. res, err = d.CacheOrderCount(c, id)
  222. if err != nil {
  223. addCache = false
  224. err = nil
  225. }
  226. defer func() {
  227. if res == -1 {
  228. res = 0
  229. }
  230. }()
  231. if res != 0 {
  232. prom.CacheHit.Incr("OrderCount")
  233. return
  234. }
  235. prom.CacheMiss.Incr("OrderCount")
  236. res, err = d.RawOrderCount(c, id)
  237. if err != nil {
  238. return
  239. }
  240. miss := res
  241. if miss == 0 {
  242. miss = -1
  243. }
  244. if !addCache {
  245. return
  246. }
  247. d.cache.Save(func() {
  248. d.AddCacheOrderCount(metadata.WithContext(c), id, miss)
  249. })
  250. return
  251. }
  252. // OrderDetails get data from cache if miss will call source method, then add to cache.
  253. func (d *Dao) OrderDetails(c context.Context, keys []int64) (res map[int64]*model.OrderDetail, err error) {
  254. if len(keys) == 0 {
  255. return
  256. }
  257. addCache := true
  258. res, err = d.CacheOrderDetails(c, keys)
  259. if err != nil {
  260. addCache = false
  261. res = nil
  262. err = nil
  263. }
  264. var miss []int64
  265. for _, key := range keys {
  266. if (res == nil) || (res[key] == nil) {
  267. miss = append(miss, key)
  268. }
  269. }
  270. prom.CacheHit.Add("OrderDetails", int64(len(keys)-len(miss)))
  271. defer func() {
  272. for k, v := range res {
  273. if v != nil && v.OrderID == 0 {
  274. delete(res, k)
  275. }
  276. }
  277. }()
  278. if len(miss) == 0 {
  279. return
  280. }
  281. var missData map[int64]*model.OrderDetail
  282. prom.CacheMiss.Add("OrderDetails", int64(len(miss)))
  283. missData, err = d.RawOrderDetails(c, miss)
  284. if res == nil {
  285. res = make(map[int64]*model.OrderDetail)
  286. }
  287. for k, v := range missData {
  288. res[k] = v
  289. }
  290. if err != nil {
  291. return
  292. }
  293. for _, key := range keys {
  294. if res[key] == nil {
  295. if missData == nil {
  296. missData = make(map[int64]*model.OrderDetail, len(keys))
  297. }
  298. missData[key] = &model.OrderDetail{OrderID: 0}
  299. }
  300. }
  301. if !addCache {
  302. return
  303. }
  304. d.cache.Save(func() {
  305. d.AddCacheOrderDetails(metadata.WithContext(c), missData)
  306. })
  307. return
  308. }
  309. // OrderSKUs get data from cache if miss will call source method, then add to cache.
  310. func (d *Dao) OrderSKUs(c context.Context, keys []int64) (res map[int64][]*model.OrderSKU, err error) {
  311. if len(keys) == 0 {
  312. return
  313. }
  314. addCache := true
  315. res, err = d.CacheOrderSKUs(c, keys)
  316. if err != nil {
  317. addCache = false
  318. res = nil
  319. err = nil
  320. }
  321. var miss []int64
  322. for _, key := range keys {
  323. if (res == nil) || (len(res[key]) == 0) {
  324. miss = append(miss, key)
  325. }
  326. }
  327. prom.CacheHit.Add("OrderSKUs", int64(len(keys)-len(miss)))
  328. defer func() {
  329. for k, v := range res {
  330. if len(v) == 1 && v[0].OrderID == -1 {
  331. delete(res, k)
  332. }
  333. }
  334. }()
  335. if len(miss) == 0 {
  336. return
  337. }
  338. var missData map[int64][]*model.OrderSKU
  339. prom.CacheMiss.Add("OrderSKUs", int64(len(miss)))
  340. missData, err = d.RawOrderSKUs(c, miss)
  341. if res == nil {
  342. res = make(map[int64][]*model.OrderSKU)
  343. }
  344. for k, v := range missData {
  345. res[k] = v
  346. }
  347. if err != nil {
  348. return
  349. }
  350. for _, key := range keys {
  351. if len(res[key]) == 0 {
  352. if missData == nil {
  353. missData = make(map[int64][]*model.OrderSKU, len(keys))
  354. }
  355. missData[key] = []*model.OrderSKU{{OrderID: -1}}
  356. }
  357. }
  358. if !addCache {
  359. return
  360. }
  361. d.cache.Save(func() {
  362. d.AddCacheOrderSKUs(metadata.WithContext(c), missData)
  363. })
  364. return
  365. }
  366. // OrderPayCharges get data from cache if miss will call source method, then add to cache.
  367. func (d *Dao) OrderPayCharges(c context.Context, keys []int64) (res map[int64]*model.OrderPayCharge, err error) {
  368. if len(keys) == 0 {
  369. return
  370. }
  371. addCache := true
  372. res, err = d.CacheOrderPayCharges(c, keys)
  373. if err != nil {
  374. addCache = false
  375. res = nil
  376. err = nil
  377. }
  378. var miss []int64
  379. for _, key := range keys {
  380. if (res == nil) || (res[key] == nil) {
  381. miss = append(miss, key)
  382. }
  383. }
  384. prom.CacheHit.Add("OrderPayCharges", int64(len(keys)-len(miss)))
  385. defer func() {
  386. for k, v := range res {
  387. if v != nil && v.ChargeID == "" {
  388. delete(res, k)
  389. }
  390. }
  391. }()
  392. if len(miss) == 0 {
  393. return
  394. }
  395. var missData map[int64]*model.OrderPayCharge
  396. prom.CacheMiss.Add("OrderPayCharges", int64(len(miss)))
  397. missData, err = d.RawOrderPayCharges(c, miss)
  398. if res == nil {
  399. res = make(map[int64]*model.OrderPayCharge)
  400. }
  401. for k, v := range missData {
  402. res[k] = v
  403. }
  404. if err != nil {
  405. return
  406. }
  407. for _, key := range keys {
  408. if res[key] == nil {
  409. if missData == nil {
  410. missData = make(map[int64]*model.OrderPayCharge, len(keys))
  411. }
  412. missData[key] = &model.OrderPayCharge{ChargeID: ""}
  413. }
  414. }
  415. if !addCache {
  416. return
  417. }
  418. d.cache.Save(func() {
  419. d.AddCacheOrderPayCharges(metadata.WithContext(c), missData)
  420. })
  421. return
  422. }
  423. // SkuByItemID get data from cache if miss will call source method, then add to cache.
  424. func (d *Dao) SkuByItemID(c context.Context, id int64) (res map[string]*model.SKUStock, err error) {
  425. addCache := true
  426. res, err = d.CacheSkuByItemID(c, id)
  427. if err != nil {
  428. addCache = false
  429. err = nil
  430. }
  431. if len(res) != 0 {
  432. prom.CacheHit.Incr("SkuByItemID")
  433. return
  434. }
  435. prom.CacheMiss.Incr("SkuByItemID")
  436. res, err = d.RawSkuByItemID(c, id)
  437. if err != nil {
  438. return
  439. }
  440. miss := res
  441. if !addCache {
  442. return
  443. }
  444. d.cache.Save(func() {
  445. d.AddCacheSkuByItemID(metadata.WithContext(c), id, miss)
  446. })
  447. return
  448. }
  449. // GetSKUs get data from cache if miss will call source method, then add to cache.
  450. func (d *Dao) GetSKUs(c context.Context, keys []int64, withNewStock bool) (res map[int64]*model.SKUStock, err error) {
  451. if len(keys) == 0 {
  452. return
  453. }
  454. addCache := true
  455. res, err = d.CacheGetSKUs(c, keys, withNewStock)
  456. if err != nil {
  457. addCache = false
  458. res = nil
  459. err = nil
  460. }
  461. var miss []int64
  462. for _, key := range keys {
  463. if (res == nil) || (res[key] == nil) {
  464. miss = append(miss, key)
  465. }
  466. }
  467. prom.CacheHit.Add("GetSKUs", int64(len(keys)-len(miss)))
  468. if len(miss) == 0 {
  469. return
  470. }
  471. var missData map[int64]*model.SKUStock
  472. prom.CacheMiss.Add("GetSKUs", int64(len(miss)))
  473. missData, err = d.RawGetSKUs(c, miss, withNewStock)
  474. if res == nil {
  475. res = make(map[int64]*model.SKUStock)
  476. }
  477. for k, v := range missData {
  478. res[k] = v
  479. }
  480. if err != nil {
  481. return
  482. }
  483. if !addCache {
  484. return
  485. }
  486. d.cache.Save(func() {
  487. d.AddCacheGetSKUs(metadata.WithContext(c), missData, withNewStock)
  488. })
  489. return
  490. }
  491. // Stocks get data from cache if miss will call source method, then add to cache.
  492. func (d *Dao) Stocks(c context.Context, keys []int64, isLocked bool) (res map[int64]int64, err error) {
  493. if len(keys) == 0 {
  494. return
  495. }
  496. addCache := true
  497. res, err = d.CacheStocks(c, keys, isLocked)
  498. if err != nil {
  499. addCache = false
  500. res = nil
  501. err = nil
  502. }
  503. var miss []int64
  504. for _, key := range keys {
  505. if _, ok := res[key]; !ok {
  506. miss = append(miss, key)
  507. }
  508. }
  509. prom.CacheHit.Add("Stocks", int64(len(keys)-len(miss)))
  510. if len(miss) == 0 {
  511. return
  512. }
  513. var missData map[int64]int64
  514. prom.CacheMiss.Add("Stocks", int64(len(miss)))
  515. missData, err = d.RawStocks(c, miss, isLocked)
  516. if res == nil {
  517. res = make(map[int64]int64)
  518. }
  519. for k, v := range missData {
  520. res[k] = v
  521. }
  522. if err != nil {
  523. return
  524. }
  525. if !addCache {
  526. return
  527. }
  528. d.cache.Save(func() {
  529. d.AddCacheStocks(metadata.WithContext(c), missData, isLocked)
  530. })
  531. return
  532. }
  533. // TicketsByOrderID get data from cache if miss will call source method, then add to cache.
  534. func (d *Dao) TicketsByOrderID(c context.Context, id int64) (res []*model.Ticket, err error) {
  535. addCache := true
  536. res, err = d.CacheTicketsByOrderID(c, id)
  537. if err != nil {
  538. addCache = false
  539. err = nil
  540. }
  541. defer func() {
  542. if len(res) == 1 && res[0].ID == -1 {
  543. res = nil
  544. }
  545. }()
  546. if len(res) != 0 {
  547. prom.CacheHit.Incr("TicketsByOrderID")
  548. return
  549. }
  550. prom.CacheMiss.Incr("TicketsByOrderID")
  551. res, err = d.RawTicketsByOrderID(c, id)
  552. if err != nil {
  553. return
  554. }
  555. miss := res
  556. if len(miss) == 0 {
  557. miss = []*model.Ticket{{ID: -1}}
  558. }
  559. if !addCache {
  560. return
  561. }
  562. d.cache.Save(func() {
  563. d.AddCacheTicketsByOrderID(metadata.WithContext(c), id, miss)
  564. })
  565. return
  566. }
  567. // TicketsByScreen get data from cache if miss will call source method, then add to cache.
  568. func (d *Dao) TicketsByScreen(c context.Context, id int64, UID int64) (res []*model.Ticket, err error) {
  569. addCache := true
  570. res, err = d.CacheTicketsByScreen(c, id, UID)
  571. if err != nil {
  572. addCache = false
  573. err = nil
  574. }
  575. defer func() {
  576. if len(res) == 1 && res[0].ID == -1 {
  577. res = nil
  578. }
  579. }()
  580. if len(res) != 0 {
  581. prom.CacheHit.Incr("TicketsByScreen")
  582. return
  583. }
  584. prom.CacheMiss.Incr("TicketsByScreen")
  585. res, err = d.RawTicketsByScreen(c, id, UID)
  586. if err != nil {
  587. return
  588. }
  589. miss := res
  590. if len(miss) == 0 {
  591. miss = []*model.Ticket{{ID: -1}}
  592. }
  593. if !addCache {
  594. return
  595. }
  596. d.cache.Save(func() {
  597. d.AddCacheTicketsByScreen(metadata.WithContext(c), id, miss, UID)
  598. })
  599. return
  600. }
  601. // TicketsByID get data from cache if miss will call source method, then add to cache.
  602. func (d *Dao) TicketsByID(c context.Context, keys []int64) (res map[int64]*model.Ticket, err error) {
  603. if len(keys) == 0 {
  604. return
  605. }
  606. addCache := true
  607. res, err = d.CacheTicketsByID(c, keys)
  608. if err != nil {
  609. addCache = false
  610. res = nil
  611. err = nil
  612. }
  613. var miss []int64
  614. for _, key := range keys {
  615. if (res == nil) || (res[key] == nil) {
  616. miss = append(miss, key)
  617. }
  618. }
  619. prom.CacheHit.Add("TicketsByID", int64(len(keys)-len(miss)))
  620. defer func() {
  621. for k, v := range res {
  622. if v != nil && v.ID == -1 {
  623. delete(res, k)
  624. }
  625. }
  626. }()
  627. if len(miss) == 0 {
  628. return
  629. }
  630. var missData map[int64]*model.Ticket
  631. prom.CacheMiss.Add("TicketsByID", int64(len(miss)))
  632. missData, err = d.RawTicketsByID(c, miss)
  633. if res == nil {
  634. res = make(map[int64]*model.Ticket)
  635. }
  636. for k, v := range missData {
  637. res[k] = v
  638. }
  639. if err != nil {
  640. return
  641. }
  642. for _, key := range keys {
  643. if res[key] == nil {
  644. if missData == nil {
  645. missData = make(map[int64]*model.Ticket, len(keys))
  646. }
  647. missData[key] = &model.Ticket{ID: -1}
  648. }
  649. }
  650. if !addCache {
  651. return
  652. }
  653. d.cache.Save(func() {
  654. d.AddCacheTicketsByID(metadata.WithContext(c), missData)
  655. })
  656. return
  657. }
  658. // TicketSend get data from cache if miss will call source method, then add to cache.
  659. func (d *Dao) TicketSend(c context.Context, keys []int64, TIDType string) (res map[int64]*model.TicketSend, err error) {
  660. if len(keys) == 0 {
  661. return
  662. }
  663. addCache := true
  664. res, err = d.CacheTicketSend(c, keys, TIDType)
  665. if err != nil {
  666. addCache = false
  667. res = nil
  668. err = nil
  669. }
  670. var miss []int64
  671. for _, key := range keys {
  672. if (res == nil) || (res[key] == nil) {
  673. miss = append(miss, key)
  674. }
  675. }
  676. prom.CacheHit.Add("TicketSend", int64(len(keys)-len(miss)))
  677. defer func() {
  678. for k, v := range res {
  679. if v != nil && v.ID == -1 {
  680. delete(res, k)
  681. }
  682. }
  683. }()
  684. if len(miss) == 0 {
  685. return
  686. }
  687. var missData map[int64]*model.TicketSend
  688. prom.CacheMiss.Add("TicketSend", int64(len(miss)))
  689. missData, err = d.RawTicketSend(c, miss, TIDType)
  690. if res == nil {
  691. res = make(map[int64]*model.TicketSend)
  692. }
  693. for k, v := range missData {
  694. res[k] = v
  695. }
  696. if err != nil {
  697. return
  698. }
  699. for _, key := range keys {
  700. if res[key] == nil {
  701. if missData == nil {
  702. missData = make(map[int64]*model.TicketSend, len(keys))
  703. }
  704. missData[key] = &model.TicketSend{ID: -1}
  705. }
  706. }
  707. if !addCache {
  708. return
  709. }
  710. d.cache.Save(func() {
  711. d.AddCacheTicketSend(metadata.WithContext(c), missData, TIDType)
  712. })
  713. return
  714. }