mc.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/job/main/passport-game-cloud/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _keyPrefixInfoPB = "pa2_"
  11. _keyPrefixTokenPB = "pt_"
  12. )
  13. func keyInfoPB(mid int64) string {
  14. return _keyPrefixInfoPB + strconv.FormatInt(mid, 10)
  15. }
  16. func keyTokenPB(accessToken string) string {
  17. return _keyPrefixTokenPB + accessToken
  18. }
  19. // pingMC check connection success.
  20. func (d *Dao) pingMC(c context.Context) (err error) {
  21. item := &memcache.Item{
  22. Key: "ping",
  23. Value: []byte{1},
  24. Expiration: d.mcExpire,
  25. }
  26. conn := d.mc.Get(c)
  27. defer conn.Close()
  28. if err = conn.Set(item); err != nil {
  29. log.Error("conn.Set(%v) error(%v)", item, err)
  30. }
  31. return
  32. }
  33. // DelInfoCache delete info cache.
  34. func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
  35. key := keyInfoPB(mid)
  36. conn := d.mc.Get(c)
  37. defer conn.Close()
  38. if err = conn.Delete(key); err != nil {
  39. if err == memcache.ErrNotFound {
  40. err = nil
  41. return
  42. }
  43. log.Error("conn.Delete(%s) error(%v)", key, err)
  44. }
  45. return
  46. }
  47. // SetTokenCache set token into cache.
  48. func (d *Dao) SetTokenCache(c context.Context, token *model.Perm) (err error) {
  49. item := &memcache.Item{
  50. Key: keyTokenPB(token.AccessToken),
  51. Object: token,
  52. Flags: memcache.FlagProtobuf,
  53. Expiration: d.mcExpire,
  54. }
  55. conn := d.mc.Get(c)
  56. defer conn.Close()
  57. if err = conn.Set(item); err != nil {
  58. log.Error("conn.Set(%s) error(%v)", item.Key, err)
  59. }
  60. return
  61. }
  62. // DelTokenCache delete token cache.
  63. func (d *Dao) DelTokenCache(c context.Context, accessToken string) (err error) {
  64. key := keyTokenPB(accessToken)
  65. conn := d.mc.Get(c)
  66. defer conn.Close()
  67. if err = conn.Delete(key); err != nil {
  68. if err == memcache.ErrNotFound {
  69. err = nil
  70. return
  71. }
  72. log.Error("conn.Delete(%s) error(%v)", key, err)
  73. }
  74. return
  75. }