memcache.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. gmc "go-common/library/cache/memcache"
  6. "go-common/library/log"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _prefixCoupons = "cs:%d:%d"
  11. _prefixCouponBlance = "cbl:%d:%d"
  12. _prefixprizeCard = "nypc:%d:%d:%d" // 元旦活动卡片
  13. _prefixprizeCards = "nypcs:%d:%d" // 元旦活动卡片列表
  14. )
  15. func couponBalancesKey(mid int64, ct int8) string {
  16. return fmt.Sprintf(_prefixCouponBlance, ct, mid)
  17. }
  18. // DelCouponBalancesCache delete user coupons blance cache.
  19. func (d *Dao) DelCouponBalancesCache(c context.Context, mid int64, ct int8) (err error) {
  20. return d.delCache(c, couponBalancesKey(mid, ct))
  21. }
  22. func couponsKey(mid int64, ct int8) string {
  23. return fmt.Sprintf(_prefixCoupons, ct, mid)
  24. }
  25. func prizeCardKey(mid, actID int64, cardType int8) string {
  26. return fmt.Sprintf(_prefixprizeCard, mid, actID, cardType)
  27. }
  28. func prizeCardsKey(mid, actID int64) string {
  29. return fmt.Sprintf(_prefixprizeCards, mid, actID)
  30. }
  31. // DelCouponsCache delete user coupons cache.
  32. func (d *Dao) DelCouponsCache(c context.Context, mid int64, ct int8) (err error) {
  33. return d.delCache(c, couponsKey(mid, ct))
  34. }
  35. // DelCache del cache.
  36. func (d *Dao) delCache(c context.Context, key string) (err error) {
  37. conn := d.mc.Get(c)
  38. defer conn.Close()
  39. if err = conn.Delete(key); err != nil {
  40. if err == gmc.ErrNotFound {
  41. log.Warn("delCache ErrNotFound(%s)", key)
  42. err = nil
  43. } else {
  44. err = errors.Wrapf(err, "mc.Delete(%s)", key)
  45. }
  46. }
  47. return
  48. }
  49. // DelPrizeCardKey .
  50. func (d *Dao) DelPrizeCardKey(c context.Context, mid, actID int64, cardType int8) (err error) {
  51. return d.delCache(c, prizeCardKey(mid, actID, cardType))
  52. }
  53. // DelPrizeCardsKey .
  54. func (d *Dao) DelPrizeCardsKey(c context.Context, mid, actID int64) (err error) {
  55. log.Warn("DelPrizeCardsKey(%s)", prizeCardsKey(mid, actID))
  56. return d.delCache(c, prizeCardsKey(mid, actID))
  57. }