redis.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package bplus
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/app-interface/model"
  6. "go-common/app/interface/main/app-interface/model/space"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _prefixContributeAttr = "cba_"
  14. _prefixContribute = "cb_"
  15. )
  16. func keyContributeAttr(vmid int64) string {
  17. return _prefixContributeAttr + strconv.FormatInt(vmid, 10)
  18. }
  19. func keyContribute(vmid int64) string {
  20. return _prefixContribute + strconv.FormatInt(vmid, 10)
  21. }
  22. // AddContributeCache .
  23. func (d *Dao) AddContributeCache(c context.Context, vmid int64, attrs *space.Attrs, items []*space.Item) (err error) {
  24. var attr int32
  25. conn := d.redis.Get(c)
  26. key := keyContribute(vmid)
  27. defer conn.Close()
  28. for _, item := range items {
  29. score := item.CTime.Time().Unix()
  30. item.FormatKey()
  31. if err = conn.Send("ZADD", key, score, item.Member); err != nil {
  32. err = errors.Wrapf(err, "conn.Send(ZADD,%s,%d,%d)", key, score, item.Member)
  33. return
  34. }
  35. }
  36. keyAttr := keyContributeAttr(vmid)
  37. if attrs != nil {
  38. if attrs.Archive {
  39. attr = model.AttrSet(attr, model.AttrYes, model.AttrBitArchive)
  40. }
  41. if attrs.Article {
  42. attr = model.AttrSet(attr, model.AttrYes, model.AttrBitArticle)
  43. }
  44. if attrs.Clip {
  45. attr = model.AttrSet(attr, model.AttrYes, model.AttrBitClip)
  46. }
  47. if attrs.Album {
  48. attr = model.AttrSet(attr, model.AttrYes, model.AttrBitAlbum)
  49. }
  50. if attrs.Audio {
  51. attr = model.AttrSet(attr, model.AttrYes, model.AttrBitAudio)
  52. }
  53. }
  54. if err = conn.Send("SET", keyAttr, attr); err != nil {
  55. err = errors.Wrapf(err, "conn.Send(SET,%s,%d)", keyAttr, attr)
  56. return
  57. }
  58. if err = conn.Flush(); err != nil {
  59. return
  60. }
  61. for i := 0; i < len(items)+1; i++ {
  62. if _, err = conn.Receive(); err != nil {
  63. return
  64. }
  65. }
  66. return
  67. }
  68. // RangeContributeCache .
  69. func (d *Dao) RangeContributeCache(c context.Context, vmid int64, pn, ps int) (items []*space.Item, err error) {
  70. conn := d.redis.Get(c)
  71. key := keyContribute(vmid)
  72. start := (pn - 1) * ps
  73. stop := pn*ps - 1
  74. defer conn.Close()
  75. var vs []interface{}
  76. if vs, err = redis.Values(conn.Do("ZREVRANGE", key, start, stop, "WITHSCORES")); err != nil {
  77. err = errors.Wrapf(err, "conn.Do(ZREVRANGE,%s,%d,%d)", key, start, stop)
  78. return
  79. } else if len(vs) == 0 {
  80. return
  81. }
  82. items = make([]*space.Item, 0, ps)
  83. for len(vs) > 0 {
  84. var (
  85. member int64
  86. score int64
  87. )
  88. if vs, err = redis.Scan(vs, &member, &score); err != nil {
  89. log.Error("redis.Scan(%v) error(%v)", vs, err)
  90. err = nil
  91. continue
  92. }
  93. if member != 0 && score != 0 {
  94. item := &space.Item{Member: member, CTime: xtime.Time(score)}
  95. item.ParseKey()
  96. if item.Goto != "" {
  97. items = append(items, item)
  98. }
  99. }
  100. }
  101. return
  102. }
  103. func (d *Dao) RangeContributionCache(c context.Context, vmid int64, cursor *model.Cursor) (items []*space.Item, err error) {
  104. conn := d.redis.Get(c)
  105. key := keyContribute(vmid)
  106. defer conn.Close()
  107. var (
  108. vs []interface{}
  109. rank int64
  110. start, stop int64
  111. )
  112. if cursor.MoveUpward() || cursor.MoveDownward() {
  113. if rank, err = redis.Int64(conn.Do("ZREVRANK", key, cursor.Current)); err != nil {
  114. if err == redis.ErrNil {
  115. err = nil
  116. return
  117. }
  118. err = errors.Wrapf(err, "conn.Do(ZREVRANK,%s,%d)", key, cursor.Current)
  119. return
  120. }
  121. }
  122. if cursor.Latest() {
  123. start = 0
  124. stop = rank + int64(cursor.Size) - 1
  125. } else if cursor.MoveUpward() {
  126. if rank == 0 {
  127. return
  128. }
  129. if start = rank - int64(cursor.Size); start < 0 {
  130. start = 0
  131. }
  132. stop = rank - 1
  133. } else if cursor.MoveDownward() {
  134. start = rank + 1
  135. stop = rank + int64(cursor.Size)
  136. }
  137. if vs, err = redis.Values(conn.Do("ZREVRANGE", key, start, stop, "WITHSCORES")); err != nil {
  138. err = errors.Wrapf(err, "conn.Do(ZREVRANGE,%s,%d,%d)", key, start, stop)
  139. return
  140. }
  141. if len(vs) == 0 {
  142. return
  143. }
  144. items = make([]*space.Item, 0, len(vs))
  145. for len(vs) > 0 {
  146. var (
  147. member int64
  148. score int64
  149. )
  150. if vs, err = redis.Scan(vs, &member, &score); err != nil {
  151. log.Error("redis.Scan(%v) error(%v)", vs, err)
  152. err = nil
  153. continue
  154. }
  155. if member != 0 && score != 0 {
  156. item := &space.Item{Member: member, CTime: xtime.Time(score)}
  157. item.ParseKey()
  158. if item.Goto != "" {
  159. items = append(items, item)
  160. }
  161. }
  162. }
  163. return
  164. }
  165. // AttrCache .
  166. func (d *Dao) AttrCache(c context.Context, vmid int64) (attrs *space.Attrs, err error) {
  167. var attr int64
  168. conn := d.redis.Get(c)
  169. key := keyContributeAttr(vmid)
  170. if attr, err = redis.Int64(conn.Do("GET", key)); err != nil {
  171. if err == redis.ErrNil {
  172. err = nil
  173. return
  174. }
  175. err = errors.Wrapf(err, "conn.Do(GET,%s)", key)
  176. return
  177. }
  178. conn.Close()
  179. attrs = &space.Attrs{}
  180. if model.AttrVal(int32(attr), model.AttrBitArchive) == model.AttrYes {
  181. attrs.Archive = true
  182. }
  183. if model.AttrVal(int32(attr), model.AttrBitArticle) == model.AttrYes {
  184. attrs.Article = true
  185. }
  186. if model.AttrVal(int32(attr), model.AttrBitClip) == model.AttrYes {
  187. attrs.Clip = true
  188. }
  189. if model.AttrVal(int32(attr), model.AttrBitAlbum) == model.AttrYes {
  190. attrs.Album = true
  191. }
  192. if model.AttrVal(int32(attr), model.AttrBitAudio) == model.AttrYes {
  193. attrs.Audio = true
  194. }
  195. return
  196. }