cache_update.go 3.4 KB

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