mc.cache.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
  2. /*
  3. Package dao is a generated mc cache package.
  4. It is generated from:
  5. type _mc interface {
  6. // get user coin count.
  7. // mc: -key=countKey
  8. CacheUserCoin(c context.Context, mid int64) (count float64, err error)
  9. // set user coin count
  10. // mc: -key=countKey -expire=d.mcExpire
  11. AddCacheUserCoin(c context.Context, mid int64, count float64) (err error)
  12. // mc: -key=itemCoinKey
  13. CacheItemCoin(c context.Context, aid int64, tp int64) (count int64, err error)
  14. // mc: -key=itemCoinKey -expire=d.mcExpire
  15. AddCacheItemCoin(c context.Context, aid int64, count int64, tp int64) (err error)
  16. // mc: -key=expKey -type=get
  17. Exp(c context.Context, mid int64) (exp int64, err error)
  18. // mc: -key=expKey -expire=d.expireExp
  19. SetTodayExpCache(c context.Context, mid int64, exp int64) (err error)
  20. }
  21. */
  22. package dao
  23. import (
  24. "context"
  25. "fmt"
  26. "strconv"
  27. "go-common/library/cache/memcache"
  28. "go-common/library/log"
  29. "go-common/library/stat/prom"
  30. )
  31. var _ _mc
  32. // CacheUserCoin get user coin count.
  33. func (d *Dao) CacheUserCoin(c context.Context, id int64) (res float64, err error) {
  34. conn := d.mc.Get(c)
  35. defer conn.Close()
  36. key := countKey(id)
  37. reply, err := conn.Get(key)
  38. if err != nil {
  39. if err == memcache.ErrNotFound {
  40. err = nil
  41. return
  42. }
  43. prom.BusinessErrCount.Incr("mc:CacheUserCoin")
  44. log.Errorv(c, log.KV("CacheUserCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  45. return
  46. }
  47. var v string
  48. err = conn.Scan(reply, &v)
  49. if err != nil {
  50. prom.BusinessErrCount.Incr("mc:CacheUserCoin")
  51. log.Errorv(c, log.KV("CacheUserCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  52. return
  53. }
  54. r, err := strconv.ParseFloat(v, 64)
  55. if err != nil {
  56. prom.BusinessErrCount.Incr("mc:CacheUserCoin")
  57. log.Errorv(c, log.KV("CacheUserCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  58. return
  59. }
  60. res = float64(r)
  61. return
  62. }
  63. // AddCacheUserCoin set user coin count
  64. func (d *Dao) AddCacheUserCoin(c context.Context, id int64, val float64) (err error) {
  65. conn := d.mc.Get(c)
  66. defer conn.Close()
  67. key := countKey(id)
  68. bs := []byte(strconv.FormatFloat(val, 'E', -1, 64))
  69. item := &memcache.Item{Key: key, Value: bs, Expiration: d.mcExpire, Flags: memcache.FlagRAW}
  70. if err = conn.Set(item); err != nil {
  71. prom.BusinessErrCount.Incr("mc:AddCacheUserCoin")
  72. log.Errorv(c, log.KV("AddCacheUserCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  73. return
  74. }
  75. return
  76. }
  77. // CacheItemCoin get data from mc
  78. func (d *Dao) CacheItemCoin(c context.Context, id int64, tp int64) (res int64, err error) {
  79. conn := d.mc.Get(c)
  80. defer conn.Close()
  81. key := itemCoinKey(id, tp)
  82. reply, err := conn.Get(key)
  83. if err != nil {
  84. if err == memcache.ErrNotFound {
  85. err = nil
  86. return
  87. }
  88. prom.BusinessErrCount.Incr("mc:CacheItemCoin")
  89. log.Errorv(c, log.KV("CacheItemCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  90. return
  91. }
  92. var v string
  93. err = conn.Scan(reply, &v)
  94. if err != nil {
  95. prom.BusinessErrCount.Incr("mc:CacheItemCoin")
  96. log.Errorv(c, log.KV("CacheItemCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  97. return
  98. }
  99. r, err := strconv.ParseInt(v, 10, 64)
  100. if err != nil {
  101. prom.BusinessErrCount.Incr("mc:CacheItemCoin")
  102. log.Errorv(c, log.KV("CacheItemCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  103. return
  104. }
  105. res = int64(r)
  106. return
  107. }
  108. // AddCacheItemCoin Set data to mc
  109. func (d *Dao) AddCacheItemCoin(c context.Context, id int64, val int64, tp int64) (err error) {
  110. conn := d.mc.Get(c)
  111. defer conn.Close()
  112. key := itemCoinKey(id, tp)
  113. bs := []byte(strconv.FormatInt(int64(val), 10))
  114. item := &memcache.Item{Key: key, Value: bs, Expiration: d.mcExpire, Flags: memcache.FlagRAW}
  115. if err = conn.Set(item); err != nil {
  116. prom.BusinessErrCount.Incr("mc:AddCacheItemCoin")
  117. log.Errorv(c, log.KV("AddCacheItemCoin", fmt.Sprintf("%+v", err)), log.KV("key", key))
  118. return
  119. }
  120. return
  121. }
  122. // Exp get data from mc
  123. func (d *Dao) Exp(c context.Context, id int64) (res int64, err error) {
  124. conn := d.mc.Get(c)
  125. defer conn.Close()
  126. key := expKey(id)
  127. reply, err := conn.Get(key)
  128. if err != nil {
  129. if err == memcache.ErrNotFound {
  130. err = nil
  131. return
  132. }
  133. prom.BusinessErrCount.Incr("mc:Exp")
  134. log.Errorv(c, log.KV("Exp", fmt.Sprintf("%+v", err)), log.KV("key", key))
  135. return
  136. }
  137. var v string
  138. err = conn.Scan(reply, &v)
  139. if err != nil {
  140. prom.BusinessErrCount.Incr("mc:Exp")
  141. log.Errorv(c, log.KV("Exp", fmt.Sprintf("%+v", err)), log.KV("key", key))
  142. return
  143. }
  144. r, err := strconv.ParseInt(v, 10, 64)
  145. if err != nil {
  146. prom.BusinessErrCount.Incr("mc:Exp")
  147. log.Errorv(c, log.KV("Exp", fmt.Sprintf("%+v", err)), log.KV("key", key))
  148. return
  149. }
  150. res = int64(r)
  151. return
  152. }
  153. // SetTodayExpCache Set data to mc
  154. func (d *Dao) SetTodayExpCache(c context.Context, id int64, val int64) (err error) {
  155. conn := d.mc.Get(c)
  156. defer conn.Close()
  157. key := expKey(id)
  158. bs := []byte(strconv.FormatInt(int64(val), 10))
  159. item := &memcache.Item{Key: key, Value: bs, Expiration: d.expireExp, Flags: memcache.FlagRAW}
  160. if err = conn.Set(item); err != nil {
  161. prom.BusinessErrCount.Incr("mc:SetTodayExpCache")
  162. log.Errorv(c, log.KV("SetTodayExpCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  163. return
  164. }
  165. return
  166. }