123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package dao
- import (
- "context"
- "fmt"
- "strconv"
- "go-common/app/service/main/relation/model"
- gmc "go-common/library/cache/memcache"
- "go-common/library/cache/redis"
- "go-common/library/log"
- "go-common/library/time"
- )
- const (
- _prefixFollowings = "at_"
- _prefixTags = "tags_" // user tag info.
- )
- func tagsKey(mid int64) string {
- return _prefixTags + strconv.FormatInt(mid, 10)
- }
- func followingsKey(mid int64) string {
- return _prefixFollowings + strconv.FormatInt(mid, 10)
- }
- // ==== redis ===
- // AddFollowingCache add following cache.
- func (d *Dao) AddFollowingCache(c context.Context, mid int64, following *model.Following) (err error) {
- var (
- ok bool
- key = followingsKey(mid)
- )
- conn := d.relRedis.Get(c)
- if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.relExpire)); err != nil {
- log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
- } else if ok {
- var ef []byte
- if ef, err = d.encode(following.Attribute, following.MTime, following.Tag, following.Special); err != nil {
- return
- }
- if _, err = conn.Do("HSET", key, following.Mid, ef); err != nil {
- log.Error("conn.Do(HSET, %s, %d) error(%v)", key, following.Mid, err)
- }
- }
- conn.Close()
- return
- }
- // DelFollowing del following cache.
- func (d *Dao) DelFollowing(c context.Context, mid int64, following *model.Following) (err error) {
- var (
- ok bool
- key = followingsKey(mid)
- )
- conn := d.relRedis.Get(c)
- if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.relExpire)); err != nil {
- log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
- } else if ok {
- if _, err = conn.Do("HDEL", key, following.Mid); err != nil {
- log.Error("conn.Do(HDEL, %s, %d) error(%v)", key, following.Mid, err)
- }
- }
- conn.Close()
- return
- }
- // encode
- func (d *Dao) encode(attribute uint32, mtime time.Time, tagids []int64, special int32) (res []byte, err error) {
- ft := &model.FollowingTags{Attr: attribute, Ts: mtime, TagIds: tagids, Special: special}
- return ft.Marshal()
- }
- // ===== memcache =====
- const (
- _prefixFollowing = "pb_a_"
- _prefixTagCount = "rs_tmtc_%d" // key of relation tag by mid & tag's count
- )
- func followingKey(mid int64) string {
- return _prefixFollowing + strconv.FormatInt(mid, 10)
- }
- func tagCountKey(mid int64) string {
- return fmt.Sprintf(_prefixTagCount, mid)
- }
- // DelFollowingCache delete following cache.
- func (d *Dao) DelFollowingCache(c context.Context, mid int64) (err error) {
- return d.delFollowingCache(c, followingKey(mid))
- }
- // delFollowingCache delete following cache.
- func (d *Dao) delFollowingCache(c context.Context, key string) (err error) {
- conn := d.mc.Get(c)
- if err = conn.Delete(key); err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- } else {
- log.Error("conn.Delete(%s) error(%v)", key, err)
- }
- }
- conn.Close()
- return
- }
- // DelTagCountCache del tag count cache.
- func (d *Dao) DelTagCountCache(c context.Context, mid int64) (err error) {
- conn := d.mc.Get(c)
- if err = conn.Delete(tagCountKey(mid)); err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- } else {
- log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
- }
- }
- conn.Close()
- return
- }
- // DelTagsCache is
- func (d *Dao) DelTagsCache(c context.Context, mid int64) (err error) {
- conn := d.mc.Get(c)
- if err = conn.Delete(tagsKey(mid)); err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- } else {
- log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
- }
- }
- conn.Close()
- return
- }
|