memcache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/spy/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _userKey = "u_%d"
  11. )
  12. // pingMC ping memcache.
  13. func (d *Dao) pingMC(c context.Context) (err error) {
  14. conn := d.mcUser.Get(c)
  15. defer conn.Close()
  16. if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
  17. log.Error("conn.Store(set,ping,1) error(%v)", err)
  18. }
  19. return
  20. }
  21. func userInfoCacheKey(mid int64) string {
  22. return fmt.Sprintf(_userKey, mid)
  23. }
  24. // UserInfoCache get user info to cache.
  25. func (d *Dao) UserInfoCache(c context.Context, mid int64) (ui *model.UserInfo, err error) {
  26. var (
  27. key = userInfoCacheKey(mid)
  28. conn = d.mcUser.Get(c)
  29. )
  30. defer conn.Close()
  31. reply, err := conn.Get(key)
  32. if err != nil {
  33. if err == memcache.ErrNotFound {
  34. err = nil
  35. return
  36. }
  37. log.Error("conn.Get(get, %s) error(%v)", key, err)
  38. return
  39. }
  40. ui = &model.UserInfo{}
  41. if err = conn.Scan(reply, ui); err != nil {
  42. log.Error("reply.Scan(%s) error(%v)", string(reply.Value), err)
  43. }
  44. return
  45. }
  46. // AddUserInfoCache add info to cache , return err if key is exist already.
  47. func (d *Dao) AddUserInfoCache(c context.Context, ui *model.UserInfo) (err error) {
  48. if ui == nil {
  49. return fmt.Errorf("AddUserInfoCache got nil *model.UserInfo")
  50. }
  51. var (
  52. key = userInfoCacheKey(ui.Mid)
  53. conn = d.mcUser.Get(c)
  54. )
  55. defer conn.Close()
  56. if err = conn.Add(&memcache.Item{Key: key, Object: ui, Expiration: d.mcUserExpire, Flags: memcache.FlagJSON}); err != nil {
  57. log.Error("conn.Add(%s, %v) error(%v)", key, ui, err)
  58. return
  59. }
  60. return
  61. }
  62. // SetUserInfoCache set info cache.
  63. func (d *Dao) SetUserInfoCache(c context.Context, ui *model.UserInfo) (err error) {
  64. if ui == nil {
  65. return fmt.Errorf("SetUserInfoCache got nil *model.UserInfo")
  66. }
  67. var (
  68. key = userInfoCacheKey(ui.Mid)
  69. conn = d.mcUser.Get(c)
  70. )
  71. defer conn.Close()
  72. if err = conn.Set(&memcache.Item{Key: key, Object: ui, Expiration: d.mcUserExpire, Flags: memcache.FlagJSON}); err != nil {
  73. log.Error("conn.Set(%s, %v) error(%v)", key, ui, err)
  74. }
  75. return
  76. }
  77. // DelInfoCache delete info cache.
  78. func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
  79. var (
  80. key = userInfoCacheKey(mid)
  81. conn = d.mcUser.Get(c)
  82. )
  83. defer conn.Close()
  84. if err = conn.Delete(key); err != nil {
  85. if err == memcache.ErrNotFound {
  86. err = nil
  87. return
  88. }
  89. log.Error("conn.Delete(%s) error(%v)", key, err)
  90. }
  91. return
  92. }