mycommon/myredis/redis.go

184 lines
3.5 KiB
Go
Raw Normal View History

2023-03-20 15:50:45 +08:00
package myredis
2023-01-15 00:21:08 +08:00
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
jsoniter "github.com/json-iterator/go"
"time"
)
2023-03-20 20:41:36 +08:00
const DefaultKey = "default"
2023-01-15 00:21:08 +08:00
var (
DefaultConfig = &Config{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
PoolSize: 16,
MinIdleConn: 4,
MaxConnAge: "1h",
IdleTimeout: "10m",
}
2023-03-20 20:41:36 +08:00
instanceMap = make(map[string]*MyRedis)
2023-01-15 00:21:08 +08:00
)
type (
MyRedis struct {
2023-03-20 20:53:18 +08:00
*redis.Client
ctx context.Context
2023-01-15 00:21:08 +08:00
}
Config struct {
Addr string
Password string
DB int
PoolSize int
MinIdleConn int
MaxConnAge string
IdleTimeout string
}
)
2023-03-20 20:41:36 +08:00
func Instance(key ...string) *MyRedis {
var key0 string
if len(key) > 0 {
key0 = key[0]
} else {
key0 = DefaultKey
}
instance, ok := instanceMap[key0]
if !ok {
panic(fmt.Errorf("%s not config", key))
}
return instance
}
func NewDefault(config *Config) (*MyRedis, error) {
return New(DefaultKey, config)
}
func New(key string, config *Config) (*MyRedis, error) {
2023-01-15 00:21:08 +08:00
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
2023-03-20 20:53:18 +08:00
rd.Client = client
ping := rd.Client.Ping(ctx)
2023-01-15 00:21:08 +08:00
if ping.Err() != nil {
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
}
2023-03-20 20:41:36 +08:00
instanceMap[key] = rd
2023-01-15 00:21:08 +08:00
return rd, nil
}
2023-03-20 20:53:18 +08:00
// GetSimple 通用get
func (r *MyRedis) GetSimple(key string) (string, error) {
return r.Client.Get(r.ctx, key).Result()
2023-01-15 00:21:08 +08:00
}
2023-03-20 20:53:18 +08:00
// SetSimple 通用set
func (r *MyRedis) SetSimple(key string, value interface{}, t ...time.Duration) (string, error) {
2023-01-15 00:21:08 +08:00
var t2 time.Duration
if len(t) > 0 {
t2 = t[0]
}
2023-03-20 20:53:18 +08:00
return r.Client.Set(r.ctx, key, value, t2).Result()
2023-01-15 00:21:08 +08:00
}
// GetJson json序列化
func (r *MyRedis) GetJson(key string) (interface{}, error) {
2023-03-20 20:53:18 +08:00
res := r.Client.Get(r.ctx, key)
2023-01-15 00:21:08 +08:00
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)
}
2023-03-20 20:53:18 +08:00
return r.Client.Set(r.ctx, key, v, t2).Result()
2023-01-15 00:21:08 +08:00
}
func (r *MyRedis) Close() {
2023-03-20 20:53:18 +08:00
if r.Client != nil {
r.Client.Close()
2023-01-15 00:21:08 +08:00
}
}
2023-03-20 20:41:36 +08:00
2023-03-20 20:53:18 +08:00
func (r *MyRedis) GetConn() *redis.Client {
return r.Client
}
func Close(key string) {
Instance(key).Client.Close()
}
func CloseAll() {
for _, v := range instanceMap {
v.Client.Close()
}
2023-03-20 20:41:36 +08:00
}