redis.go 2.8 KB

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