123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- package like
- import (
- "context"
- "encoding/json"
- "fmt"
- match "go-common/app/interface/main/activity/model/like"
- "go-common/library/cache/redis"
- "go-common/library/log"
- "go-common/library/time"
- )
- const (
- _keyMatch = "mat_%d"
- _keyActMatch = "am_%d"
- _keyObject = "ob_%d"
- _keyObjects = "os_%d"
- _keyUserLog = "ugl_%d_%d"
- _keyMatchFollow = "mf_%d"
- )
- func keyMatch(id int64) string {
- return fmt.Sprintf(_keyMatch, id)
- }
- func keyActMatch(sid int64) string {
- return fmt.Sprintf(_keyActMatch, sid)
- }
- func keyObject(id int64) string {
- return fmt.Sprintf(_keyObject, id)
- }
- func keyObjects(sid int64) string {
- return fmt.Sprintf(_keyObjects, sid)
- }
- func keyUserLog(sid, mid int64) string {
- return fmt.Sprintf(_keyUserLog, sid, mid)
- }
- func keyMatchFollow(mid int64) string {
- return fmt.Sprintf(_keyMatchFollow, mid)
- }
- // MatchCache get match from cache.
- func (d *Dao) MatchCache(c context.Context, id int64) (mat *match.Match, err error) {
- var (
- bs []byte
- key = keyMatch(id)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
- if err == redis.ErrNil {
- err = nil
- mat = nil
- } else {
- log.Error("conn.Do(GET,%s) error(%v)", key, err)
- }
- return
- }
- mat = new(match.Match)
- if err = json.Unmarshal(bs, mat); err != nil {
- log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
- }
- return
- }
- // SetMatchCache set match to cache.
- func (d *Dao) SetMatchCache(c context.Context, id int64, mat *match.Match) (err error) {
- var (
- bs []byte
- key = keyMatch(id)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = json.Marshal(mat); err != nil {
- log.Error("json.Marshal() error(%v)", err)
- return
- }
- if err = conn.Send("SET", key, bs); err != nil {
- log.Error("conn.Send(SET,%s,%d) error(%v)", key, id, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
- log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("add conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < 2; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("add conn.Receive()%d error(%v)", i+1, err)
- return
- }
- }
- return
- }
- // ActMatchCache get match list from cache.
- func (d *Dao) ActMatchCache(c context.Context, sid int64) (res []*match.Match, err error) {
- key := keyActMatch(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
- if err != nil {
- log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
- return
- }
- if len(values) == 0 {
- return
- }
- var num int64
- for len(values) > 0 {
- bs := []byte{}
- if values, err = redis.Scan(values, &bs, &num); err != nil {
- log.Error("redis.Scan(%v) error(%v)", values, err)
- return
- }
- match := &match.Match{}
- if err = json.Unmarshal(bs, match); err != nil {
- log.Error("json.Unmarshal(%v) error(%v)", bs, err)
- return
- }
- res = append(res, match)
- }
- return
- }
- // SetActMatchCache set match list cache.
- func (d *Dao) SetActMatchCache(c context.Context, sid int64, matchs []*match.Match) (err error) {
- key := keyActMatch(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- count := 0
- if err = conn.Send("DEL", key); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", key, err)
- return
- }
- count++
- for _, match := range matchs {
- bs, _ := json.Marshal(match)
- if err = conn.Send("ZADD", key, match.Ctime, bs); err != nil {
- log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
- return
- }
- count++
- }
- if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
- log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
- return
- }
- count++
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < count; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // ObjectCache get object from cache.
- func (d *Dao) ObjectCache(c context.Context, id int64) (mat *match.Object, err error) {
- var (
- bs []byte
- key = keyObject(id)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
- if err == redis.ErrNil {
- err = nil
- mat = nil
- } else {
- log.Error("conn.Do(GET,%s) error(%v)", key, err)
- }
- return
- }
- mat = new(match.Object)
- if err = json.Unmarshal(bs, mat); err != nil {
- log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
- }
- return
- }
- // CacheMatchSubjects .
- func (d *Dao) CacheMatchSubjects(c context.Context, ids []int64) (res map[int64]*match.Object, err error) {
- var (
- key string
- args = redis.Args{}
- bss [][]byte
- )
- for _, pid := range ids {
- key = keyObject(pid)
- args = args.Add(key)
- }
- conn := d.redis.Get(c)
- defer conn.Close()
- if bss, err = redis.ByteSlices(conn.Do("MGET", args...)); err != nil {
- if err == redis.ErrNil {
- err = nil
- } else {
- log.Error("CacheMatchSubjects conn.Do(MGET,%s) error(%v)", key, err)
- }
- return
- }
- res = make(map[int64]*match.Object, len(ids))
- for _, bs := range bss {
- obj := new(match.Object)
- if bs == nil {
- continue
- }
- if err = json.Unmarshal(bs, obj); err != nil {
- log.Error("CacheMatchSubjects json.Unmarshal(%s) error(%v)", string(bs), err)
- err = nil
- continue
- }
- res[obj.ID] = obj
- }
- return
- }
- // SetObjectCache set object to cache.
- func (d *Dao) SetObjectCache(c context.Context, id int64, object *match.Object) (err error) {
- var (
- bs []byte
- key = keyObject(id)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = json.Marshal(object); err != nil {
- log.Error("json.Marshal() error(%v)", err)
- return
- }
- if err = conn.Send("SET", key, bs); err != nil {
- log.Error("conn.Send(HSET,%s,%d) error(%v)", key, id, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
- log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("add conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < 2; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("add conn.Receive()%d error(%v)", i+1, err)
- return
- }
- }
- return
- }
- // AddCacheMatchSubjects .
- func (d *Dao) AddCacheMatchSubjects(c context.Context, data map[int64]*match.Object) (err error) {
- if len(data) == 0 {
- return
- }
- var (
- bs []byte
- keyID string
- keyIDs []string
- argsPid = redis.Args{}
- )
- conn := d.redis.Get(c)
- defer conn.Close()
- for _, v := range data {
- if bs, err = json.Marshal(v); err != nil {
- log.Error("json.Marshal err(%v)", err)
- continue
- }
- keyID = keyObject(v.ID)
- keyIDs = append(keyIDs, keyID)
- argsPid = argsPid.Add(keyID).Add(string(bs))
- }
- if err = conn.Send("MSET", argsPid...); err != nil {
- log.Error("AddCacheMatchSubjects conn.Send(MSET) error(%v)", err)
- return
- }
- count := 1
- for _, v := range keyIDs {
- count++
- if err = conn.Send("EXPIRE", v, d.matchExpire); err != nil {
- log.Error("AddCacheMatchSubjects conn.Send(Expire, %s, %d) error(%v)", v, d.matchExpire, err)
- return
- }
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < count; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // ObjectsCache get object list from cache.
- func (d *Dao) ObjectsCache(c context.Context, sid int64, start, end int) (res []*match.Object, total int, err error) {
- key := keyObjects(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- values, err := redis.Values(conn.Do("ZRANGE", key, start, end, "WITHSCORES"))
- if err != nil {
- log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
- return
- }
- if len(values) == 0 {
- return
- }
- var num int64
- for len(values) > 0 {
- bs := []byte{}
- if values, err = redis.Scan(values, &bs, &num); err != nil {
- log.Error("redis.Scan(%v) error(%v)", values, err)
- return
- }
- object := &match.Object{}
- if err = json.Unmarshal(bs, object); err != nil {
- log.Error("json.Unmarshal(%v) error(%v)", bs, err)
- return
- }
- res = append(res, object)
- }
- total = from(num)
- return
- }
- // SetObjectsCache set object list cache.
- func (d *Dao) SetObjectsCache(c context.Context, sid int64, objects []*match.Object, total int) (err error) {
- key := keyObjects(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- count := 0
- if err = conn.Send("DEL", key); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", key, err)
- return
- }
- count++
- for _, object := range objects {
- bs, _ := json.Marshal(object)
- if err = conn.Send("ZADD", key, combine(object.GameStime, total), bs); err != nil {
- log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
- return
- }
- count++
- }
- if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
- log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
- return
- }
- count++
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < count; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // UserLogCache get user log list from cache.
- func (d *Dao) UserLogCache(c context.Context, sid, mid int64) (res []*match.UserLog, err error) {
- key := keyUserLog(sid, mid)
- conn := d.redis.Get(c)
- defer conn.Close()
- values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, -1, "WITHSCORES"))
- if err != nil {
- log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
- return
- }
- if len(values) == 0 {
- return
- }
- var num int64
- for len(values) > 0 {
- bs := []byte{}
- if values, err = redis.Scan(values, &bs, &num); err != nil {
- log.Error("redis.Scan(%v) error(%v)", values, err)
- return
- }
- userLog := &match.UserLog{}
- if err = json.Unmarshal(bs, userLog); err != nil {
- log.Error("json.Unmarshal(%v) error(%v)", bs, err)
- return
- }
- res = append(res, userLog)
- }
- return
- }
- // SetUserLogCache set user log list cache.
- func (d *Dao) SetUserLogCache(c context.Context, sid, mid int64, userLogs []*match.UserLog) (err error) {
- key := keyUserLog(sid, mid)
- conn := d.redis.Get(c)
- defer conn.Close()
- count := 0
- if err = conn.Send("DEL", key); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", key, err)
- return
- }
- count++
- for _, userLog := range userLogs {
- bs, _ := json.Marshal(userLog)
- if err = conn.Send("ZADD", key, userLog.Ctime, bs); err != nil {
- log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
- return
- }
- count++
- }
- if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
- log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
- return
- }
- count++
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < count; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // DelUserLogCache delete user log cache.
- func (d *Dao) DelUserLogCache(c context.Context, sid, mid int64) (err error) {
- key := keyUserLog(sid, mid)
- conn := d.redis.Get(c)
- defer conn.Close()
- _, err = conn.Do("DEL", key)
- return
- }
- // DelActMatchCache del match cache
- func (d *Dao) DelActMatchCache(c context.Context, sid, matID int64) (err error) {
- matKey := keyMatch(matID)
- actMKey := keyActMatch(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- if err = conn.Send("DEL", matKey); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", actMKey, err)
- return
- }
- if err = conn.Send("DEL", actMKey); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", actMKey, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush() error(%v)", err)
- return
- }
- for i := 0; i < 2; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // DelObjectCache del object cache
- func (d *Dao) DelObjectCache(c context.Context, objID, sid int64) (err error) {
- objKey := keyObject(objID)
- objsKey := keyObjects(sid)
- conn := d.redis.Get(c)
- defer conn.Close()
- if err = conn.Send("DEL", objKey); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", objKey, err)
- return
- }
- if err = conn.Send("DEL", objsKey); err != nil {
- log.Error("conn.Send(DEL, %s) error(%v)", objKey, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("conn.Flush() error(%v)", err)
- return
- }
- for i := 0; i < 2; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("conn.Receive() error(%v)", err)
- return
- }
- }
- return
- }
- // AddFollow add follow teams
- func (d *Dao) AddFollow(c context.Context, mid int64, teams []string) (err error) {
- var (
- bs []byte
- key = keyMatchFollow(mid)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = json.Marshal(teams); err != nil {
- log.Error("json.Marshal() error(%v)", err)
- return
- }
- if err = conn.Send("SET", key, bs); err != nil {
- log.Error("conn.Send(SET,%s,%d) error(%v)", key, mid, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.followExpire); err != nil {
- log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
- return
- }
- if err = conn.Flush(); err != nil {
- log.Error("add conn.Flush error(%v)", err)
- return
- }
- for i := 0; i < 2; i++ {
- if _, err = conn.Receive(); err != nil {
- log.Error("add conn.Receive()%d error(%v)", i+1, err)
- return
- }
- }
- return
- }
- // Follow get follow teams
- func (d *Dao) Follow(c context.Context, mid int64) (res []string, err error) {
- var (
- bs []byte
- key = keyMatchFollow(mid)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
- if err == redis.ErrNil {
- err = nil
- } else {
- log.Error("conn.Do(GET,%s) error(%v)", key, err)
- }
- return
- }
- if err = json.Unmarshal(bs, &res); err != nil {
- log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
- }
- return
- }
- func from(i int64) int {
- return int(i & 0xffff)
- }
- func combine(ctime time.Time, count int) int64 {
- return ctime.Time().Unix()<<16 | int64(count)
- }
|