member.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/job/main/member-cache/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _expPrefix = "exp_%d"
  13. _expExpire = 86400
  14. )
  15. func expKey(mid int64) string {
  16. return fmt.Sprintf(_expPrefix, mid)
  17. }
  18. func keyInfo(mid int64) string {
  19. return model.CacheKeyInfo + strconv.FormatInt(mid, 10)
  20. }
  21. func (d *Dao) mcBaseKey(mid int64) (key string) {
  22. return fmt.Sprintf(model.CacheKeyBase, mid)
  23. }
  24. func (d *Dao) moralKey(mid int64) (key string) {
  25. return fmt.Sprintf(model.CacheKeyMoral, mid)
  26. }
  27. func realnameInfoKey(mid int64) string {
  28. return fmt.Sprintf("realname_info_%d", mid)
  29. }
  30. func realnameApplyStatusKey(mid int64) string {
  31. return fmt.Sprintf("realname_apply_%d", mid)
  32. }
  33. // DelMoralCache delete moral cache.
  34. func (d *Dao) DelMoralCache(c context.Context, mid int64) (err error) {
  35. key := d.moralKey(mid)
  36. conn := d.memberMemcache.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. // DelBaseInfoCache delete baseInfo cache.
  48. func (d *Dao) DelBaseInfoCache(c context.Context, mid int64) (err error) {
  49. key := d.mcBaseKey(mid)
  50. conn := d.memberMemcache.Get(c)
  51. defer conn.Close()
  52. if err = conn.Delete(key); err != nil {
  53. if err == memcache.ErrNotFound {
  54. err = nil
  55. return
  56. }
  57. log.Error("conn.Delete(%s) error(%v)", key, err)
  58. }
  59. return
  60. }
  61. // DelInfoCache delete account info from cache.
  62. func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
  63. conn := d.memberMemcache.Get(c)
  64. err = conn.Delete(keyInfo(mid))
  65. conn.Close()
  66. if err == memcache.ErrNotFound {
  67. err = nil
  68. }
  69. return
  70. }
  71. // SetExpCache set user exp cache.
  72. func (d *Dao) SetExpCache(c context.Context, mid, exp int64) (err error) {
  73. conn := d.memberMemcache.Get(c)
  74. defer conn.Close()
  75. if err = conn.Set(&memcache.Item{
  76. Key: expKey(mid),
  77. Value: []byte(strconv.FormatInt(exp, 10)),
  78. Expiration: _expExpire,
  79. }); err != nil {
  80. log.Error("setexpcache mid %d err %v ", mid, err)
  81. }
  82. return
  83. }
  84. // DelExpCache set user exp cache.
  85. func (d *Dao) DelExpCache(c context.Context, mid int64) error {
  86. conn := d.memberMemcache.Get(c)
  87. defer conn.Close()
  88. if err := conn.Delete(expKey(mid)); err != nil {
  89. if err == memcache.ErrNotFound {
  90. return nil
  91. }
  92. return errors.WithStack(err)
  93. }
  94. return nil
  95. }
  96. // DeleteRealnameCache delete all realname cache
  97. func (d *Dao) DeleteRealnameCache(c context.Context, mid int64) (err error) {
  98. var (
  99. key1 = realnameInfoKey(mid)
  100. key2 = realnameApplyStatusKey(mid)
  101. conn = d.memberMemcache.Get(c)
  102. )
  103. defer conn.Close()
  104. if err = conn.Delete(key1); err != nil {
  105. if err != memcache.ErrNotFound {
  106. err = errors.Wrapf(err, "conn.Delete(%s)", key1)
  107. return
  108. }
  109. err = nil
  110. }
  111. if err = conn.Delete(key2); err != nil {
  112. if err == memcache.ErrNotFound {
  113. err = nil
  114. } else {
  115. err = errors.Wrapf(err, "conn.Delete(%s)", key2)
  116. }
  117. return
  118. }
  119. return
  120. }