redis.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package like
  2. import (
  3. "strconv"
  4. "go-common/library/cache/redis"
  5. "go-common/library/log"
  6. "golang.org/x/net/context"
  7. )
  8. const (
  9. _prefixAttention = "lg_"
  10. )
  11. func redisKey(key string) string {
  12. return _prefixAttention + key
  13. }
  14. //RsSet set res
  15. func (d *Dao) RsSet(c context.Context, key string, value string) (err error) {
  16. var (
  17. rkey = redisKey(key)
  18. conn = d.redis.Get(c)
  19. )
  20. defer conn.Close()
  21. if _, err = conn.Do("SET", rkey, value); err != nil {
  22. log.Error("conn.Send(SET, %s, %s) error(%v)", rkey, value, err)
  23. return
  24. }
  25. return
  26. }
  27. // RbSet setRb
  28. func (d *Dao) RbSet(c context.Context, key string, value []byte) (err error) {
  29. var (
  30. rkey = redisKey(key)
  31. conn = d.redis.Get(c)
  32. )
  33. defer conn.Close()
  34. if _, err = conn.Do("SET", rkey, value); err != nil {
  35. log.Error("conn.Send(SET, %s, %d) error(%v)", rkey, value, err)
  36. return
  37. }
  38. return
  39. }
  40. // RsGet getRs
  41. func (d *Dao) RsGet(c context.Context, key string) (res string, err error) {
  42. var (
  43. rkey = redisKey(key)
  44. conn = d.redis.Get(c)
  45. )
  46. defer conn.Close()
  47. if res, err = redis.String(conn.Do("GET", rkey)); err != nil {
  48. if err == redis.ErrNil {
  49. err = nil
  50. } else {
  51. log.Error("conn.Do(GET key(%s)) error(%v)", rkey, err)
  52. }
  53. return
  54. }
  55. return
  56. }
  57. // RsSetNX NXset get
  58. func (d *Dao) RsSetNX(c context.Context, key string) (res bool, err error) {
  59. var (
  60. rkey = redisKey(key)
  61. conn = d.redis.Get(c)
  62. )
  63. defer conn.Close()
  64. if res, err = redis.Bool(conn.Do("SETNX", rkey, 1)); err != nil {
  65. log.Error("conn.Do(SETNX key(%s)) error(%v)", rkey, err)
  66. return
  67. }
  68. return
  69. }
  70. // Incr incr
  71. func (d *Dao) Incr(c context.Context, key string) (res bool, err error) {
  72. var (
  73. rkey = redisKey(key)
  74. conn = d.redis.Get(c)
  75. )
  76. defer conn.Close()
  77. if res, err = redis.Bool(conn.Do("incr", rkey)); err != nil {
  78. log.Error("conn.Do(incr key(%s)) error(%v)", rkey, err)
  79. return
  80. }
  81. return
  82. }
  83. // CreateSelection Create selection
  84. func (d *Dao) CreateSelection(c context.Context, aid int64, stage int64) (err error) {
  85. key := strconv.FormatInt(aid, 10) + ":" + strconv.FormatInt(stage, 10)
  86. var (
  87. rkeyYes = redisKey(key + ":yes")
  88. rkeyNo = redisKey(key + ":no")
  89. conn = d.redis.Get(c)
  90. )
  91. defer conn.Close()
  92. if err = conn.Send("SET", rkeyYes, 0); err != nil {
  93. log.Error("conn.Send(SET %s) error(%v)", rkeyYes, err)
  94. return
  95. }
  96. if err = conn.Send("SET", rkeyNo, 0); err != nil {
  97. log.Error("conn.Send(SET %s) error(%v)", rkeyNo, err)
  98. return
  99. }
  100. if err = conn.Flush(); err != nil {
  101. log.Error("conn.Flush() error(%v)", err)
  102. return
  103. }
  104. for i := 0; i < 2; i++ {
  105. if _, err = conn.Receive(); err != nil {
  106. log.Error("conn.Receive() error(%v)", err)
  107. return
  108. }
  109. }
  110. return
  111. }
  112. // Selection selection
  113. func (d *Dao) Selection(c context.Context, aid int64, stage int64) (yes int64, no int64, err error) {
  114. key := strconv.FormatInt(aid, 10) + ":" + strconv.FormatInt(stage, 10)
  115. var (
  116. rkeyYes = redisKey(key + ":yes")
  117. rkeyNo = redisKey(key + ":no")
  118. conn = d.redis.Get(c)
  119. )
  120. defer conn.Close()
  121. if err = conn.Send("GET", rkeyYes); err != nil {
  122. log.Error("conn.Send(SET %s) error(%v)", rkeyYes, err)
  123. return
  124. }
  125. if err = conn.Send("GET", rkeyNo); err != nil {
  126. log.Error("conn.Send(SET %s) error(%v)", rkeyNo, err)
  127. return
  128. }
  129. if err = conn.Flush(); err != nil {
  130. log.Error("conn.Flush() error(%v)", err)
  131. return
  132. }
  133. if yes, err = redis.Int64(conn.Receive()); err != nil {
  134. log.Error("conn.Receive(yes) error(%v)", err)
  135. return
  136. }
  137. if no, err = redis.Int64(conn.Receive()); err != nil {
  138. log.Error("conn.Receive(no) error(%v)", err)
  139. return
  140. }
  141. return
  142. }