mc.cache.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/ugcpay/conf"
  6. "go-common/app/service/main/ugcpay/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "go-common/library/stat/prom"
  10. )
  11. var _ _mc
  12. // CacheOrderUser get data from mc
  13. func (d *Dao) CacheOrderUser(c context.Context, id string) (res *model.Order, err error) {
  14. conn := d.mc.Get(c)
  15. defer conn.Close()
  16. key := orderKey(id)
  17. reply, err := conn.Get(key)
  18. if err != nil {
  19. if err == memcache.ErrNotFound {
  20. err = nil
  21. return
  22. }
  23. prom.BusinessErrCount.Incr("mc:CacheOrderUser")
  24. log.Errorv(c, log.KV("CacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
  25. return
  26. }
  27. res = &model.Order{}
  28. err = conn.Scan(reply, res)
  29. if err != nil {
  30. prom.BusinessErrCount.Incr("mc:CacheOrderUser")
  31. log.Errorv(c, log.KV("CacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
  32. return
  33. }
  34. return
  35. }
  36. // AddCacheOrderUser Set data to mc
  37. func (d *Dao) AddCacheOrderUser(c context.Context, id string, val *model.Order) (err error) {
  38. if val == nil {
  39. return
  40. }
  41. conn := d.mc.Get(c)
  42. defer conn.Close()
  43. key := orderKey(id)
  44. item := &memcache.Item{Key: key, Object: val, Expiration: conf.Conf.CacheTTL.OrderTTL, Flags: memcache.FlagJSON}
  45. if err = conn.Set(item); err != nil {
  46. prom.BusinessErrCount.Incr("mc:AddCacheOrderUser")
  47. log.Errorv(c, log.KV("AddCacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
  48. return
  49. }
  50. return
  51. }
  52. // DelCacheOrderUser delete data from mc
  53. func (d *Dao) DelCacheOrderUser(c context.Context, id string) (err error) {
  54. conn := d.mc.Get(c)
  55. defer conn.Close()
  56. key := orderKey(id)
  57. if err = conn.Delete(key); err != nil {
  58. if err == memcache.ErrNotFound {
  59. err = nil
  60. return
  61. }
  62. prom.BusinessErrCount.Incr("mc:DelCacheOrderUser")
  63. log.Errorv(c, log.KV("DelCacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
  64. return
  65. }
  66. return
  67. }
  68. // CacheAsset get data from mc
  69. func (d *Dao) CacheAsset(c context.Context, id int64, otype string, currency string) (res *model.Asset, err error) {
  70. conn := d.mc.Get(c)
  71. defer conn.Close()
  72. key := assetKey(id, otype, currency)
  73. reply, err := conn.Get(key)
  74. if err != nil {
  75. if err == memcache.ErrNotFound {
  76. err = nil
  77. return
  78. }
  79. prom.BusinessErrCount.Incr("mc:CacheAsset")
  80. log.Errorv(c, log.KV("CacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
  81. return
  82. }
  83. res = &model.Asset{}
  84. err = conn.Scan(reply, res)
  85. if err != nil {
  86. prom.BusinessErrCount.Incr("mc:CacheAsset")
  87. log.Errorv(c, log.KV("CacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
  88. return
  89. }
  90. return
  91. }
  92. // AddCacheAsset Set data to mc
  93. func (d *Dao) AddCacheAsset(c context.Context, id int64, otype string, currency string, value *model.Asset) (err error) {
  94. if value == nil {
  95. return
  96. }
  97. conn := d.mc.Get(c)
  98. defer conn.Close()
  99. key := assetKey(id, otype, currency)
  100. item := &memcache.Item{Key: key, Object: value, Expiration: conf.Conf.CacheTTL.AssetTTL, Flags: memcache.FlagJSON}
  101. if err = conn.Set(item); err != nil {
  102. prom.BusinessErrCount.Incr("mc:AddCacheAsset")
  103. log.Errorv(c, log.KV("AddCacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
  104. return
  105. }
  106. return
  107. }
  108. // DelCacheAsset delete data from mc
  109. func (d *Dao) DelCacheAsset(c context.Context, id int64, otype string, currency string) (err error) {
  110. conn := d.mc.Get(c)
  111. defer conn.Close()
  112. key := assetKey(id, otype, currency)
  113. if err = conn.Delete(key); err != nil {
  114. if err == memcache.ErrNotFound {
  115. err = nil
  116. return
  117. }
  118. prom.BusinessErrCount.Incr("mc:DelCacheAsset")
  119. log.Errorv(c, log.KV("DelCacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
  120. return
  121. }
  122. return
  123. }