elec_mc.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/ugcpay/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "go-common/library/stat/prom"
  9. )
  10. func elecOrderIDKey(orderID string) string {
  11. return fmt.Sprintf("ur_eoi_%s", orderID)
  12. }
  13. // AddCacheOrderID Set data to mc
  14. func (d *Dao) AddCacheOrderID(c context.Context, orderID string) (ok bool, err error) {
  15. ok = true
  16. conn := d.mc.Get(c)
  17. defer conn.Close()
  18. key := elecOrderIDKey(orderID)
  19. item := &memcache.Item{Key: key, Object: struct{}{}, Expiration: conf.Conf.CacheTTL.ElecOrderIDTTL, Flags: memcache.FlagJSON}
  20. if err = conn.Add(item); err != nil {
  21. if err == memcache.ErrNotStored {
  22. ok = false
  23. err = nil
  24. return
  25. }
  26. prom.BusinessErrCount.Incr("mc:AddCacheOrderID")
  27. log.Errorv(c, log.KV("AddCacheOrderID", fmt.Sprintf("%+v", err)), log.KV("key", key))
  28. return
  29. }
  30. return
  31. }
  32. // DelCacheOrderID .
  33. func (d *Dao) DelCacheOrderID(c context.Context, orderID string) (err error) {
  34. conn := d.mc.Get(c)
  35. defer conn.Close()
  36. key := elecOrderIDKey(orderID)
  37. if err = conn.Delete(key); err != nil {
  38. if err == memcache.ErrNotFound {
  39. err = nil
  40. return
  41. }
  42. prom.BusinessErrCount.Incr("mc:DelCacheOrderID")
  43. log.Errorv(c, log.KV("DelCacheOrderID", fmt.Sprintf("%+v", err)), log.KV("key", key))
  44. return
  45. }
  46. return
  47. }