update
parent
275652416b
commit
7c499ad86a
|
@ -56,11 +56,11 @@ func Instance(key ...string) *MyRedis {
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefault(config *Config) (*MyRedis, error) {
|
func InitDefault(config *Config) error {
|
||||||
return New(DefaultKey, config)
|
return Init(DefaultKey, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(key string, config *Config) (*MyRedis, error) {
|
func Init(key string, config *Config) error {
|
||||||
var (
|
var (
|
||||||
maxConnAge, _ = time.ParseDuration(DefaultConfig.MaxConnAge)
|
maxConnAge, _ = time.ParseDuration(DefaultConfig.MaxConnAge)
|
||||||
idleTimeout, _ = time.ParseDuration(DefaultConfig.IdleTimeout)
|
idleTimeout, _ = time.ParseDuration(DefaultConfig.IdleTimeout)
|
||||||
|
@ -77,7 +77,7 @@ func New(key string, config *Config) (*MyRedis, error) {
|
||||||
if config.MaxConnAge != "" {
|
if config.MaxConnAge != "" {
|
||||||
t, err := time.ParseDuration(config.MaxConnAge)
|
t, err := time.ParseDuration(config.MaxConnAge)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parse MaxConnAge err: %s\n", err)
|
return fmt.Errorf("parse MaxConnAge err: %s\n", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
maxConnAge = t
|
maxConnAge = t
|
||||||
|
@ -86,7 +86,7 @@ func New(key string, config *Config) (*MyRedis, error) {
|
||||||
if config.IdleTimeout != "" {
|
if config.IdleTimeout != "" {
|
||||||
t, err := time.ParseDuration(config.IdleTimeout)
|
t, err := time.ParseDuration(config.IdleTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parse IdleTimeout err: %s\n", err)
|
return fmt.Errorf("parse IdleTimeout err: %s\n", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
idleTimeout = t
|
idleTimeout = t
|
||||||
|
@ -107,11 +107,11 @@ func New(key string, config *Config) (*MyRedis, error) {
|
||||||
rd.Client = client
|
rd.Client = client
|
||||||
ping := rd.Client.Ping(ctx)
|
ping := rd.Client.Ping(ctx)
|
||||||
if ping.Err() != nil {
|
if ping.Err() != nil {
|
||||||
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
|
return fmt.Errorf("connet redis err: %s", ping.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceMap[key] = rd
|
instanceMap[key] = rd
|
||||||
return rd, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSimple 通用get
|
// GetSimple 通用get
|
||||||
|
@ -165,16 +165,6 @@ func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (st
|
||||||
return r.Client.Set(ctx, key, v, t2).Result()
|
return r.Client.Set(ctx, key, v, t2).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *MyRedis) Close() {
|
|
||||||
if r.Client != nil {
|
|
||||||
r.Client.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *MyRedis) GetConn() *redis.Client {
|
|
||||||
return r.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func Close(key string) {
|
func Close(key string) {
|
||||||
Instance(key).Client.Close()
|
Instance(key).Client.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRedis(t *testing.T) {
|
func TestRedis(t *testing.T) {
|
||||||
redis, err := NewDefault(&Config{
|
err := InitDefault(&Config{
|
||||||
Addr: "127.0.0.1:6379",
|
Addr: "192.168.244.128:6379",
|
||||||
Password: "",
|
Password: "",
|
||||||
DB: 15,
|
DB: 15,
|
||||||
PoolSize: 16,
|
PoolSize: 16,
|
||||||
|
@ -21,16 +21,16 @@ func TestRedis(t *testing.T) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer redis.Close()
|
defer CloseAll()
|
||||||
|
|
||||||
set, err := redis.Set(context.Background(), "name", "qwe123", time.Minute).Result()
|
set, err := Instance().Set(context.Background(), "name", "qwe123", time.Minute).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(set)
|
fmt.Println(set)
|
||||||
|
|
||||||
get, err := redis.Get(context.Background(), "name").Result()
|
get, err := Instance().Get(context.Background(), "name").Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue