redis.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. "time"
  8. "github.com/satori/go.uuid"
  9. )
  10. func bagIDCache(uid, giftID, expireAt int64) string {
  11. return fmt.Sprintf("bag_id:%d:%d:%d", uid, giftID, expireAt)
  12. }
  13. // SetBagIDCache SetBagIDCache
  14. func (d *Dao) SetBagIDCache(ctx context.Context, uid, giftID, expireAt, bagID, expire int64) (err error) {
  15. key := bagIDCache(uid, giftID, expireAt)
  16. conn := d.redis.Get(ctx)
  17. defer conn.Close()
  18. _, err = conn.Do("SETEX", key, expire, bagID)
  19. if err != nil {
  20. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  21. }
  22. return
  23. }
  24. func bagListKey(uid int64) string {
  25. return fmt.Sprintf("bag_list:%d", uid)
  26. }
  27. // ClearBagListCache ClearBagListCache
  28. func (d *Dao) ClearBagListCache(ctx context.Context, uid int64) (err error) {
  29. key := bagListKey(uid)
  30. conn := d.redis.Get(ctx)
  31. defer conn.Close()
  32. _, err = conn.Do("DEL", key)
  33. if err != nil {
  34. log.Error("conn.Do(DEL, %s) error(%v)", key, err)
  35. }
  36. return
  37. }
  38. // GetBagIDCache GetBagIDCache
  39. func (d *Dao) GetBagIDCache(ctx context.Context, uid, giftID, expireAt int64) (bagID int64, err error) {
  40. key := bagIDCache(uid, giftID, expireAt)
  41. conn := d.redis.Get(ctx)
  42. defer conn.Close()
  43. bagID, err = redis.Int64(conn.Do("GET", key))
  44. if err != nil {
  45. if err == redis.ErrNil {
  46. err = nil
  47. } else {
  48. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  49. }
  50. return
  51. }
  52. return
  53. }
  54. func bagNumKey(uid, giftID, expireAt int64) string {
  55. return fmt.Sprintf("bag_num:%d:%d:%d", uid, giftID, expireAt)
  56. }
  57. // SetBagNumCache SetBagNumCache
  58. func (d *Dao) SetBagNumCache(ctx context.Context, uid, giftID, expireAt, giftNum, expire int64) (err error) {
  59. key := bagNumKey(uid, giftID, expireAt)
  60. conn := d.redis.Get(ctx)
  61. defer conn.Close()
  62. _, err = conn.Do("SETEX", key, expire, giftNum)
  63. if err != nil {
  64. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  65. }
  66. return
  67. }
  68. //Lock Lock
  69. func (d *Dao) Lock(ctx context.Context, key string, ttl int, retry int, retryDelay int) (gotLock bool, lockValue string, err error) {
  70. if retry <= 0 {
  71. retry = 1
  72. }
  73. lockValue = uuid.NewV4().String()
  74. retryTimes := 0
  75. conn := d.redis.Get(ctx)
  76. defer conn.Close()
  77. realKey := lockKey(key)
  78. for ; retryTimes < retry; retryTimes++ {
  79. var res interface{}
  80. res, err = conn.Do("SET", realKey, lockValue, "PX", ttl, "NX")
  81. if err != nil {
  82. log.Error("redis_lock failed:%s:%v", realKey, err)
  83. break
  84. }
  85. if res != nil {
  86. gotLock = true
  87. break
  88. }
  89. time.Sleep(time.Duration(retryDelay) * time.Millisecond)
  90. }
  91. return
  92. }
  93. func lockKey(key string) string {
  94. return fmt.Sprintf("gift_job_lock:%s", key)
  95. }