mc.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/log"
  7. "go-common/library/stat/prom"
  8. "github.com/pkg/errors"
  9. )
  10. func orderKey(id string) string {
  11. return fmt.Sprintf("up_o_%s", id)
  12. }
  13. func assetKey(oid int64, otype string, currency string) string {
  14. return fmt.Sprintf("up_a_%d_%s_%s", oid, otype, currency)
  15. }
  16. func taskKey(task string) string {
  17. return fmt.Sprintf("up_t_%s", task)
  18. }
  19. // DelCacheOrderUser delete data from mc
  20. func (d *Dao) DelCacheOrderUser(c context.Context, orderID string) (err error) {
  21. conn := d.mc.Get(c)
  22. defer conn.Close()
  23. key := orderKey(orderID)
  24. if err = conn.Delete(key); err != nil {
  25. if err == memcache.ErrNotFound {
  26. err = nil
  27. return
  28. }
  29. prom.BusinessErrCount.Incr("mc:DelCacheOrderUser")
  30. log.Errorv(c, log.KV("DelCacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
  31. return
  32. }
  33. return
  34. }
  35. // DelCacheAsset delete data from mc
  36. func (d *Dao) DelCacheAsset(c context.Context, oid int64, otype string, currency string) (err error) {
  37. conn := d.mc.Get(c)
  38. defer conn.Close()
  39. key := assetKey(oid, otype, currency)
  40. if err = conn.Delete(key); err != nil {
  41. if err == memcache.ErrNotFound {
  42. err = nil
  43. return
  44. }
  45. prom.BusinessErrCount.Incr("mc:DelCacheAsset")
  46. log.Errorv(c, log.KV("DelCacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
  47. return
  48. }
  49. return
  50. }
  51. // AddCacheTask .
  52. func (d *Dao) AddCacheTask(c context.Context, task string, ttl int32) (ok bool, err error) {
  53. var (
  54. conn = d.mc.Get(c)
  55. item = &memcache.Item{
  56. Key: taskKey(task),
  57. Value: []byte{0},
  58. Expiration: ttl,
  59. }
  60. )
  61. defer conn.Close()
  62. if err = conn.Add(item); err != nil {
  63. if err == memcache.ErrNotStored {
  64. err = nil
  65. ok = false
  66. return
  67. }
  68. err = errors.WithStack(err)
  69. return
  70. }
  71. ok = true
  72. return
  73. }
  74. // DelCacheTask .
  75. func (d *Dao) DelCacheTask(c context.Context, task string) (err error) {
  76. var (
  77. conn = d.mc.Get(c)
  78. key = taskKey(task)
  79. )
  80. defer conn.Close()
  81. if err = conn.Delete(key); err != nil {
  82. if err == memcache.ErrNotFound {
  83. err = nil
  84. } else {
  85. err = errors.WithStack(err)
  86. }
  87. }
  88. return
  89. }
  90. // ugcpay-rank
  91. func elecUserSetting(mid int64) string {
  92. return fmt.Sprintf("eus_%d", mid)
  93. }
  94. // DelCacheUserSetting .
  95. func (d *Dao) DelCacheUserSetting(c context.Context, mid int64) (err error) {
  96. var (
  97. conn = d.mcRank.Get(c)
  98. key = elecUserSetting(mid)
  99. )
  100. defer conn.Close()
  101. if err = conn.Delete(key); err != nil {
  102. if err == memcache.ErrNotFound {
  103. err = nil
  104. } else {
  105. err = errors.WithStack(err)
  106. }
  107. }
  108. return
  109. }