cache.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. stderr "errors"
  6. "github.com/pkg/errors"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. )
  10. // Incr incr
  11. func (d *Dao) Incr(context context.Context, name string) int {
  12. conn := d.redis.Get(context)
  13. defer conn.Close()
  14. reply, _ := redis.Int(conn.Do("INCR", name))
  15. return reply
  16. }
  17. // Lock .
  18. func (d *Dao) Lock(ctx context.Context, realKey string, ttl int, retry int, retryDelay int) (gotLock bool, lockValue string, err error) {
  19. if retry <= 0 {
  20. retry = 1
  21. }
  22. lockValue = "locked:" + randomString(5)
  23. retryTimes := 0
  24. conn := d.redis.Get(ctx)
  25. defer conn.Close()
  26. for ; retryTimes < retry; retryTimes++ {
  27. var res interface{}
  28. res, err = conn.Do("SET", realKey, lockValue, "PX", ttl, "NX")
  29. if err != nil {
  30. log.Error("redis_lock failed:%s:%s", realKey, err.Error())
  31. break
  32. }
  33. if res != nil {
  34. gotLock = true
  35. break
  36. }
  37. time.Sleep(time.Duration(retryDelay * 1000))
  38. }
  39. return
  40. }
  41. // UnLock .
  42. func (d *Dao) UnLock(ctx context.Context, realKey string, lockValue string) (err error) {
  43. conn := d.redis.Get(ctx)
  44. defer conn.Close()
  45. res, err := redis.String(conn.Do("GET", realKey))
  46. if err != nil {
  47. return
  48. }
  49. if res != lockValue {
  50. err = ErrUnLockGet
  51. return
  52. }
  53. _, err = conn.Do("DEL", realKey)
  54. return
  55. }
  56. // Get get
  57. func (d *Dao) Get(ctx context.Context, key string) (string, error) {
  58. conn := d.redis.Get(ctx)
  59. defer conn.Close()
  60. return redis.String(conn.Do("GET", key))
  61. }
  62. // GetInt64 GetInt64
  63. func (d *Dao) GetInt64(ctx context.Context, key string) (int64, error) {
  64. conn := d.redis.Get(ctx)
  65. defer conn.Close()
  66. return redis.Int64(conn.Do("GET", key))
  67. }
  68. // Set Set
  69. func (d *Dao) Set(ctx context.Context, key string, value interface{}) (bool, error) {
  70. conn := d.redis.Get(ctx)
  71. defer conn.Close()
  72. if _, err := redis.String(conn.Do("SET", key, value)); err != nil {
  73. return false, errors.WithStack(err)
  74. }
  75. return true, nil
  76. }
  77. // SetEx SetEx
  78. func (d *Dao) SetEx(ctx context.Context, key string, value interface{}, ex int) (bool, error) {
  79. conn := d.redis.Get(ctx)
  80. defer conn.Close()
  81. if _, err := redis.String(conn.Do("SET", key, value, "EX", ex)); err != nil {
  82. return false, errors.WithStack(err)
  83. }
  84. return true, nil
  85. }
  86. //HMSet HMSet
  87. func (d *Dao) HMSet(ctx context.Context, key string, value interface{}) (bool, error) {
  88. conn := d.redis.Get(ctx)
  89. defer conn.Close()
  90. arg := redis.Args{}.Add(key).AddFlat(value)
  91. if _, err := redis.String(conn.Do("HMSET", arg...)); err != nil {
  92. return false, errors.WithStack(err)
  93. }
  94. return true, nil
  95. }
  96. //Del Del
  97. func (d *Dao) Del(ctx context.Context, key string) (int64, error) {
  98. conn := d.redis.Get(ctx)
  99. defer conn.Close()
  100. return redis.Int64(conn.Do("DEL", key))
  101. }
  102. //SIsMember SIsMember
  103. func (d *Dao) SIsMember(ctx context.Context, key, value string) (bool, error) {
  104. conn := d.redis.Get(ctx)
  105. defer conn.Close()
  106. return redis.Bool(conn.Do("SISMEMBER", key, value))
  107. }
  108. //SAdd SAdd
  109. func (d *Dao) SAdd(ctx context.Context, key, value string) (int, error) {
  110. conn := d.redis.Get(ctx)
  111. defer conn.Close()
  112. return redis.Int(conn.Do("SADD", key, value))
  113. }
  114. // Expire Expire
  115. func (d *Dao) Expire(ctx context.Context, key string, ttl int) (bool, error) {
  116. conn := d.redis.Get(ctx)
  117. defer conn.Close()
  118. return redis.Bool(conn.Do("EXPIRE", key, ttl))
  119. }
  120. //ErrEmptyMap ErrEmptyMap
  121. var ErrEmptyMap = stderr.New("empty map")
  122. // HGetAll HGetAll
  123. func (d *Dao) HGetAll(ctx context.Context, key string) (map[string]string, error) {
  124. conn := d.redis.Get(ctx)
  125. defer conn.Close()
  126. m, err := redis.StringMap(conn.Do("HGETALL", key))
  127. if err != nil {
  128. return nil, err
  129. }
  130. if len(m) <= 0 {
  131. return nil, ErrEmptyMap
  132. }
  133. return m, nil
  134. }
  135. // SetWithNxEx SetWithNxEx
  136. func (d *Dao) SetWithNxEx(ctx context.Context, key, value string, ex int64) (string, error) {
  137. conn := d.redis.Get(ctx)
  138. defer conn.Close()
  139. return redis.String(conn.Do("SET", key, value, "EX", ex, "NX"))
  140. }