redis.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package spam
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. // Cache Cache
  9. type Cache struct {
  10. redisPool *redis.Pool
  11. expireRp int
  12. expireAct int
  13. }
  14. // NewCache NewCache
  15. func NewCache(c *redis.Config) *Cache {
  16. return &Cache{
  17. redisPool: redis.NewPool(c),
  18. expireRp: 60, // 60s
  19. expireAct: 20, // 20s
  20. }
  21. }
  22. func (c *Cache) keyRcntCnt(mid int64) string {
  23. return "rc_" + strconv.FormatInt(mid, 10)
  24. }
  25. func (c *Cache) keyUpRcntCnt(mid int64) string {
  26. return "urc_" + strconv.FormatInt(mid, 10)
  27. }
  28. func (c *Cache) keyDailyCnt(mid int64) string {
  29. return "rd_" + strconv.FormatInt(mid, 10)
  30. }
  31. func (c *Cache) keyActRec(mid int64) string {
  32. return "ra_" + strconv.FormatInt(mid, 10)
  33. }
  34. func (c *Cache) keySpamRpRec(mid int64) string {
  35. return "sr_" + strconv.FormatInt(mid, 10)
  36. }
  37. func (c *Cache) keySpamRpDaily(mid int64) string {
  38. return "sd_" + strconv.FormatInt(mid, 10)
  39. }
  40. func (c *Cache) keySpamActRec(mid int64) string {
  41. return "sa_" + strconv.FormatInt(mid, 10)
  42. }
  43. // IncrReply incr user reply count.
  44. func (c *Cache) IncrReply(ctx context.Context, mid int64, isUp bool) (count int, err error) {
  45. key := c.keyRcntCnt(mid)
  46. if isUp {
  47. key = c.keyUpRcntCnt(mid)
  48. }
  49. conn := c.redisPool.Get(ctx)
  50. defer conn.Close()
  51. conn.Send("INCR", key)
  52. conn.Send("EXPIRE", key, c.expireRp)
  53. if err = conn.Flush(); err != nil {
  54. log.Error("conn.Flush error(%v)", err)
  55. return
  56. }
  57. if count, err = redis.Int(conn.Receive()); err != nil {
  58. log.Error("conn.Receive error(%v)", key, err)
  59. return
  60. }
  61. if _, err = conn.Receive(); err != nil {
  62. log.Error("conn.Receive error(%v)", key, err)
  63. return
  64. }
  65. return
  66. }
  67. // IncrAct incr user action count.
  68. func (c *Cache) IncrAct(ctx context.Context, mid int64) (count int, err error) {
  69. key := c.keyActRec(mid)
  70. conn := c.redisPool.Get(ctx)
  71. defer conn.Close()
  72. conn.Send("INCR", key)
  73. conn.Send("EXPIRE", key, c.expireAct)
  74. if err = conn.Flush(); err != nil {
  75. log.Error("conn.Flush error(%v)", err)
  76. return
  77. }
  78. if count, err = redis.Int(conn.Receive()); err != nil {
  79. log.Error("conn.Receive error(%v)", key, err)
  80. return
  81. }
  82. if _, err = conn.Receive(); err != nil {
  83. log.Error("conn.Receive error(%v)", key, err)
  84. return
  85. }
  86. return
  87. }
  88. // IncrDailyReply IncrDailyReply
  89. func (c *Cache) IncrDailyReply(ctx context.Context, mid int64) (count int, err error) {
  90. key := c.keyDailyCnt(mid)
  91. conn := c.redisPool.Get(ctx)
  92. defer conn.Close()
  93. if count, err = redis.Int(conn.Do("INCR", key)); err != nil {
  94. log.Error("conn.Do(INCRBY, %s), error(%v)", key, err)
  95. }
  96. return
  97. }
  98. // TTLDailyReply TTLDailyReply
  99. func (c *Cache) TTLDailyReply(ctx context.Context, mid int64) (ttl int, err error) {
  100. key := c.keyDailyCnt(mid)
  101. conn := c.redisPool.Get(ctx)
  102. defer conn.Close()
  103. if ttl, err = redis.Int(conn.Do("TTL", key)); err != nil {
  104. log.Error("conn.Do(TTL, %s), error(%v)", key, err)
  105. }
  106. return
  107. }
  108. // ExpireDailyReply ExpireDailyReply
  109. func (c *Cache) ExpireDailyReply(ctx context.Context, mid int64, exp int) (err error) {
  110. key := c.keyDailyCnt(mid)
  111. conn := c.redisPool.Get(ctx)
  112. defer conn.Close()
  113. if _, err = conn.Do("EXPIRE", key, exp); err != nil {
  114. log.Error("conn.Do(EXPIRE, %s), error(%v)", key, err)
  115. }
  116. return
  117. }
  118. // SetReplyRecSpam SetReplyRecSpam
  119. func (c *Cache) SetReplyRecSpam(ctx context.Context, mid int64, code, exp int) (err error) {
  120. key := c.keySpamRpRec(mid)
  121. conn := c.redisPool.Get(ctx)
  122. defer conn.Close()
  123. if _, err = conn.Do("SETEX", key, exp, code); err != nil {
  124. log.Error("conn.Do error(%v)", err)
  125. }
  126. return
  127. }
  128. // SetReplyDailySpam SetReplyDailySpam
  129. func (c *Cache) SetReplyDailySpam(ctx context.Context, mid int64, code, exp int) (err error) {
  130. key := c.keySpamRpDaily(mid)
  131. conn := c.redisPool.Get(ctx)
  132. defer conn.Close()
  133. if _, err = conn.Do("SETEX", key, exp, code); err != nil {
  134. log.Error("conn.Do error(%v)", err)
  135. }
  136. return
  137. }
  138. // SetActionRecSpam SetActionRecSpam
  139. func (c *Cache) SetActionRecSpam(ctx context.Context, mid int64, code, exp int) (err error) {
  140. key := c.keySpamActRec(mid)
  141. conn := c.redisPool.Get(ctx)
  142. defer conn.Close()
  143. if _, err = conn.Do("SETEX", key, exp, code); err != nil {
  144. log.Error("conn.Do error(%v)", err)
  145. }
  146. return
  147. }