redis_seg.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package dao
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "go-common/app/job/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. _keyIdxContent = "c_%d_%d" // dm content hash(d_type_oid, dmid, xml)
  14. )
  15. func keyIdx(tp int32, oid, cnt, n int64) string {
  16. return fmt.Sprintf(_keyIdx, tp, oid, cnt, n)
  17. }
  18. // keyIdxSub return dm idx key.
  19. func keyIdxSub(tp int32, oid int64) string {
  20. return fmt.Sprintf(_keyIdxSub, tp, oid)
  21. }
  22. // keyIdxContent return key of different dm.
  23. func keyIdxContent(tp int32, oid int64) string {
  24. return fmt.Sprintf(_keyIdxContent, tp, oid)
  25. }
  26. // ExpireDMID set expire time of index.
  27. func (d *Dao) ExpireDMID(c context.Context, tp int32, oid, cnt, n int64) (ok bool, err error) {
  28. key := keyIdx(tp, oid, cnt, n)
  29. conn := d.dmSegRds.Get(c)
  30. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.dmSegExpire)); err != nil {
  31. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  32. }
  33. conn.Close()
  34. return
  35. }
  36. // DMIDCache return dm ids.
  37. func (d *Dao) DMIDCache(c context.Context, tp int32, oid int64, cnt, n, limit int64) (dmids []int64, err error) {
  38. var (
  39. conn = d.dmSegRds.Get(c)
  40. key = keyIdx(tp, oid, cnt, n)
  41. )
  42. defer conn.Close()
  43. if dmids, err = redis.Int64s(conn.Do("ZRANGE", key, 0, -1)); err != nil {
  44. log.Error("DMIDSPCache.conn.DO(ZRANGEBYSCORE %s) error(%v)", key, err)
  45. }
  46. return
  47. }
  48. // AddDMIDCache add dmid(normal and special) to segment redis.
  49. func (d *Dao) AddDMIDCache(c context.Context, tp int32, oid, cnt, n int64, dmids ...int64) (err error) {
  50. key := keyIdx(tp, oid, cnt, n)
  51. conn := d.dmSegRds.Get(c)
  52. defer conn.Close()
  53. for _, dmid := range dmids {
  54. if err = conn.Send("ZADD", key, dmid, dmid); err != nil {
  55. log.Error("conn.Send(ZADD %s) error(%v)", key, err)
  56. return
  57. }
  58. }
  59. if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
  60. log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
  61. return
  62. }
  63. if err = conn.Flush(); err != nil {
  64. log.Error("conn.Flush() error(%v)", err)
  65. return
  66. }
  67. for i := 0; i < len(dmids)+1; i++ {
  68. if _, err = conn.Receive(); err != nil {
  69. log.Error("conn.Receive() error(%v)", err)
  70. return
  71. }
  72. }
  73. return
  74. }
  75. // DelDMIDCache delete dm segment cache.
  76. func (d *Dao) DelDMIDCache(c context.Context, tp int32, oid, cnt, n int64) (err error) {
  77. key := keyIdx(tp, oid, cnt, n)
  78. conn := d.dmSegRds.Get(c)
  79. if _, err = conn.Do("DEL", key); err != nil {
  80. log.Error("conn.Do(DEL %s) err(%v)", key, err)
  81. }
  82. conn.Close()
  83. return
  84. }
  85. // ExpireDMIDSubtitle set expire time of subtitle dmid.
  86. func (d *Dao) ExpireDMIDSubtitle(c context.Context, tp int32, oid int64) (ok bool, err error) {
  87. key := keyIdxSub(tp, oid)
  88. conn := d.dmSegRds.Get(c)
  89. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.dmSegExpire)); err != nil {
  90. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  91. }
  92. conn.Close()
  93. return
  94. }
  95. // DMIDSubtitleCache get subtitle dmid.
  96. func (d *Dao) DMIDSubtitleCache(c context.Context, tp int32, oid int64, ps, pe, limit int64) (dmids []int64, err error) {
  97. var (
  98. conn = d.dmSegRds.Get(c)
  99. key = keyIdxSub(tp, oid)
  100. )
  101. defer conn.Close()
  102. if dmids, err = redis.Int64s(conn.Do("ZRANGEBYSCORE", key, ps, pe, "LIMIT", 0, limit)); err != nil {
  103. log.Error("conn.DO(ZRANGEBYSCORE %s) error(%v)", key, err)
  104. }
  105. return
  106. }
  107. // AddDMIDSubtitleCache add subtitle dmid to redis.
  108. func (d *Dao) AddDMIDSubtitleCache(c context.Context, tp int32, oid int64, dms ...*model.DM) (err error) {
  109. key := keyIdxSub(tp, oid)
  110. conn := d.dmSegRds.Get(c)
  111. defer conn.Close()
  112. for _, dm := range dms {
  113. if err = conn.Send("ZADD", key, dm.Progress, dm.ID); err != nil {
  114. log.Error("conn.Send(ZADD %s) error(%v)", key, err)
  115. return
  116. }
  117. }
  118. if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
  119. log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
  120. return
  121. }
  122. if err = conn.Flush(); err != nil {
  123. log.Error("conn.Flush() error(%v)", err)
  124. return
  125. }
  126. for i := 0; i < len(dms)+1; i++ {
  127. if _, err = conn.Receive(); err != nil {
  128. log.Error("conn.Receive() error(%v)", err)
  129. return
  130. }
  131. }
  132. return
  133. }
  134. // DelDMIDSubtitleCache delete subtitle dmid cache.
  135. func (d *Dao) DelDMIDSubtitleCache(c context.Context, tp int32, oid int64) (err error) {
  136. key := keyIdxSub(tp, oid)
  137. conn := d.dmSegRds.Get(c)
  138. if _, err = conn.Do("DEL", key); err != nil {
  139. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  140. }
  141. conn.Close()
  142. return
  143. }
  144. // AddIdxContentCaches add index content cache to redis.
  145. func (d *Dao) AddIdxContentCaches(c context.Context, tp int32, oid int64, dms ...*model.DM) (err error) {
  146. var (
  147. conn = d.dmSegRds.Get(c)
  148. key = keyIdxContent(tp, oid)
  149. )
  150. defer conn.Close()
  151. for _, dm := range dms {
  152. if err = conn.Send("HSET", key, dm.ID, dm.ToXMLSeg()); err != nil {
  153. log.Error("conn.Send(HSET %s,%v) error(%v)", key, dm, err)
  154. return
  155. }
  156. }
  157. if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
  158. log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
  159. return
  160. }
  161. if err = conn.Flush(); err != nil {
  162. log.Error("conn.Flush() error(%v)", err)
  163. return
  164. }
  165. for i := 0; i <= len(dms); i++ {
  166. if _, err = conn.Receive(); err != nil {
  167. log.Error("conn.Receive() error(%v)", err)
  168. return
  169. }
  170. }
  171. return
  172. }
  173. // DelIdxContentCaches del index content cache.
  174. func (d *Dao) DelIdxContentCaches(c context.Context, tp int32, oid int64, dmids ...int64) (err error) {
  175. key := keyIdxContent(tp, oid)
  176. conn := d.dmSegRds.Get(c)
  177. args := []interface{}{key}
  178. for _, dmid := range dmids {
  179. args = append(args, dmid)
  180. }
  181. if _, err = conn.Do("HDEL", args...); err != nil {
  182. log.Error("conn.Do(HDEL %s) error(%v)", key, err)
  183. }
  184. conn.Close()
  185. return
  186. }
  187. // IdxContentCache get xml info by dmid.
  188. func (d *Dao) IdxContentCache(c context.Context, tp int32, oid int64, dmids []int64) (res []byte, missed []int64, err error) {
  189. var (
  190. k int
  191. dmid int64
  192. values [][]byte
  193. key = keyIdxContent(tp, oid)
  194. args = []interface{}{key}
  195. )
  196. for _, dmid = range dmids {
  197. args = append(args, dmid)
  198. }
  199. conn := d.dmSegRds.Get(c)
  200. defer conn.Close()
  201. if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
  202. log.Error("conn.Do(HMGET %v) error(%v)", args, err)
  203. if err == redis.ErrNil {
  204. return nil, nil, nil
  205. }
  206. return
  207. }
  208. for k, dmid = range dmids {
  209. if len(values[k]) == 0 {
  210. missed = append(missed, dmid)
  211. continue
  212. }
  213. res = append(res, values[k]...)
  214. }
  215. return
  216. }
  217. // IdxContentCacheV2 get elems info by dmid.
  218. func (d *Dao) IdxContentCacheV2(c context.Context, tp int32, oid int64, dmids []int64) (elems []*model.Elem, missed []int64, err error) {
  219. var (
  220. values [][]byte
  221. key = keyIdxContent(tp, oid)
  222. args = []interface{}{key}
  223. )
  224. for _, dmid := range dmids {
  225. args = append(args, dmid)
  226. }
  227. conn := d.dmSegRds.Get(c)
  228. defer conn.Close()
  229. if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
  230. log.Error("conn.Do(HMGET %v) error(%v)", args, err)
  231. if err == redis.ErrNil {
  232. return nil, nil, nil
  233. }
  234. return
  235. }
  236. for k, dmid := range dmids {
  237. if len(values[k]) == 0 {
  238. missed = append(missed, dmid)
  239. continue
  240. }
  241. elem, err := d.xmlToElem(values[k])
  242. if err != nil {
  243. missed = append(missed, dmid)
  244. continue
  245. }
  246. elems = append(elems, elem)
  247. }
  248. return
  249. }
  250. // 在缓存过渡期将<d p="弹幕ID,弹幕属性,播放时间,弹幕模式,字体大小,颜色,发送时间,弹幕池,用户hash id">弹幕内容</d>
  251. // 装换为 model.Elem结构
  252. func (d *Dao) xmlToElem(data []byte) (e *model.Elem, err error) {
  253. var v struct {
  254. XMLName xml.Name `xml:"d"`
  255. Attribute string `xml:"p,attr"`
  256. Content string `xml:",chardata"`
  257. }
  258. if err = xml.Unmarshal(data, &v); err != nil {
  259. return
  260. }
  261. e = &model.Elem{Content: v.Content, Attribute: v.Attribute}
  262. return
  263. }