mc.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package block
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/service/main/member/model/block"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. func userKey(mid int64) (key string) {
  11. key = fmt.Sprintf("u_%d", mid)
  12. return
  13. }
  14. func userDetailKey(mid int64) (key string) {
  15. key = fmt.Sprintf("ud_%d", mid)
  16. return
  17. }
  18. // UsersCache get block info by mids
  19. func (d *Dao) UsersCache(c context.Context, mids []int64) (res map[int64]*model.MCBlockInfo, err error) {
  20. res = make(map[int64]*model.MCBlockInfo, len(mids))
  21. if len(mids) == 0 {
  22. return
  23. }
  24. var (
  25. keys = make([]string, 0, len(mids))
  26. keyMap = make(map[string]int64, len(mids))
  27. key string
  28. conn = d.mc.Get(c)
  29. rs map[string]*memcache.Item
  30. )
  31. defer conn.Close()
  32. for _, mid := range mids {
  33. key = userKey(mid)
  34. if _, ok := keyMap[key]; !ok {
  35. keyMap[key] = mid
  36. keys = append(keys, key)
  37. }
  38. }
  39. if rs, err = conn.GetMulti(keys); err != nil {
  40. if err == memcache.ErrNotFound {
  41. err = nil
  42. return
  43. }
  44. err = errors.Wrapf(err, "keys : %+v", keys)
  45. return
  46. }
  47. for k, r := range rs {
  48. info := &model.MCBlockInfo{}
  49. if err = conn.Scan(r, info); err != nil {
  50. err = errors.Wrapf(err, "key : %s", k)
  51. log.Error("%+v", err)
  52. err = nil
  53. continue
  54. }
  55. res[keyMap[k]] = info
  56. }
  57. return
  58. }
  59. // SetUserCache set user block info to cache
  60. func (d *Dao) SetUserCache(c context.Context, mid int64, status model.BlockStatus, startTime, endTime int64) (err error) {
  61. var (
  62. key = userKey(mid)
  63. conn = d.mc.Get(c)
  64. info = &model.MCBlockInfo{
  65. BlockStatus: status,
  66. StartTime: startTime,
  67. EndTime: endTime,
  68. }
  69. )
  70. log.Info("Set User Cache key (%s) obj (%+v)", key, info)
  71. defer conn.Close()
  72. if err = conn.Set(&memcache.Item{
  73. Key: key,
  74. Object: info,
  75. Expiration: d.mcUserExpire(key),
  76. Flags: memcache.FlagJSON,
  77. }); err != nil {
  78. err = (err)
  79. return
  80. }
  81. return
  82. }
  83. // DeleteUserCache delete user cache
  84. func (d *Dao) DeleteUserCache(c context.Context, mid int64) (err error) {
  85. var (
  86. key = userKey(mid)
  87. conn = d.mc.Get(c)
  88. )
  89. defer conn.Close()
  90. if err = conn.Delete(key); err != nil {
  91. err = (err)
  92. return
  93. }
  94. return
  95. }
  96. // SetUserDetailCache set user detail to cache
  97. func (d *Dao) SetUserDetailCache(c context.Context, mid int64, detail *model.MCUserDetail) (err error) {
  98. var (
  99. key = userDetailKey(mid)
  100. conn = d.mc.Get(c)
  101. )
  102. log.Info("Set User Detail Cache key (%s) obj (%+v)", key, detail)
  103. defer conn.Close()
  104. if err = conn.Set(&memcache.Item{
  105. Key: key,
  106. Object: detail,
  107. Expiration: d.mcUserExpire(key),
  108. Flags: memcache.FlagJSON,
  109. }); err != nil {
  110. return
  111. }
  112. return
  113. }
  114. // DeleteUserDetailCache delete user detail cache
  115. func (d *Dao) DeleteUserDetailCache(c context.Context, mid int64) (err error) {
  116. var (
  117. key = userDetailKey(mid)
  118. conn = d.mc.Get(c)
  119. )
  120. defer conn.Close()
  121. err = conn.Delete(key)
  122. return
  123. }
  124. // UserDetailsCache .
  125. func (d *Dao) UserDetailsCache(c context.Context, mids []int64) (res map[int64]*model.MCUserDetail, err error) {
  126. res = make(map[int64]*model.MCUserDetail)
  127. if len(mids) == 0 {
  128. return
  129. }
  130. var (
  131. keys = make([]string, 0, len(mids))
  132. keyMap = make(map[string]int64, len(mids))
  133. key string
  134. conn = d.mc.Get(c)
  135. rs map[string]*memcache.Item
  136. )
  137. defer conn.Close()
  138. for _, mid := range mids {
  139. key = userDetailKey(mid)
  140. if _, ok := keyMap[key]; !ok {
  141. keyMap[key] = mid
  142. keys = append(keys, key)
  143. }
  144. }
  145. if rs, err = conn.GetMulti(keys); err != nil {
  146. err = errors.Wrapf(err, "keys : %+v", keys)
  147. return
  148. }
  149. for k, r := range rs {
  150. detail := &model.MCUserDetail{}
  151. if err = conn.Scan(r, detail); err != nil {
  152. err = errors.Wrapf(err, "key : %+v", k)
  153. log.Error("%+v", err)
  154. err = nil
  155. continue
  156. }
  157. res[keyMap[k]] = detail
  158. }
  159. return
  160. }