mc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/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. func oauth2Key(platform string, openID string) string {
  13. return fmt.Sprintf("sns_oauth2_%s_%s", platform, openID)
  14. }
  15. // SetSnsCache set sns to cache
  16. func (d *Dao) SetSnsCache(c context.Context, mid int64, platform string, sns *model.SnsProto) (err error) {
  17. key := snsKey(platform, mid)
  18. conn := d.mc.Get(c)
  19. defer conn.Close()
  20. item := &memcache.Item{Key: key, Object: sns, Flags: memcache.FlagProtobuf, Expiration: d.mcExpire}
  21. if err = conn.Set(item); err != nil {
  22. log.Error("fail to set sns to mc, key(%s) expire(%d) error(%+v)", key, d.mcExpire, err)
  23. }
  24. return
  25. }
  26. // SetOauth2Cache set oauth2 info to cache
  27. func (d *Dao) SetOauth2Cache(c context.Context, openID, platform string, sns *model.Oauth2Proto) (err error) {
  28. key := oauth2Key(platform, openID)
  29. conn := d.mc.Get(c)
  30. defer conn.Close()
  31. item := &memcache.Item{Key: key, Object: sns, Flags: memcache.FlagProtobuf, Expiration: 300}
  32. if err = conn.Set(item); err != nil {
  33. log.Error("fail to set oauth2 info to mc, key(%s) expire(%d) error(%+v)", key, 300, err)
  34. }
  35. return
  36. }
  37. // SnsCache sns cache
  38. func (d *Dao) SnsCache(c context.Context, mid int64, platform string) (res *model.SnsProto, err error) {
  39. key := snsKey(platform, mid)
  40. conn := d.mc.Get(c)
  41. defer conn.Close()
  42. item, err := conn.Get(key)
  43. if err != nil {
  44. if err == memcache.ErrNotFound {
  45. return nil, nil
  46. }
  47. log.Error("conn.Get(%s) error(%v)", key, err)
  48. return nil, err
  49. }
  50. res = new(model.SnsProto)
  51. if err = conn.Scan(item, res); err != nil {
  52. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  53. return nil, err
  54. }
  55. return res, nil
  56. }
  57. // Oauth2Cache oauth2 info cache
  58. func (d *Dao) Oauth2Cache(c context.Context, openID, platform string) (res *model.Oauth2Proto, err error) {
  59. key := oauth2Key(platform, openID)
  60. conn := d.mc.Get(c)
  61. defer conn.Close()
  62. item, err := conn.Get(key)
  63. if err != nil {
  64. if err == memcache.ErrNotFound {
  65. return nil, nil
  66. }
  67. log.Error("conn.Get(%s) error(%v)", key, err)
  68. return nil, err
  69. }
  70. res = new(model.Oauth2Proto)
  71. if err = conn.Scan(item, res); err != nil {
  72. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  73. return nil, err
  74. }
  75. return res, nil
  76. }
  77. // DelSnsCache del sns cache
  78. func (d *Dao) DelSnsCache(c context.Context, mid int64, platform string) (err error) {
  79. key := snsKey(platform, mid)
  80. conn := d.mc.Get(c)
  81. defer conn.Close()
  82. if err = conn.Delete(key); err != nil {
  83. if err == memcache.ErrNotFound {
  84. err = nil
  85. return
  86. }
  87. log.Error("fail to del sns cache, key(%s) error(%+v)", key, err)
  88. }
  89. return
  90. }
  91. // DelOauth2Cache del oauth2 cache
  92. func (d *Dao) DelOauth2Cache(c context.Context, openID, platform string) (err error) {
  93. key := oauth2Key(platform, openID)
  94. conn := d.mc.Get(c)
  95. defer conn.Close()
  96. if err = conn.Delete(key); err != nil {
  97. if err == memcache.ErrNotFound {
  98. err = nil
  99. return
  100. }
  101. log.Error("fail to del oauth2 cache, key(%s) error(%+v)", key, err)
  102. }
  103. return
  104. }