redis.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package toview
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/history/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. const _key = "v_" // mid -> score:time member:aid
  10. // keyToView return key string
  11. func key(mid int64) string {
  12. return _key + strconv.FormatInt(mid, 10)
  13. }
  14. // Expire expire toview by mid.
  15. func (d *Dao) Expire(c context.Context, mid int64) (ok bool, err error) {
  16. conn := d.redis.Get(c)
  17. if ok, err = redis.Bool(conn.Do("EXPIRE", key(mid), d.expire)); err != nil {
  18. log.Error("conn.Do(EXPIRE, %s) error(%v)", key(mid), err)
  19. }
  20. conn.Close()
  21. return
  22. }
  23. // Cache return the user all toview from redis.
  24. func (d *Dao) Cache(c context.Context, mid int64, start, end int) (res []*model.ToView, err error) {
  25. conn := d.redis.Get(c)
  26. defer conn.Close()
  27. values, err := redis.Values(conn.Do("ZREVRANGE", key(mid), start, end, "WITHSCORES"))
  28. if err != nil {
  29. log.Error("dao.Do(ZREVRANGE %v) error(%v)", key(mid), err)
  30. return
  31. }
  32. if len(values) == 0 {
  33. return
  34. }
  35. res = make([]*model.ToView, 0, len(values)/2)
  36. for len(values) > 0 {
  37. t := &model.ToView{}
  38. if values, err = redis.Scan(values, &t.Aid, &t.Unix); err != nil {
  39. log.Error("redis.Scan(%v) error(%v)", values, err)
  40. return
  41. }
  42. res = append(res, t)
  43. }
  44. return
  45. }
  46. // CacheMap return the user all toview map from redis.
  47. func (d *Dao) CacheMap(c context.Context, mid int64) (res map[int64]*model.ToView, err error) {
  48. conn := d.redis.Get(c)
  49. defer conn.Close()
  50. values, err := redis.Values(conn.Do("ZREVRANGE", key(mid), 0, -1, "WITHSCORES"))
  51. if err != nil {
  52. log.Error("dao.Do(ZREVRANGE %v) error(%v)", key(mid), err)
  53. return
  54. }
  55. if len(values) == 0 {
  56. return
  57. }
  58. res = make(map[int64]*model.ToView, len(values)/2)
  59. for len(values) > 0 {
  60. t := &model.ToView{}
  61. if values, err = redis.Scan(values, &t.Aid, &t.Unix); err != nil {
  62. log.Error("redis.Scan(%v) error(%v)", values, err)
  63. return
  64. }
  65. res[t.Aid] = t
  66. }
  67. return
  68. }
  69. // CntCache return the user toview count from redis.
  70. func (d *Dao) CntCache(c context.Context, mid int64) (count int, err error) {
  71. conn := d.redis.Get(c)
  72. if count, err = redis.Int(conn.Do("ZCARD", key(mid))); err != nil {
  73. log.Error("dao.Do(ZCARD,%s) err(%v)", key(mid), err)
  74. }
  75. conn.Close()
  76. return
  77. }
  78. // ClearCache delete the user toview redis.
  79. func (d *Dao) ClearCache(c context.Context, mid int64) (err error) {
  80. conn := d.redis.Get(c)
  81. if _, err = conn.Do("DEL", key(mid)); err != nil {
  82. log.Error("conn.Do(DEL %s) error(%v)", key(mid), err)
  83. }
  84. conn.Close()
  85. return
  86. }
  87. // DelCaches delete the user toview redis.
  88. func (d *Dao) DelCaches(c context.Context, mid int64, aids []int64) (err error) {
  89. conn := d.redis.Get(c)
  90. defer conn.Close()
  91. for _, aid := range aids {
  92. if err = conn.Send("ZREM", key(mid), aid); err != nil {
  93. log.Error("conn.Send(ZREM %s,%d) error(%v)", key(mid), aid, err)
  94. return
  95. }
  96. }
  97. if err = conn.Flush(); err != nil {
  98. log.Error("conn.Flush() error(%v)", err)
  99. return
  100. }
  101. for i := 0; i < len(aids); i++ {
  102. if _, err = conn.Receive(); err != nil {
  103. log.Error("conn.Receive() error(%v)", err)
  104. return
  105. }
  106. }
  107. return
  108. }
  109. // AddCache add user toview to redis.
  110. func (d *Dao) AddCache(c context.Context, mid, aid, now int64) error {
  111. return d.addCache(c, key(mid), []*model.ToView{&model.ToView{Aid: aid, Unix: now}})
  112. }
  113. // AddCacheList add user toview to redis.
  114. func (d *Dao) AddCacheList(c context.Context, mid int64, views []*model.ToView) error {
  115. return d.addCache(c, key(mid), views)
  116. }
  117. // addCache add user toview to redis.
  118. func (d *Dao) addCache(c context.Context, key string, views []*model.ToView) (err error) {
  119. conn := d.redis.Get(c)
  120. defer conn.Close()
  121. for _, v := range views {
  122. if err = conn.Send("ZADD", key, v.Unix, v.Aid); err != nil {
  123. log.Error("conn.Send(ZREM %s,%d) error(%v)", key, v.Aid, err)
  124. return
  125. }
  126. }
  127. if err = conn.Send("EXPIRE", key, d.expire); err != nil {
  128. log.Error("conn.Send(EXPIRE) error(%v)", err)
  129. return
  130. }
  131. if err = conn.Flush(); err != nil {
  132. log.Error("conn.Flush() error(%v)", err)
  133. return
  134. }
  135. for i := 0; i < len(views)+1; i++ {
  136. if _, err = conn.Receive(); err != nil {
  137. log.Error("conn.Receive() error(%v)", err)
  138. return
  139. }
  140. }
  141. return
  142. }
  143. // PingRedis check redis connection
  144. func (d *Dao) PingRedis(c context.Context) (err error) {
  145. return
  146. }