package myredis import ( "context" "fmt" "github.com/go-redis/redis/v8" jsoniter "github.com/json-iterator/go" "time" ) const DefaultKey = "default" var ( DefaultConfig = &Config{ Addr: "127.0.0.1:6379", Password: "", DB: 0, PoolSize: 16, MinIdleConn: 4, MaxConnAge: "1h", IdleTimeout: "10m", } instanceMap = make(map[string]*MyRedis) ) 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 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) { 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()) } instanceMap[key] = rd 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() } } func (r *MyRedis) Redis() *redis.Client { return r.redis }