relation.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. relation "go-common/app/service/main/relation/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. const (
  13. _prefixFollowings = "at_"
  14. _prefixTags = "tags_" // user tag info.
  15. _prefixFollowing = "pb_a_"
  16. _prefixStat = "c_" // key of stat
  17. _prefixTagCount = "rs_tmtc_%d" // key of relation tag by mid & tag's count
  18. )
  19. func statKey(mid int64) string {
  20. return _prefixStat + strconv.FormatInt(mid, 10)
  21. }
  22. func tagsKey(mid int64) string {
  23. return _prefixTags + strconv.FormatInt(mid, 10)
  24. }
  25. func followingsKey(mid int64) string {
  26. return _prefixFollowings + strconv.FormatInt(mid, 10)
  27. }
  28. func followingKey(mid int64) string {
  29. return _prefixFollowing + strconv.FormatInt(mid, 10)
  30. }
  31. func tagCountKey(mid int64) string {
  32. return fmt.Sprintf(_prefixTagCount, mid)
  33. }
  34. // DelStatCache is
  35. func (d *Dao) DelStatCache(ctx context.Context, mid int64) error {
  36. conn := d.mc.Get(ctx)
  37. defer conn.Close()
  38. if err := conn.Delete(statKey(mid)); err != nil {
  39. if err == memcache.ErrNotFound {
  40. return nil
  41. }
  42. log.Error("Failed to delete stat cache: mid: %d: %+v", mid, err)
  43. return err
  44. }
  45. return nil
  46. }
  47. // DelFollowerCache del follower cache
  48. func (d *Dao) DelFollowerCache(ctx context.Context, fid int64) error {
  49. key := followingKey(fid)
  50. conn := d.mc.Get(ctx)
  51. defer conn.Close()
  52. if err := conn.Delete(key); err != nil {
  53. if err == memcache.ErrNotFound {
  54. err = nil
  55. } else {
  56. log.Error("conn.Delete(%s) error(%v)", key, err)
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. // DelFollowing del following cache.
  63. func (d *Dao) DelFollowing(c context.Context, mid int64, following *relation.Following) (err error) {
  64. var (
  65. ok bool
  66. key = followingsKey(mid)
  67. )
  68. conn := d.redis.Get(c)
  69. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.RelationTTL)); err != nil {
  70. log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
  71. } else if ok {
  72. if _, err = conn.Do("HDEL", key, following.Mid); err != nil {
  73. log.Error("conn.Do(HDEL, %s, %d) error(%v)", key, following.Mid, err)
  74. }
  75. }
  76. conn.Close()
  77. return
  78. }
  79. // DelTagsCache is
  80. func (d *Dao) DelTagsCache(ctx context.Context, mid int64) (err error) {
  81. conn := d.mc.Get(ctx)
  82. if err = conn.Delete(tagsKey(mid)); err != nil {
  83. if err == memcache.ErrNotFound {
  84. err = nil
  85. } else {
  86. log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
  87. }
  88. }
  89. conn.Close()
  90. return
  91. }
  92. // AddFollowingCache is
  93. func (d *Dao) AddFollowingCache(c context.Context, mid int64, following *relation.Following) (err error) {
  94. var (
  95. ok bool
  96. key = followingsKey(mid)
  97. )
  98. conn := d.redis.Get(c)
  99. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.RelationTTL)); err != nil {
  100. log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
  101. } else if ok {
  102. var ef []byte
  103. if ef, err = d.encode(following.Attribute, following.MTime, following.Tag, following.Special); err != nil {
  104. return
  105. }
  106. if _, err = conn.Do("HSET", key, following.Mid, ef); err != nil {
  107. log.Error("conn.Do(HSET, %s, %d) error(%v)", key, following.Mid, err)
  108. }
  109. }
  110. conn.Close()
  111. return
  112. }
  113. // encode
  114. func (d *Dao) encode(attribute uint32, mtime xtime.Time, tagids []int64, special int32) (res []byte, err error) {
  115. ft := &relation.FollowingTags{Attr: attribute, Ts: mtime, TagIds: tagids, Special: special}
  116. return ft.Marshal()
  117. }
  118. // DelFollowingCache delete following cache.
  119. func (d *Dao) DelFollowingCache(c context.Context, mid int64) (err error) {
  120. return d.delFollowingCache(c, followingKey(mid))
  121. }
  122. // delFollowingCache delete following cache.
  123. func (d *Dao) delFollowingCache(c context.Context, key string) (err error) {
  124. conn := d.mc.Get(c)
  125. if err = conn.Delete(key); err != nil {
  126. if err == memcache.ErrNotFound {
  127. err = nil
  128. } else {
  129. log.Error("conn.Delete(%s) error(%v)", key, err)
  130. }
  131. }
  132. conn.Close()
  133. return
  134. }
  135. // DelTagCountCache del tag count cache.
  136. func (d *Dao) DelTagCountCache(c context.Context, mid int64) (err error) {
  137. conn := d.mc.Get(c)
  138. if err = conn.Delete(tagCountKey(mid)); err != nil {
  139. if err == memcache.ErrNotFound {
  140. err = nil
  141. } else {
  142. log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
  143. }
  144. }
  145. conn.Close()
  146. return
  147. }