redis_seg.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package dao
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "go-common/app/interface/main/dm2/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _keyIdx = "i_%d_%d_%d_%d" // normal dm segment sortedset(s_type_oid_cnt_n, ctime, dmid)
  12. _keyIdxSub = "s_%d_%d" // subtitle dm sortedset(s_type_oid, progress, dmid)
  13. _keyIdxSpe = "spe_%d_%d" // special dm sortedset(spe_type_oid, progress,dmid)
  14. _keyIdxContent = "c_%d_%d" // dm content hash(d_type_oid, dmid, xml)
  15. )
  16. func keyIdx(tp int32, oid, cnt, n int64) string {
  17. return fmt.Sprintf(_keyIdx, tp, oid, cnt, n)
  18. }
  19. // keyIdxSub return dm idx key.
  20. func keyIdxSub(tp int32, oid int64) string {
  21. return fmt.Sprintf(_keyIdxSub, tp, oid)
  22. }
  23. // keyIdxContent return key of different dm.
  24. func keyIdxContent(tp int32, oid int64) string {
  25. return fmt.Sprintf(_keyIdxContent, tp, oid)
  26. }
  27. func keyIdxSpecial(tp int32, oid int64) string {
  28. return fmt.Sprintf(_keyIdxSpe, tp, oid)
  29. }
  30. // DMIDSubtitlesCache return subtitle dm ids.
  31. func (d *Dao) DMIDSubtitlesCache(c context.Context, tp int32, oid int64, ps, pe, limit int64) (dmids []int64, err error) {
  32. var (
  33. conn = d.dmSegRds.Get(c)
  34. keySub = keyIdxSub(tp, oid)
  35. )
  36. defer conn.Close()
  37. if dmids, err = redis.Int64s(conn.Do("ZRANGEBYSCORE", keySub, ps, pe, "LIMIT", 0, limit)); err != nil {
  38. log.Error("conn.DO(ZRANGEBYSCORE %s) error(%v)", keySub, err)
  39. }
  40. if len(dmids) > 0 {
  41. PromCacheHit("dm_seg_dmid", 1)
  42. } else {
  43. PromCacheMiss("dm_seg_dmid", 1)
  44. }
  45. return
  46. }
  47. // DMIDCache return dm index id.
  48. func (d *Dao) DMIDCache(c context.Context, tp int32, oid int64, cnt, n, limit int64) (dmids []int64, err error) {
  49. var (
  50. conn = d.dmSegRds.Get(c)
  51. keyIdx = keyIdx(tp, oid, cnt, n)
  52. )
  53. defer conn.Close()
  54. if dmids, err = redis.Int64s(conn.Do("ZRANGE", keyIdx, 0, -1)); err != nil {
  55. log.Error("DMIDSPCache.conn.DO(ZRANGEBYSCORE %s) error(%v)", keyIdx, err)
  56. }
  57. if len(dmids) > 0 {
  58. PromCacheHit("dm_seg_dmid", 1)
  59. } else {
  60. PromCacheMiss("dm_seg_dmid", 1)
  61. }
  62. return
  63. }
  64. // IdxContentCache get xml info by dmid.
  65. func (d *Dao) IdxContentCache(c context.Context, tp int32, oid int64, dmids []int64) (res []byte, missed []int64, err error) {
  66. var (
  67. k int
  68. dmid int64
  69. values [][]byte
  70. key = keyIdxContent(tp, oid)
  71. args = []interface{}{key}
  72. )
  73. for _, dmid = range dmids {
  74. args = append(args, dmid)
  75. }
  76. conn := d.dmSegRds.Get(c)
  77. defer conn.Close()
  78. if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
  79. log.Error("conn.Do(HMGET %v) error(%v)", args, err)
  80. if err == redis.ErrNil {
  81. return nil, nil, nil
  82. }
  83. return
  84. }
  85. for k, dmid = range dmids {
  86. if len(values[k]) == 0 {
  87. missed = append(missed, dmid)
  88. continue
  89. }
  90. res = append(res, values[k]...)
  91. }
  92. PromCacheMiss("dmid_content", int64(len(missed)))
  93. PromCacheHit("dmid_content", int64(len(dmids)-len(missed)))
  94. return
  95. }
  96. // IdxContentCacheV2 get elems info by dmid.
  97. func (d *Dao) IdxContentCacheV2(c context.Context, tp int32, oid int64, dmids []int64) (elems []*model.Elem, missed []int64, err error) {
  98. var (
  99. k int
  100. dmid int64
  101. values [][]byte
  102. key = keyIdxContent(tp, oid)
  103. args = []interface{}{key}
  104. )
  105. for _, dmid = range dmids {
  106. args = append(args, dmid)
  107. }
  108. conn := d.dmSegRds.Get(c)
  109. defer conn.Close()
  110. if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
  111. if err == redis.ErrNil {
  112. err = nil
  113. missed = dmids
  114. } else {
  115. log.Error("conn.Do(HMGET %v) error(%v)", args, err)
  116. }
  117. return
  118. }
  119. for k, dmid = range dmids {
  120. if len(values[k]) == 0 {
  121. missed = append(missed, dmid)
  122. continue
  123. }
  124. elem, err := d.xmlToElem(values[k])
  125. if err != nil {
  126. missed = append(missed, dmid)
  127. continue
  128. }
  129. elems = append(elems, elem)
  130. }
  131. PromCacheMiss("dmid_elem", int64(len(missed)))
  132. PromCacheHit("dmid_elem", int64(len(dmids)-len(missed)))
  133. return
  134. }
  135. // 在缓存过渡期将<d p="弹幕ID,弹幕属性,播放时间,弹幕模式,字体大小,颜色,发送时间,弹幕池,用户hash id">弹幕内容</d>
  136. // 装换为 model.Elem结构
  137. func (d *Dao) xmlToElem(data []byte) (e *model.Elem, err error) {
  138. var v struct {
  139. XMLName xml.Name `xml:"d"`
  140. Attribute string `xml:"p,attr"`
  141. Content string `xml:",chardata"`
  142. }
  143. if err = xml.Unmarshal(data, &v); err != nil {
  144. return
  145. }
  146. e = &model.Elem{Content: v.Content, Attribute: v.Attribute}
  147. return
  148. }
  149. // AddIdxContentCache add index content cache to redis.
  150. func (d *Dao) AddIdxContentCache(c context.Context, tp int32, oid int64, dms []*model.DM, realname bool) (err error) {
  151. var (
  152. key string
  153. conn = d.dmSegRds.Get(c)
  154. )
  155. defer conn.Close()
  156. for _, dm := range dms {
  157. key = keyIdxContent(tp, oid)
  158. if err = conn.Send("HSET", key, dm.ID, dm.ToXMLSeg(realname)); err != nil {
  159. log.Error("conn.Send(HSET %s,%v) error(%v)", key, dm, err)
  160. return
  161. }
  162. }
  163. if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
  164. log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
  165. return
  166. }
  167. if err = conn.Flush(); err != nil {
  168. log.Error("conn.Flush() error(%v)", err)
  169. return
  170. }
  171. for i := 0; i <= len(dms); i++ {
  172. if _, err = conn.Receive(); err != nil {
  173. log.Error("conn.Receive() error(%v)", err)
  174. return
  175. }
  176. }
  177. return
  178. }
  179. // DMIDSpecialsCache return special dmids
  180. func (d *Dao) DMIDSpecialsCache(c context.Context, tp int32, oid int64) (dmids []int64, err error) {
  181. var (
  182. conn = d.dmSegRds.Get(c)
  183. keySpe = keyIdxSpecial(tp, oid)
  184. )
  185. defer conn.Close()
  186. if dmids, err = redis.Int64s(conn.Do("ZRANGE", keySpe, 0, -1)); err != nil {
  187. log.Error("conn.DO(ZRANGE %s) error(%v)", keySpe, err)
  188. }
  189. if len(dmids) > 0 {
  190. PromCacheHit("dm_spe_dmid", 1)
  191. } else {
  192. PromCacheMiss("dm_spe_dmid", 1)
  193. }
  194. return
  195. }