145 lines
2.9 KiB
Go
145 lines
2.9 KiB
Go
|
package redis
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
jsoniter "github.com/json-iterator/go"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
DefaultConfig = &Config{
|
||
|
Addr: "127.0.0.1:6379",
|
||
|
Password: "",
|
||
|
DB: 0,
|
||
|
PoolSize: 16,
|
||
|
MinIdleConn: 4,
|
||
|
MaxConnAge: "1h",
|
||
|
IdleTimeout: "10m",
|
||
|
}
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
MyRedis struct {
|
||
|
Redis *redis.Client
|
||
|
ctx context.Context
|
||
|
}
|
||
|
|
||
|
Config struct {
|
||
|
Addr string
|
||
|
Password string
|
||
|
DB int
|
||
|
PoolSize int
|
||
|
MinIdleConn int
|
||
|
MaxConnAge string
|
||
|
IdleTimeout string
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func New(config *Config) (*MyRedis, error) {
|
||
|
var (
|
||
|
maxConnAge, _ = time.ParseDuration(DefaultConfig.MaxConnAge)
|
||
|
idleTimeout, _ = time.ParseDuration(DefaultConfig.IdleTimeout)
|
||
|
)
|
||
|
|
||
|
if config.PoolSize <= 0 {
|
||
|
config.MinIdleConn = DefaultConfig.PoolSize
|
||
|
}
|
||
|
|
||
|
if config.MinIdleConn <= 0 {
|
||
|
config.MinIdleConn = DefaultConfig.MinIdleConn
|
||
|
}
|
||
|
|
||
|
if config.MaxConnAge != "" {
|
||
|
t, err := time.ParseDuration(config.MaxConnAge)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("parse MaxConnAge err: %s\n", err)
|
||
|
|
||
|
}
|
||
|
maxConnAge = t
|
||
|
}
|
||
|
|
||
|
if config.IdleTimeout != "" {
|
||
|
t, err := time.ParseDuration(config.IdleTimeout)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("parse IdleTimeout err: %s\n", err)
|
||
|
|
||
|
}
|
||
|
idleTimeout = t
|
||
|
}
|
||
|
|
||
|
client := redis.NewClient(&redis.Options{
|
||
|
Addr: config.Addr,
|
||
|
Password: config.Password,
|
||
|
DB: config.DB,
|
||
|
PoolSize: config.PoolSize,
|
||
|
MinIdleConns: config.MinIdleConn,
|
||
|
MaxConnAge: maxConnAge,
|
||
|
IdleTimeout: idleTimeout,
|
||
|
})
|
||
|
|
||
|
ctx := context.Background()
|
||
|
rd := &MyRedis{}
|
||
|
rd.ctx = ctx
|
||
|
rd.Redis = client
|
||
|
ping := rd.Redis.Ping(ctx)
|
||
|
if ping.Err() != nil {
|
||
|
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
|
||
|
}
|
||
|
|
||
|
return rd, nil
|
||
|
}
|
||
|
|
||
|
// Get 通用get
|
||
|
func (r *MyRedis) Get(key string) (string, error) {
|
||
|
return r.Redis.Get(r.ctx, key).Result()
|
||
|
}
|
||
|
|
||
|
// Set 通用set
|
||
|
func (r *MyRedis) Set(key string, value interface{}, t ...time.Duration) (string, error) {
|
||
|
var t2 time.Duration
|
||
|
if len(t) > 0 {
|
||
|
t2 = t[0]
|
||
|
}
|
||
|
return r.Redis.Set(r.ctx, key, value, t2).Result()
|
||
|
}
|
||
|
|
||
|
// GetJson json序列化
|
||
|
func (r *MyRedis) GetJson(key string) (interface{}, error) {
|
||
|
res := r.Redis.Get(r.ctx, key)
|
||
|
if res.Err() != nil {
|
||
|
return nil, res.Err()
|
||
|
}
|
||
|
|
||
|
b, err := res.Bytes()
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("get key:%s 反序列化json失败(-1)", key)
|
||
|
}
|
||
|
var result interface{}
|
||
|
err = jsoniter.Unmarshal(b, &result)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("get key:%s 反序列化json失败(-2)", key)
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// SetJson json序列化set
|
||
|
func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (string, error) {
|
||
|
var t2 time.Duration
|
||
|
if len(t) > 0 {
|
||
|
t2 = t[0]
|
||
|
}
|
||
|
v, err := jsoniter.Marshal(value)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("set key:%s 序列化json失败", key)
|
||
|
}
|
||
|
return r.Redis.Set(r.ctx, key, v, t2).Result()
|
||
|
}
|
||
|
|
||
|
func (r *MyRedis) Close() {
|
||
|
if r.Redis != nil {
|
||
|
r.Redis.Close()
|
||
|
}
|
||
|
}
|