redis.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _prefixLimitDay = "ld_%d_%d_%s"
  10. _prefixLimitBiz = "lb_%d_%d_%s_%d"
  11. _prefixLimitNotLive = "lnl_%d_%s"
  12. )
  13. func limitDayKey(day string, app, mid int64) string {
  14. return fmt.Sprintf(_prefixLimitDay, app, mid, day)
  15. }
  16. func limitBizKey(day string, app, mid, biz int64) string {
  17. return fmt.Sprintf(_prefixLimitBiz, app, mid, day, biz)
  18. }
  19. func limitNotLiveKey(day string, mid int64) string {
  20. return fmt.Sprintf(_prefixLimitNotLive, mid, day)
  21. }
  22. // pingRedis ping redis.
  23. func (d *Dao) pingRedis(ctx context.Context) (err error) {
  24. conn := d.redis.Get(ctx)
  25. defer conn.Close()
  26. if _, err = conn.Do("SET", "PING", "PONG"); err != nil {
  27. PromError("redis: ping remote")
  28. log.Error("remote redis: conn.Do(SET,PING,PONG) error(%v)", err)
  29. }
  30. return
  31. }
  32. // LimitDayCache gets limit cache by day & mid.
  33. // 测试用,业务用不着
  34. func (d *Dao) LimitDayCache(ctx context.Context, day string, app, mid int64) (count int, err error) {
  35. var (
  36. key = limitDayKey(day, app, mid)
  37. conn = d.redis.Get(ctx)
  38. )
  39. defer conn.Close()
  40. if count, err = redis.Int(conn.Do("GET", key)); err != nil {
  41. PromError("redis:LimitDayCache")
  42. log.Error("LimitDayCache(%s,%d,%d) error(%v)", day, app, mid, err)
  43. }
  44. return
  45. }
  46. // IncrLimitDayCache increases and gets limit cache by day & mid.
  47. func (d *Dao) IncrLimitDayCache(ctx context.Context, day string, app, mid int64) (count int, err error) {
  48. var (
  49. key = limitDayKey(day, app, mid)
  50. conn = d.redis.Get(ctx)
  51. )
  52. defer conn.Close()
  53. if err = conn.Send("INCR", key); err != nil {
  54. PromError("redis:IncrLimitDayCache")
  55. log.Error("IncrLimitDayCache(%s,%d,%d) error(%v)", day, app, mid, err)
  56. return
  57. }
  58. if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
  59. PromError("redis:IncrLimitDayCache:expire")
  60. log.Error("IncrLimitDayCache(%s,%d,%d) expire error(%v)", day, app, mid, err)
  61. return
  62. }
  63. if err = conn.Flush(); err != nil {
  64. PromError("redis:IncrLimitDayCache:flush")
  65. log.Error("IncrLimitDayCache(%s,%d,%d) flush error(%v)", day, app, mid, err)
  66. return
  67. }
  68. if count, err = redis.Int(conn.Receive()); err != nil {
  69. PromError("redis:IncrLimitDayCache:receive:incr")
  70. log.Error("IncrLimitDayCache(%s,%d,%d) receive incr error(%+v)", day, app, mid, err)
  71. return
  72. }
  73. if _, err = conn.Receive(); err != nil {
  74. PromError("redis:IncrLimitDayCache:receive:expire")
  75. log.Error("IncrLimitDayCache(%s,%d,%d) receive expire error(%+v)", day, app, mid, err)
  76. }
  77. return
  78. }
  79. // IncrLimitBizCache increases and gets limit cache by day & mid & bisiness.
  80. func (d *Dao) IncrLimitBizCache(ctx context.Context, day string, app, mid, biz int64) (count int, err error) {
  81. var (
  82. key = limitBizKey(day, app, mid, biz)
  83. conn = d.redis.Get(ctx)
  84. )
  85. defer conn.Close()
  86. if err = conn.Send("INCR", key); err != nil {
  87. PromError("redis:IncrLimitBizCache")
  88. log.Error("IncrLimitBizCache(%s,%d,%d,%d) error(%v)", day, app, mid, biz, err)
  89. return
  90. }
  91. if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
  92. PromError("redis:IncrLimitBizCache:expire")
  93. log.Error("IncrLimitBizCache(%s,%d,%d,%d) expire error(%v)", day, app, mid, biz, err)
  94. return
  95. }
  96. if err = conn.Flush(); err != nil {
  97. PromError("redis:IncrLimitBizCache:flush")
  98. log.Error("IncrLimitBizCache(%s,%d,%d,%d) flush error(%v)", day, app, mid, biz, err)
  99. return
  100. }
  101. if count, err = redis.Int(conn.Receive()); err != nil {
  102. PromError("redis:IncrLimitBizCache:receive:incr")
  103. log.Error("IncrLimitBizCache(%s,%d,%d,%d) receive incr error(%+v)", day, app, mid, biz, err)
  104. return
  105. }
  106. if _, err = conn.Receive(); err != nil {
  107. PromError("redis:IncrLimitBizCache:receive:expire")
  108. log.Error("IncrLimitBizCache(%s,%d,%d,%d) receive expire error(%+v)", day, app, mid, biz, err)
  109. }
  110. return
  111. }
  112. // IncrLimitNotLiveCache increases and gets not live limit cache by day & mid.
  113. func (d *Dao) IncrLimitNotLiveCache(ctx context.Context, day string, mid int64) (count int, err error) {
  114. var (
  115. key = limitNotLiveKey(day, mid)
  116. conn = d.redis.Get(ctx)
  117. )
  118. defer conn.Close()
  119. if err = conn.Send("INCR", key); err != nil {
  120. PromError("redis:IncrLimitNotLiveCache")
  121. log.Error("IncrLimitNotLiveCache(%s,%d) error(%v)", day, mid, err)
  122. return
  123. }
  124. if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
  125. PromError("redis:IncrLimitNotLiveCache:expire")
  126. log.Error("IncrLimitNotLiveCache(%s,%d) expire error(%v)", day, mid, err)
  127. return
  128. }
  129. if err = conn.Flush(); err != nil {
  130. PromError("redis:IncrLimitNotLiveCache:flush")
  131. log.Error("IncrLimitNotLiveCache(%s,%d) flush error(%v)", day, mid, err)
  132. return
  133. }
  134. if count, err = redis.Int(conn.Receive()); err != nil {
  135. PromError("redis:IncrLimitNotLiveCache:receive:incr")
  136. log.Error("IncrLimitNotLiveCache(%s,%d) receive incr error(%+v)", day, mid, err)
  137. return
  138. }
  139. if _, err = conn.Receive(); err != nil {
  140. PromError("redis:IncrLimitNotLiveCache:receive:expire")
  141. log.Error("IncrLimitNotLiveCache(%s,%d) receive expire error(%+v)", day, mid, err)
  142. }
  143. return
  144. }