mc.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/passport-sns/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. func snsKey(platform string, mid int64) string {
  10. return fmt.Sprintf("sns_%s_%d", platform, mid)
  11. }
  12. // SetSnsCache set sns to cache
  13. func (d *Dao) SetSnsCache(c context.Context, mid int64, platform string, sns *model.SnsProto) (err error) {
  14. key := snsKey(platform, mid)
  15. conn := d.mc.Get(c)
  16. defer conn.Close()
  17. item := &memcache.Item{Key: key, Object: sns, Flags: memcache.FlagProtobuf, Expiration: d.mcExpire}
  18. if err = conn.Set(item); err != nil {
  19. log.Error("fail to set sns to mc, key(%s) expire(%d) error(%+v)", key, d.mcExpire, err)
  20. }
  21. return
  22. }
  23. // DelSnsCache del sns cache
  24. func (d *Dao) DelSnsCache(c context.Context, mid int64, platform string) (err error) {
  25. key := snsKey(platform, mid)
  26. conn := d.mc.Get(c)
  27. defer conn.Close()
  28. if err = conn.Delete(key); err != nil {
  29. if err == memcache.ErrNotFound {
  30. err = nil
  31. return
  32. }
  33. log.Error("fail to del sns cache, key(%s) error(%+v)", key, err)
  34. }
  35. return
  36. }
  37. // GetUnionIDCache .
  38. func (d *Dao) GetUnionIDCache(c context.Context, key string) (v string, err error) {
  39. conn := d.mc.Get(c)
  40. r, err := conn.Get(key)
  41. conn.Close()
  42. if err != nil {
  43. if err == memcache.ErrNotFound {
  44. err = nil
  45. return
  46. }
  47. log.Error("GetUnionIDCache, key(%s) error(%+v)", key, err)
  48. return
  49. }
  50. v = string(r.Value)
  51. return
  52. }