redis.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "eta/eta_api/utils/redis"
  4. client "github.com/go-redis/redis/v8"
  5. "time"
  6. )
  7. type RedisClient interface {
  8. Get(key string) interface{}
  9. GetStr(key string) string
  10. GetUInt64(key string) (uint64, error)
  11. RedisBytes(key string) (data []byte, err error)
  12. RedisString(key string) (data string, err error)
  13. RedisInt(key string) (data int, err error)
  14. Put(key string, val interface{}, timeout time.Duration) error
  15. SetNX(key string, val interface{}, timeout time.Duration) bool
  16. Delete(key string) error
  17. IsExist(key string) bool
  18. LPush(key string, val interface{}) error
  19. Brpop(key string, callback func([]byte))
  20. BrpopWithTimeout(key string, timeout time.Duration, callback func([]byte)) error
  21. LLen(key string) (int64, error)
  22. GetRedisTTL(key string) time.Duration
  23. Incrby(key string, num int) (interface{}, error)
  24. Do(commandName string, args ...interface{}) (reply interface{}, err error)
  25. SAdd(key string, args ...interface{}) (err error)
  26. SRem(key string, args ...interface{}) (err error)
  27. SIsMember(key string, args interface{}) (bool, error)
  28. ZAdd(key string, members ...*redis.Zset) error
  29. ZRangeWithScores(key string) ([]*redis.Zset, error)
  30. Expire(key string, duration time.Duration) error
  31. RedisClient() client.UniversalClient
  32. Keys(pattern string) ([]string, error)
  33. }
  34. func initRedis(redisType string, conf string) (redisClient RedisClient, err error) {
  35. switch redisType {
  36. case "cluster": // 集群
  37. redisClient, err = redis.InitClusterRedis(conf)
  38. default: // 默认走单机
  39. redisClient, err = redis.InitStandaloneRedis(conf)
  40. }
  41. return
  42. }
  43. // redis没有key的错误
  44. const RedisNoKeyErr = "redis: nil"