memcache.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/member/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "strconv"
  9. errors "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 (d *Dao) mcBaseKey(mid int64) (key string) {
  19. return fmt.Sprintf(model.CacheKeyBase, mid)
  20. }
  21. func (d *Dao) moralKey(mid int64) (key string) {
  22. return fmt.Sprintf(model.CacheKeyMoral, mid)
  23. }
  24. // SetStartCache set data import start
  25. func (d *Dao) SetStartCache(c context.Context, mid int64, key string) (err error) {
  26. conn := d.mc.Get(c)
  27. defer conn.Close()
  28. if conn.Set(&memcache.Item{
  29. Key: key,
  30. Object: mid,
  31. Flags: memcache.FlagJSON,
  32. Expiration: 0,
  33. }); err != nil {
  34. log.Error("conn.Set(%s, %d) error(%v)", key, mid, err)
  35. }
  36. return
  37. }
  38. // DelMoralCache delete moral cache.
  39. func (d *Dao) DelMoralCache(c context.Context, mid int64) (err error) {
  40. key := d.moralKey(mid)
  41. conn := d.mc.Get(c)
  42. defer conn.Close()
  43. if err = conn.Delete(key); err != nil {
  44. if err == memcache.ErrNotFound {
  45. err = nil
  46. return
  47. }
  48. log.Error("conn.Delete(%s) error(%v)", key, err)
  49. }
  50. return
  51. }
  52. // DelBaseInfoCache delete baseInfo cache.
  53. func (d *Dao) DelBaseInfoCache(c context.Context, mid int64) (err error) {
  54. key := d.mcBaseKey(mid)
  55. conn := d.mc.Get(c)
  56. defer conn.Close()
  57. if err = conn.Delete(key); err != nil {
  58. if err == memcache.ErrNotFound {
  59. err = nil
  60. return
  61. }
  62. log.Error("conn.Delete(%s) error(%v)", key, err)
  63. }
  64. return
  65. }
  66. // SetExpCache set user exp cache.
  67. func (d *Dao) SetExpCache(c context.Context, mid, exp int64) (err error) {
  68. conn := d.mc.Get(c)
  69. defer conn.Close()
  70. if err = conn.Set(&memcache.Item{
  71. Key: expKey(mid),
  72. Value: []byte(strconv.FormatInt(exp, 10)),
  73. Expiration: _expExpire,
  74. }); err != nil {
  75. log.Error("setexpcache mid %d err %v ", mid, err)
  76. }
  77. return
  78. }
  79. func realnameInfoKey(mid int64) string {
  80. return fmt.Sprintf("realname_info_%d", mid)
  81. }
  82. func realnameApplyStatusKey(mid int64) string {
  83. return fmt.Sprintf("realname_apply_%d", mid)
  84. }
  85. // DeleteRealnameCache delete all realname cache
  86. func (d *Dao) DeleteRealnameCache(c context.Context, mid int64) (err error) {
  87. var (
  88. key1 = realnameInfoKey(mid)
  89. key2 = realnameApplyStatusKey(mid)
  90. conn = d.mc.Get(c)
  91. )
  92. defer conn.Close()
  93. if err = conn.Delete(key1); err != nil {
  94. if err != memcache.ErrNotFound {
  95. err = errors.Wrapf(err, "conn.Delete(%s)", key1)
  96. return
  97. }
  98. err = nil
  99. }
  100. if err = conn.Delete(key2); err != nil {
  101. if err == memcache.ErrNotFound {
  102. err = nil
  103. } else {
  104. err = errors.Wrapf(err, "conn.Delete(%s)", key2)
  105. }
  106. return
  107. }
  108. return
  109. }