123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- package dao
- import (
- "context"
- "encoding/xml"
- "fmt"
- "go-common/app/job/main/dm2/model"
- "go-common/library/cache/redis"
- "go-common/library/log"
- )
- const (
- _keyIdx = "i_%d_%d_%d_%d" // normal dm segment sortedset(s_type_oid_cnt_n, ctime, dmid)
- _keyIdxSub = "s_%d_%d" // subtitle dm sortedset(s_type_oid, progress, dmid)
- _keyIdxContent = "c_%d_%d" // dm content hash(d_type_oid, dmid, xml)
- )
- func keyIdx(tp int32, oid, cnt, n int64) string {
- return fmt.Sprintf(_keyIdx, tp, oid, cnt, n)
- }
- // keyIdxSub return dm idx key.
- func keyIdxSub(tp int32, oid int64) string {
- return fmt.Sprintf(_keyIdxSub, tp, oid)
- }
- // keyIdxContent return key of different dm.
- func keyIdxContent(tp int32, oid int64) string {
- return fmt.Sprintf(_keyIdxContent, tp, oid)
- }
- // ExpireDMID set expire time of index.
- func (d *Dao) ExpireDMID(c context.Context, tp int32, oid, cnt, n int64) (ok bool, err error) {
- key := keyIdx(tp, oid, cnt, n)
- conn := d.dmSegRds.Get(c)
- if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.dmSegExpire)); err != nil {
- log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
- }
- conn.Close()
- return
- }
- // DMIDCache return dm ids.
- func (d *Dao) DMIDCache(c context.Context, tp int32, oid int64, cnt, n, limit int64) (dmids []int64, err error) {
- var (
- conn = d.dmSegRds.Get(c)
- key = keyIdx(tp, oid, cnt, n)
- )
- defer conn.Close()
- if dmids, err = redis.Int64s(conn.Do("ZRANGE", key, 0, -1)); err != nil {
- log.Error("DMIDSPCache.conn.DO(ZRANGEBYSCORE %s) error(%v)", key, err)
- }
- return
- }
- // AddDMIDCache add dmid(normal and special) to segment redis.
- func (d *Dao) AddDMIDCache(c context.Context, tp int32, oid, cnt, n int64, dmids ...int64) (err error) {
- key := keyIdx(tp, oid, cnt, n)
- conn := d.dmSegRds.Get(c)
- defer conn.Close()
- for _, dmid := range dmids {
- if err = conn.Send("ZADD", key, dmid, dmid); err != nil {
- log.Error("conn.Send(ZADD %s) error(%v)", key, err)
- return
- }
- }
- if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
- log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush() error(%v)", err)
- return
- }
- for i := 0; i < len(dmids)+1; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // DelDMIDCache delete dm segment cache.
- func (d *Dao) DelDMIDCache(c context.Context, tp int32, oid, cnt, n int64) (err error) {
- key := keyIdx(tp, oid, cnt, n)
- conn := d.dmSegRds.Get(c)
- if _, err = conn.Do("DEL", key); err != nil {
- log.Error("conn.Do(DEL %s) err(%v)", key, err)
- }
- conn.Close()
- return
- }
- // ExpireDMIDSubtitle set expire time of subtitle dmid.
- func (d *Dao) ExpireDMIDSubtitle(c context.Context, tp int32, oid int64) (ok bool, err error) {
- key := keyIdxSub(tp, oid)
- conn := d.dmSegRds.Get(c)
- if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.dmSegExpire)); err != nil {
- log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
- }
- conn.Close()
- return
- }
- // DMIDSubtitleCache get subtitle dmid.
- func (d *Dao) DMIDSubtitleCache(c context.Context, tp int32, oid int64, ps, pe, limit int64) (dmids []int64, err error) {
- var (
- conn = d.dmSegRds.Get(c)
- key = keyIdxSub(tp, oid)
- )
- defer conn.Close()
- if dmids, err = redis.Int64s(conn.Do("ZRANGEBYSCORE", key, ps, pe, "LIMIT", 0, limit)); err != nil {
- log.Error("conn.DO(ZRANGEBYSCORE %s) error(%v)", key, err)
- }
- return
- }
- // AddDMIDSubtitleCache add subtitle dmid to redis.
- func (d *Dao) AddDMIDSubtitleCache(c context.Context, tp int32, oid int64, dms ...*model.DM) (err error) {
- key := keyIdxSub(tp, oid)
- conn := d.dmSegRds.Get(c)
- defer conn.Close()
- for _, dm := range dms {
- if err = conn.Send("ZADD", key, dm.Progress, dm.ID); err != nil {
- log.Error("conn.Send(ZADD %s) error(%v)", key, err)
- return
- }
- }
- if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
- log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush() error(%v)", err)
- return
- }
- for i := 0; i < len(dms)+1; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // DelDMIDSubtitleCache delete subtitle dmid cache.
- func (d *Dao) DelDMIDSubtitleCache(c context.Context, tp int32, oid int64) (err error) {
- key := keyIdxSub(tp, oid)
- conn := d.dmSegRds.Get(c)
- if _, err = conn.Do("DEL", key); err != nil {
- log.Error("conn.Do(DEL %s) error(%v)", key, err)
- }
- conn.Close()
- return
- }
- // AddIdxContentCaches add index content cache to redis.
- func (d *Dao) AddIdxContentCaches(c context.Context, tp int32, oid int64, dms ...*model.DM) (err error) {
- var (
- conn = d.dmSegRds.Get(c)
- key = keyIdxContent(tp, oid)
- )
- defer conn.Close()
- for _, dm := range dms {
- if err = conn.Send("HSET", key, dm.ID, dm.ToXMLSeg()); err != nil {
- log.Error("conn.Send(HSET %s,%v) error(%v)", key, dm, err)
- return
- }
- }
- if err = conn.Send("EXPIRE", key, d.dmSegExpire); err != nil {
- log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush() error(%v)", err)
- return
- }
- for i := 0; i <= len(dms); i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // DelIdxContentCaches del index content cache.
- func (d *Dao) DelIdxContentCaches(c context.Context, tp int32, oid int64, dmids ...int64) (err error) {
- key := keyIdxContent(tp, oid)
- conn := d.dmSegRds.Get(c)
- args := []interface{}{key}
- for _, dmid := range dmids {
- args = append(args, dmid)
- }
- if _, err = conn.Do("HDEL", args...); err != nil {
- log.Error("conn.Do(HDEL %s) error(%v)", key, err)
- }
- conn.Close()
- return
- }
- // IdxContentCache get xml info by dmid.
- func (d *Dao) IdxContentCache(c context.Context, tp int32, oid int64, dmids []int64) (res []byte, missed []int64, err error) {
- var (
- k int
- dmid int64
- values [][]byte
- key = keyIdxContent(tp, oid)
- args = []interface{}{key}
- )
- for _, dmid = range dmids {
- args = append(args, dmid)
- }
- conn := d.dmSegRds.Get(c)
- defer conn.Close()
- if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
- log.Error("conn.Do(HMGET %v) error(%v)", args, err)
- if err == redis.ErrNil {
- return nil, nil, nil
- }
- return
- }
- for k, dmid = range dmids {
- if len(values[k]) == 0 {
- missed = append(missed, dmid)
- continue
- }
- res = append(res, values[k]...)
- }
- return
- }
- // IdxContentCacheV2 get elems info by dmid.
- func (d *Dao) IdxContentCacheV2(c context.Context, tp int32, oid int64, dmids []int64) (elems []*model.Elem, missed []int64, err error) {
- var (
- values [][]byte
- key = keyIdxContent(tp, oid)
- args = []interface{}{key}
- )
- for _, dmid := range dmids {
- args = append(args, dmid)
- }
- conn := d.dmSegRds.Get(c)
- defer conn.Close()
- if values, err = redis.ByteSlices(conn.Do("HMGET", args...)); err != nil {
- log.Error("conn.Do(HMGET %v) error(%v)", args, err)
- if err == redis.ErrNil {
- return nil, nil, nil
- }
- return
- }
- for k, dmid := range dmids {
- if len(values[k]) == 0 {
- missed = append(missed, dmid)
- continue
- }
- elem, err := d.xmlToElem(values[k])
- if err != nil {
- missed = append(missed, dmid)
- continue
- }
- elems = append(elems, elem)
- }
- return
- }
- // 在缓存过渡期将<d p="弹幕ID,弹幕属性,播放时间,弹幕模式,字体大小,颜色,发送时间,弹幕池,用户hash id">弹幕内容</d>
- // 装换为 model.Elem结构
- func (d *Dao) xmlToElem(data []byte) (e *model.Elem, err error) {
- var v struct {
- XMLName xml.Name `xml:"d"`
- Attribute string `xml:"p,attr"`
- Content string `xml:",chardata"`
- }
- if err = xml.Unmarshal(data, &v); err != nil {
- return
- }
- e = &model.Elem{Content: v.Content, Attribute: v.Attribute}
- return
- }
|