mycommon/myredis/redis.go

181 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,
2024-06-01 18:17:41 +08:00
MaxConnAge: "4h",
IdleTimeout: "15m",
2023-01-15 00:21:08 +08:00
}
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
2023-01-15 00:21:08 +08:00
}
Config struct {
Addr string
Password string
DB int
PoolSize int
MinIdleConn int
MaxConnAge string
IdleTimeout string
}
)
2024-06-01 18:17:41 +08:00
func DB(key ...string) *MyRedis {
2023-03-20 20:41:36 +08:00
var key0 string
if len(key) > 0 {
key0 = key[0]
} else {
key0 = DefaultKey
}
instance, ok := instanceMap[key0]
if !ok {
2023-05-17 19:46:45 +08:00
panic(fmt.Errorf("redis %s not config", key0))
2023-03-20 20:41:36 +08:00
}
return instance
}
2023-05-17 19:56:42 +08:00
func InitDefault(config *Config) error {
return Init(DefaultKey, config)
2023-03-20 20:41:36 +08:00
}
2023-05-17 19:56:42 +08:00
func Init(key string, config *Config) error {
2023-05-18 17:52:26 +08:00
rd, err := New(config)
if err != nil {
return err
}
instanceMap[key] = rd
return nil
}
func New(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 {
2023-05-18 17:52:26 +08:00
return nil, fmt.Errorf("parse MaxConnAge err: %s\n", err)
2023-01-15 00:21:08 +08:00
}
maxConnAge = t
}
if config.IdleTimeout != "" {
t, err := time.ParseDuration(config.IdleTimeout)
if err != nil {
2023-05-18 17:52:26 +08:00
return nil, fmt.Errorf("parse IdleTimeout err: %s\n", err)
2023-01-15 00:21:08 +08:00
}
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,
})
2023-05-24 21:48:23 +08:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
2023-01-15 00:21:08 +08:00
rd := &MyRedis{}
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 {
2023-05-18 17:52:26 +08:00
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
2023-01-15 00:21:08 +08:00
}
2023-05-18 17:52:26 +08:00
return rd, nil
2023-01-15 00:21:08 +08:00
}
2023-03-20 20:53:18 +08:00
// GetSimple 通用get
func (r *MyRedis) GetSimple(key string) (string, error) {
2023-05-17 19:14:10 +08:00
ctx := context.Background()
return r.Client.Get(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-05-17 19:14:10 +08:00
ctx := context.Background()
2023-01-15 00:21:08 +08:00
var t2 time.Duration
if len(t) > 0 {
t2 = t[0]
}
2023-05-17 19:14:10 +08:00
return r.Client.Set(ctx, key, value, t2).Result()
2023-01-15 00:21:08 +08:00
}
// GetJson json序列化
2023-05-24 22:06:29 +08:00
func (r *MyRedis) GetJson(key string, result interface{}) error {
2023-05-17 19:14:10 +08:00
ctx := context.Background()
2023-05-24 22:06:29 +08:00
res, err := r.Client.Get(ctx, key).Bytes()
2023-01-15 00:21:08 +08:00
if err != nil {
2023-05-24 22:06:29 +08:00
return err
2023-01-15 00:21:08 +08:00
}
2023-05-24 22:06:29 +08:00
err = jsoniter.Unmarshal(res, &result)
2023-01-15 00:21:08 +08:00
if err != nil {
2023-05-24 22:06:29 +08:00
return fmt.Errorf("get key:%s 反序列化json失败(-2)", key)
2023-01-15 00:21:08 +08:00
}
2023-05-24 22:06:29 +08:00
return nil
2023-01-15 00:21:08 +08:00
}
// SetJson json序列化set
func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (string, error) {
2023-05-17 19:14:10 +08:00
ctx := context.Background()
2023-01-15 00:21:08 +08:00
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-05-17 19:14:10 +08:00
return r.Client.Set(ctx, key, v, t2).Result()
2023-01-15 00:21:08 +08:00
}
2024-06-01 18:28:09 +08:00
func CloseDB(key string) {
2024-06-01 18:17:41 +08:00
DB(key).Client.Close()
2023-03-20 20:53:18 +08:00
}
2024-06-01 18:28:09 +08:00
func CloseAllDB() {
2023-03-20 20:53:18 +08:00
for _, v := range instanceMap {
v.Client.Close()
}
2023-03-20 20:41:36 +08:00
}