redis.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _addPrefix2 = "nca_"
  10. )
  11. func hashField(aid, tp int64) int64 {
  12. return aid*1000 + tp
  13. }
  14. func addKey2(mid int64) (key string) {
  15. key = _addPrefix2 + strconv.FormatInt(mid, 10)
  16. return
  17. }
  18. // CoinsAddedCache get coin added of archive.
  19. func (d *Dao) CoinsAddedCache(c context.Context, mid, aid, tp int64) (added int64, err error) {
  20. conn := d.redis.Get(c)
  21. defer conn.Close()
  22. key := addKey2(mid)
  23. if added, err = redis.Int64(conn.Do("HGET", key, hashField(aid, tp))); err != nil {
  24. if err == redis.ErrNil {
  25. err = nil
  26. return
  27. }
  28. PromError("redis:CoinsAddedCache")
  29. log.Errorv(c, log.KV("log", "redis.Do(HGET)"), log.KV("err", err), log.KV("mid", mid))
  30. }
  31. return
  32. }
  33. // SetCoinAddedCache set coin added of archive
  34. func (d *Dao) SetCoinAddedCache(c context.Context, mid, aid, tp, count int64) (err error) {
  35. conn := d.redis.Get(c)
  36. defer conn.Close()
  37. key := addKey2(mid)
  38. defer func() {
  39. if err != nil {
  40. PromError("redis:SetCoinAddedCache")
  41. log.Errorv(c,
  42. log.KV("log", "s.coinDao.SetCoinAdded()"),
  43. log.KV("mid", mid),
  44. log.KV("aid", aid),
  45. log.KV("err", err),
  46. )
  47. }
  48. }()
  49. if err = conn.Send("HSETNX", key, hashField(aid, tp), count); err != nil {
  50. return
  51. }
  52. if err = conn.Send("EXPIRE", key, d.expireAdded); err != nil {
  53. return
  54. }
  55. if err = conn.Flush(); err != nil {
  56. return
  57. }
  58. if _, err = redis.Bool(conn.Receive()); err != nil {
  59. return
  60. }
  61. conn.Receive()
  62. return
  63. }
  64. // SetCoinAddedsCache multiset added cache
  65. func (d *Dao) SetCoinAddedsCache(c context.Context, mid int64, counts map[int64]int64) (err error) {
  66. if len(counts) == 0 {
  67. // 空缓存
  68. counts = map[int64]int64{-1: -1}
  69. }
  70. key := addKey2(mid)
  71. conn := d.redis.Get(c)
  72. defer conn.Close()
  73. for field, count := range counts {
  74. if err = conn.Send("HSETNX", key, field, count); err != nil {
  75. log.Errorv(c,
  76. log.KV("log", "conn.Send(HSETNX)"),
  77. log.KV("err", err),
  78. log.KV("mid", mid),
  79. )
  80. PromError("redis:SetCoinAddedsCache")
  81. return
  82. }
  83. }
  84. if err = conn.Send("EXPIRE", key, d.expireAdded); err != nil {
  85. log.Errorv(c,
  86. log.KV("log", "conn.Send(EXPIRE)"),
  87. log.KV("err", err),
  88. log.KV("mid", mid),
  89. )
  90. PromError("redis:SetCoinAddedsCache")
  91. return
  92. }
  93. if err = conn.Flush(); err != nil {
  94. log.Errorv(c, log.KV("log", "conn.Flush()"), log.KV("err", err), log.KV("mid", mid))
  95. PromError("redis:SetCoinAddedsCache")
  96. return
  97. }
  98. for i := 0; i < len(counts)+1; i++ {
  99. if _, err = conn.Receive(); err != nil {
  100. log.Errorv(c, log.KV("log", "conn.Recive()"), log.KV("err", err), log.KV("mid", mid))
  101. PromError("redis:SetCoinAddedsCache")
  102. return
  103. }
  104. }
  105. return
  106. }
  107. // IncrCoinAddedCache Incr coin added
  108. func (d *Dao) IncrCoinAddedCache(c context.Context, mid, aid, tp, count int64) (err error) {
  109. conn := d.redis.Get(c)
  110. defer conn.Close()
  111. key := addKey2(mid)
  112. if _, err = conn.Do("HINCRBY", key, hashField(aid, tp), count); err != nil {
  113. PromError("redis:IncrCoinAdded")
  114. log.Errorv(c, log.KV("log", "conn.Do(HINCRBY) error"), log.KV("err", err), log.KV("mid", mid), log.KV("aid", aid))
  115. }
  116. return
  117. }
  118. // ExpireCoinAdded set expire time for coinadded
  119. func (d *Dao) ExpireCoinAdded(c context.Context, mid int64) (ok bool, err error) {
  120. conn := d.redis.Get(c)
  121. defer conn.Close()
  122. key := addKey2(mid)
  123. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.expireAdded)); err != nil {
  124. if err == redis.ErrNil {
  125. err = nil
  126. return
  127. }
  128. PromError("redis:ExpireCoinAdded")
  129. log.Errorv(c, log.KV("log", "conn.Do(EXPIRE)"), log.KV("err", err))
  130. }
  131. return
  132. }