redis.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _prefixBuyFlag = "b_"
  10. _prefixApplyFlag = "a_"
  11. )
  12. func keyBuyFlag(mid int64) string {
  13. return _prefixBuyFlag + strconv.FormatInt(mid, 10)
  14. }
  15. func keyApplyFlag(code string) string {
  16. return _prefixApplyFlag + code
  17. }
  18. // pingRedis ping redis.
  19. func (d *Dao) pingRedis(c context.Context) (err error) {
  20. conn := d.redis.Get(c)
  21. defer conn.Close()
  22. if _, err = redis.String(conn.Do("PING")); err != nil {
  23. log.Error("redis.String(conn.Do(PING)) error(%v)", err)
  24. }
  25. return
  26. }
  27. // SetBuyFlagCache set buy flag cache
  28. func (d *Dao) SetBuyFlagCache(c context.Context, mid int64, f string) (ok bool, err error) {
  29. key := keyBuyFlag(mid)
  30. conn := d.redis.Get(c)
  31. defer conn.Close()
  32. var res interface{}
  33. if res, err = conn.Do("SET", key, f, "EX", d.inviteExpire, "NX"); err != nil {
  34. log.Error("conn.Do(SET, %s, %s, EX, %d, NX)", key, f)
  35. return
  36. }
  37. if res != nil {
  38. ok = true
  39. }
  40. return
  41. }
  42. // DelBuyFlagCache del buy flag cache
  43. func (d *Dao) DelBuyFlagCache(c context.Context, mid int64) (err error) {
  44. key := keyBuyFlag(mid)
  45. conn := d.redis.Get(c)
  46. defer conn.Close()
  47. if _, err = conn.Do("DEL", key); err != nil {
  48. log.Error("conn.Do(DEL, %s)", key)
  49. }
  50. return
  51. }
  52. // SetApplyFlagCache set apply flag cache
  53. func (d *Dao) SetApplyFlagCache(c context.Context, code, f string) (ok bool, err error) {
  54. key := keyApplyFlag(code)
  55. conn := d.redis.Get(c)
  56. defer conn.Close()
  57. var res interface{}
  58. if res, err = conn.Do("SET", key, f, "EX", d.inviteExpire, "NX"); err != nil {
  59. log.Error("conn.Do(SET, %s, %s, EX, %d, NX)", key, f, d.inviteExpire)
  60. return
  61. }
  62. if res != nil {
  63. ok = true
  64. }
  65. return
  66. }
  67. // DelApplyFlagCache del apply Flag Cache
  68. func (d *Dao) DelApplyFlagCache(c context.Context, code string) (err error) {
  69. key := keyApplyFlag(code)
  70. conn := d.redis.Get(c)
  71. defer conn.Close()
  72. if _, err = conn.Do("DEL", key); err != nil {
  73. log.Error("conn.Do(DEL, %s)", key)
  74. }
  75. return
  76. }