redis.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package anchorReward
  2. import (
  3. "context"
  4. "go-common/library/cache/redis"
  5. "go-common/library/log"
  6. )
  7. const (
  8. _preLock = "lk_"
  9. _preExpireCount = "ec_"
  10. )
  11. func lockKey(key string) string {
  12. return _preLock + key
  13. }
  14. func expireCountKey(key string) string {
  15. return _preExpireCount + key
  16. }
  17. //DelLockCache del lock cache.
  18. func (d *Dao) DelLockCache(c context.Context, k string) (err error) {
  19. var (
  20. key = lockKey(k)
  21. conn = d.redis.Get(c)
  22. )
  23. defer conn.Close()
  24. if _, err = conn.Do("DEL", key); err != nil {
  25. log.Error("DelLockCache.conn.Do(del,%v) err(%v)", key, err)
  26. } else {
  27. log.Info("DelLockCache.conn.Do(del,%v)", key)
  28. }
  29. return
  30. }
  31. // GetExpireCountCache .
  32. func (d *Dao) GetExpireCountCache(c context.Context, k string) (count int64, err error) {
  33. var (
  34. key = expireCountKey(k)
  35. conn = d.redis.Get(c)
  36. )
  37. //spew.Dump(k)
  38. defer conn.Close()
  39. item, err := redis.Int(conn.Do("GET", key))
  40. if err != nil {
  41. if err == redis.ErrNil {
  42. count = 0
  43. err = nil
  44. } else {
  45. log.Error("conn.Do(GET %s) error(%v)", key, err)
  46. return 0, err
  47. }
  48. }
  49. count = int64(item)
  50. return
  51. }
  52. // AddExpireCountCache .
  53. func (d *Dao) AddExpireCountCache(c context.Context, k string, times int64) (err error) {
  54. var (
  55. key = expireCountKey(k)
  56. conn = d.redis.Get(c)
  57. )
  58. //spew.Dump(k)
  59. defer conn.Close()
  60. if _, err = conn.Do("INCR", key); err != nil {
  61. log.Error("conn.Do(incr,%v) err(%v)", key, err)
  62. }
  63. if _, err = redis.Bool(conn.Do("EXPIRE", key, times)); err != nil {
  64. log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, times, err)
  65. return
  66. }
  67. return
  68. }
  69. // ClearExpireCountCache .
  70. func (d *Dao) ClearExpireCountCache(c context.Context, k string) (err error) {
  71. var (
  72. key = expireCountKey(k)
  73. conn = d.redis.Get(c)
  74. )
  75. //spew.Dump(k)
  76. defer conn.Close()
  77. if _, err = conn.Do("DEL", key); err != nil {
  78. log.Error("conn.Do(del,%v) err(%v)", key, err)
  79. return
  80. }
  81. return
  82. }
  83. //SetNxLock redis lock.
  84. func (d *Dao) SetNxLock(c context.Context, k string, times int64) (res bool, err error) {
  85. var (
  86. key = lockKey(k)
  87. conn = d.redis.Get(c)
  88. )
  89. defer conn.Close()
  90. if res, err = redis.Bool(conn.Do("SETNX", key, "1")); err != nil {
  91. log.Error("conn.Do(SETNX(%d)) error(%v)", key, err)
  92. return
  93. }
  94. //spew.Dump(res, err )
  95. if res {
  96. if _, err = redis.Bool(conn.Do("EXPIRE", key, times)); err != nil {
  97. log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, times, err)
  98. return
  99. }
  100. log.Info("conn.Do(EXPIRE, %s, %d) ", key, times)
  101. }
  102. return
  103. }