memcache.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package pendant
  2. import (
  3. "context"
  4. "fmt"
  5. gmc "go-common/library/cache/memcache"
  6. )
  7. const (
  8. _prefixRedPointFlag = "r_p_f_%d"
  9. )
  10. func redPointFlagKey(mid int64) string {
  11. return fmt.Sprintf(_prefixRedPointFlag, mid)
  12. }
  13. // RedPointCache get new pendant info red point cache.
  14. func (d *Dao) RedPointCache(c context.Context, mid int64) (pid int64, err error) {
  15. conn := d.mc.Get(c)
  16. defer conn.Close()
  17. item, err := conn.Get(redPointFlagKey(mid))
  18. if err != nil {
  19. if err == gmc.ErrNotFound {
  20. err = nil
  21. }
  22. return
  23. }
  24. err = conn.Scan(item, &pid)
  25. return
  26. }
  27. // SetRedPointCache set red point cache.
  28. func (d *Dao) SetRedPointCache(c context.Context, mid, pid int64) (err error) {
  29. var (
  30. item = &gmc.Item{Key: redPointFlagKey(mid), Object: pid, Expiration: d.pointExpire, Flags: gmc.FlagJSON}
  31. conn = d.mc.Get(c)
  32. )
  33. defer conn.Close()
  34. err = conn.Set(item)
  35. return
  36. }
  37. // DelRedPointCache delete new pendant info red point cache.
  38. func (d *Dao) DelRedPointCache(c context.Context, mid int64) (err error) {
  39. conn := d.mc.Get(c)
  40. defer conn.Close()
  41. if err = conn.Delete(redPointFlagKey(mid)); err != nil {
  42. if err == gmc.ErrNotFound {
  43. err = nil
  44. }
  45. }
  46. return
  47. }