blocked_mc.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/interface/main/credit/model"
  7. gmc "go-common/library/cache/memcache"
  8. )
  9. const (
  10. _prefixBlockedUserList = "bul_%d"
  11. _prefixBlockInfo = "blo_%d"
  12. )
  13. func userBlockedListKey(mid int64) string {
  14. return fmt.Sprintf(_prefixBlockedUserList, mid)
  15. }
  16. func blockedInfoKey(id int64) string {
  17. return fmt.Sprintf(_prefixBlockInfo, id)
  18. }
  19. // BlockedUserListCache get user blocked list.
  20. func (d *Dao) BlockedUserListCache(c context.Context, mid int64) (ls []*model.BlockedInfo, err error) {
  21. var (
  22. reply *gmc.Item
  23. conn = d.mc.Get(c)
  24. )
  25. defer conn.Close()
  26. reply, err = conn.Get(userBlockedListKey(mid))
  27. if err != nil {
  28. if err == gmc.ErrNotFound {
  29. err = nil
  30. }
  31. return
  32. }
  33. ls = make([]*model.BlockedInfo, 0)
  34. err = conn.Scan(reply, &ls)
  35. return
  36. }
  37. // SetBlockedUserListCache set user blocked list cache.
  38. func (d *Dao) SetBlockedUserListCache(c context.Context, mid int64, ls []*model.BlockedInfo) (err error) {
  39. var (
  40. item = &gmc.Item{Key: userBlockedListKey(mid), Object: ls, Expiration: d.userExpire, Flags: gmc.FlagJSON}
  41. conn = d.mc.Get(c)
  42. )
  43. defer conn.Close()
  44. err = conn.Set(item)
  45. return
  46. }
  47. // BlockedInfoCache get blocked info by blocked id.
  48. func (d *Dao) BlockedInfoCache(c context.Context, id int64) (info *model.BlockedInfo, err error) {
  49. var (
  50. reply *gmc.Item
  51. conn = d.mc.Get(c)
  52. key = blockedInfoKey(id)
  53. )
  54. defer conn.Close()
  55. reply, err = conn.Get(key)
  56. if err != nil {
  57. if err == gmc.ErrNotFound {
  58. err = nil
  59. }
  60. return
  61. }
  62. info = &model.BlockedInfo{}
  63. err = conn.Scan(reply, &info)
  64. return
  65. }
  66. // SetBlockedInfoCache set user blocked list cache.
  67. func (d *Dao) SetBlockedInfoCache(c context.Context, id int64, info *model.BlockedInfo) (err error) {
  68. var (
  69. item = &gmc.Item{Key: blockedInfoKey(id), Object: info, Expiration: d.minCommonExpire, Flags: gmc.FlagJSON}
  70. conn = d.mc.Get(c)
  71. )
  72. defer conn.Close()
  73. err = conn.Set(item)
  74. return
  75. }
  76. // BlockedInfosCache get blocked infos by ids.
  77. func (d *Dao) BlockedInfosCache(c context.Context, ids []int64) (infos []*model.BlockedInfo, miss []int64, err error) {
  78. var (
  79. rs map[string]*gmc.Item
  80. conn = d.mc.Get(c)
  81. )
  82. defer conn.Close()
  83. keys := make([]string, len(ids))
  84. for _, id := range ids {
  85. keys = append(keys, blockedInfoKey(id))
  86. }
  87. rs, err = conn.GetMulti(keys)
  88. if err != nil {
  89. if err == gmc.ErrNotFound {
  90. err = nil
  91. }
  92. return
  93. }
  94. for _, id := range ids {
  95. if r, ok := rs[strconv.FormatInt(id, 10)]; ok {
  96. info := &model.BlockedInfo{}
  97. conn.Scan(r, &info)
  98. infos = append(infos, info)
  99. } else {
  100. miss = append(miss, id)
  101. }
  102. }
  103. return
  104. }
  105. // SetBlockedInfosCache set user blocked list cache.
  106. func (d *Dao) SetBlockedInfosCache(c context.Context, infos []*model.BlockedInfo) (err error) {
  107. conn := d.mc.Get(c)
  108. defer conn.Close()
  109. for _, info := range infos {
  110. item := &gmc.Item{Key: blockedInfoKey(info.ID), Object: info, Expiration: d.minCommonExpire, Flags: gmc.FlagJSON}
  111. if err = conn.Set(item); err != nil {
  112. return
  113. }
  114. }
  115. return
  116. }