dao.cache.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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=-1 -check_null_code=$==-1 -singleflight=true
  7. UserCoin(c context.Context, mid int64) (count float64, err error)
  8. // cache: -nullcache=-1 -check_null_code=$==-1 -singleflight=true
  9. ItemCoin(c context.Context, aid int64, tp int64) (count int64, err error)
  10. }
  11. */
  12. package dao
  13. import (
  14. "context"
  15. "go-common/library/stat/prom"
  16. "golang.org/x/sync/singleflight"
  17. )
  18. var _ _cache
  19. var cacheSingleFlights = [2]*singleflight.Group{{}, {}}
  20. // UserCoin get data from cache if miss will call source method, then add to cache.
  21. func (d *Dao) UserCoin(c context.Context, id int64) (res float64, err error) {
  22. addCache := true
  23. res, err = d.CacheUserCoin(c, id)
  24. if err != nil {
  25. addCache = false
  26. err = nil
  27. }
  28. defer func() {
  29. if res == -1 {
  30. res = 0
  31. }
  32. }()
  33. if res != 0 {
  34. prom.CacheHit.Incr("UserCoin")
  35. return
  36. }
  37. var rr interface{}
  38. sf := d.cacheSFUserCoin(id)
  39. rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
  40. prom.CacheMiss.Incr("UserCoin")
  41. r, e = d.RawUserCoin(c, id)
  42. return
  43. })
  44. res = rr.(float64)
  45. if err != nil {
  46. return
  47. }
  48. miss := res
  49. if miss == 0 {
  50. miss = -1
  51. }
  52. if !addCache {
  53. return
  54. }
  55. d.cache.Do(c, func(c context.Context) {
  56. d.AddCacheUserCoin(c, id, miss)
  57. })
  58. return
  59. }
  60. // ItemCoin get data from cache if miss will call source method, then add to cache.
  61. func (d *Dao) ItemCoin(c context.Context, id int64, tp int64) (res int64, err error) {
  62. addCache := true
  63. res, err = d.CacheItemCoin(c, id, tp)
  64. if err != nil {
  65. addCache = false
  66. err = nil
  67. }
  68. defer func() {
  69. if res == -1 {
  70. res = 0
  71. }
  72. }()
  73. if res != 0 {
  74. prom.CacheHit.Incr("ItemCoin")
  75. return
  76. }
  77. var rr interface{}
  78. sf := d.cacheSFItemCoin(id, tp)
  79. rr, err, _ = cacheSingleFlights[1].Do(sf, func() (r interface{}, e error) {
  80. prom.CacheMiss.Incr("ItemCoin")
  81. r, e = d.RawItemCoin(c, id, tp)
  82. return
  83. })
  84. res = rr.(int64)
  85. if err != nil {
  86. return
  87. }
  88. miss := res
  89. if miss == 0 {
  90. miss = -1
  91. }
  92. if !addCache {
  93. return
  94. }
  95. d.cache.Do(c, func(c context.Context) {
  96. d.AddCacheItemCoin(c, id, miss, tp)
  97. })
  98. return
  99. }