memcache.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/identify/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. var (
  10. loginCacheValue = []byte("1")
  11. )
  12. // SetAccessCache .
  13. func (d *Dao) SetAccessCache(c context.Context, key string, res *model.IdentifyInfo) {
  14. conn := d.mc.Get(c)
  15. defer conn.Close()
  16. key = cacheKey(key)
  17. item := &memcache.Item{Key: key, Object: res, Flags: memcache.FlagProtobuf, Expiration: res.Expires}
  18. if err := conn.Set(item); err != nil {
  19. log.Error("identify set error(%s,%d,%v)", key, res.Expires, err)
  20. }
  21. }
  22. // AccessCache .
  23. func (d *Dao) AccessCache(c context.Context, key string) (res *model.IdentifyInfo, err error) {
  24. conn := d.mc.Get(c)
  25. defer conn.Close()
  26. key = cacheKey(key)
  27. r, err := conn.Get(key)
  28. if err != nil {
  29. if err == memcache.ErrNotFound {
  30. missedCount.Incr("access_cache")
  31. err = nil
  32. return
  33. }
  34. log.Error("conn.Get(%s) error(%v)", key, err)
  35. return
  36. }
  37. res = &model.IdentifyInfo{}
  38. if err = conn.Scan(r, res); err != nil {
  39. PromError("mc:json解析失败")
  40. log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
  41. return
  42. }
  43. cachedCount.Incr("access_cache")
  44. return
  45. }
  46. // DelCache delete access cache.
  47. func (d *Dao) DelCache(c context.Context, key string) (err error) {
  48. conn := d.mc.Get(c)
  49. defer conn.Close()
  50. key = cacheKey(key)
  51. if err = conn.Delete(key); err != nil {
  52. if err == memcache.ErrNotFound {
  53. err = nil
  54. return
  55. }
  56. log.Error("dao.DelCache(%s) error(%v)", key, err)
  57. }
  58. return
  59. }
  60. func cacheKey(key string) string {
  61. return fmt.Sprintf("i_%s", key)
  62. }
  63. func loginCacheKey(mid int64, ip string) string {
  64. return fmt.Sprintf("l%d%s", mid, ip)
  65. }
  66. // SetLoginCache set login cache
  67. func (d *Dao) SetLoginCache(c context.Context, mid int64, ip string, expires int32) (err error) {
  68. key := loginCacheKey(mid, ip)
  69. conn := d.mcLogin.Get(c)
  70. defer conn.Close()
  71. item := &memcache.Item{Key: key, Value: loginCacheValue, Flags: memcache.FlagRAW, Expiration: expires}
  72. // use Add instead of Set
  73. if err = conn.Set(item); err != nil {
  74. log.Error("loginCache set error(%s,%v)", key, err)
  75. }
  76. return
  77. }
  78. // ExistMIDAndIP check is exist mid
  79. func (d *Dao) ExistMIDAndIP(c context.Context, mid int64, ip string) (ok bool, err error) {
  80. key := loginCacheKey(mid, ip)
  81. conn := d.mcLogin.Get(c)
  82. defer conn.Close()
  83. _, err = conn.Get(key)
  84. if err != nil {
  85. if err == memcache.ErrNotFound {
  86. missedCount.Incr("isExistMID")
  87. err = nil
  88. return false, nil
  89. }
  90. log.Error("loginCache conn.Get(%s) error(%v)", key, err)
  91. return
  92. }
  93. return true, nil
  94. }