redis.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "runtime"
  7. "strings"
  8. "time"
  9. "github.com/go-redis/redis/v8"
  10. )
  11. //"other=127.0.0.1:2203,push=127.0.0.1:2203,sso=172.17.4.83:1711"
  12. //"other=127.0.0.1:2203=0,push=127.0.0.1:2203,sso=172.17.4.83:1711"
  13. var (
  14. ctx = context.Background()
  15. RedisPool map[string]*redis.Client
  16. )
  17. type RedisUtil interface {
  18. Init(opt interface{})
  19. Put(key string, val interface{}) //单个存放
  20. // Get(key string) //单个获取
  21. // GetByPattern(key string) //根据正则获取
  22. // BulkPut(timeout int, obj ...interface{}) //批量存储
  23. // BulkGet(key ...string) //批量获取
  24. // Expire(key string, expire int) //设置过期
  25. // Exists(key string) //是否存在
  26. // FlushDB(dbnum ...int) //清空
  27. // Del(key ...interface{}) //删除key
  28. // DelByPattern(key ...string) //根据正则删除
  29. // Incr(key string) //自增
  30. // Decr(key string, val int) //自减
  31. // Pop(key string) //移除并获取某个key
  32. // LPOP(list string) //移出并获取列表的第一个元素
  33. // RPUSH(list string, val interface{}) //在列表中添加一个或多个值
  34. // LLEN(list string) //获取列表长度
  35. }
  36. func InitRedis(addrs string) {
  37. InitRedisBySize(addrs, 80, 10, 240)
  38. }
  39. func InitRedisBySize(addrs string, maxSize, maxIdle, timeout int) {
  40. RedisPool = map[string]*redis.Client{}
  41. addr := strings.Split(addrs, ",")
  42. for _, v := range addr {
  43. saddr := strings.Split(v, "=")
  44. RedisPool[saddr[0]] = redis.NewClient(&redis.Options{
  45. Addr: saddr[1],
  46. Password: "",
  47. DB: 0,
  48. PoolSize: maxSize,
  49. MinIdleConns: maxIdle,
  50. IdleTimeout: time.Duration(timeout) * time.Second,
  51. })
  52. }
  53. }
  54. //并存入字符串缓存
  55. func PutKV(key string, obj interface{}) bool {
  56. return Put("other", key, obj, -1)
  57. }
  58. func PutCKV(code, key string, obj interface{}) bool {
  59. return Put(code, key, obj, -1)
  60. }
  61. func Put(code, key string, obj interface{}, timeout int) bool {
  62. b := false
  63. defer catch()
  64. var err error
  65. _obj, _err := json.Marshal(obj)
  66. if _err != nil {
  67. log.Println("redisutil-SET-序列化出错Error", _err)
  68. return b
  69. }
  70. if timeout < 1 {
  71. _, err = RedisPool[code].Set(ctx, key, _obj, 0).Result()
  72. } else {
  73. _, err = RedisPool[code].SetEX(ctx, key, _obj, time.Duration(timeout)*time.Second).Result()
  74. }
  75. if nil != err {
  76. log.Println("redisutil-SETError-put", err)
  77. } else {
  78. b = true
  79. }
  80. return b
  81. }
  82. func catch() {
  83. if r := recover(); r != nil {
  84. log.Println(r)
  85. for skip := 0; ; skip++ {
  86. _, file, line, ok := runtime.Caller(skip)
  87. if !ok {
  88. break
  89. }
  90. go log.Printf("%v,%v\n", file, line)
  91. }
  92. }
  93. }