2 Star 0 Fork 0

TeamsHub/backend-gopkg

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
redis.go 1.59 KB
Copy Edit Raw Blame History
wuzheng0709 authored 2024-08-04 19:14 +08:00 . fix mod
package redis
import (
redis "github.com/redis/go-redis/v9"
"time"
)
type Client struct {
redis.Cmdable
}
type Config struct {
PoolSize int `yaml:"poolSize"`
Addr []string `yaml:"addr"`
Pwd string `yaml:"pwd"`
DialTimeout time.Duration `yaml:"DialTimeout"`
ReadTimeout time.Duration `yaml:"ReadTimeout"`
WriteTimeout time.Duration `yaml:"WriteTimeout"`
}
func NewRedis(o *Config) (client *Client, err error) {
var redisCli redis.Cmdable
if len(o.Addr) > 1 {
redisCli = redis.NewClusterClient(
&redis.ClusterOptions{
Addrs: o.Addr,
PoolSize: o.PoolSize,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
Password: o.Pwd,
},
)
} else {
redisCli = redis.NewClient(
&redis.Options{
Addr: o.Addr[0],
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
Password: o.Pwd,
PoolSize: o.PoolSize,
DB: 0,
},
)
}
err = redisCli.Ping(ctx).Err()
if nil != err {
panic(err)
}
client = new(Client)
client.Cmdable = redisCli
return client, nil
}
func (c *Client) Process(cmd redis.Cmder) error {
switch redisCli := c.Cmdable.(type) {
case *redis.ClusterClient:
return redisCli.Process(ctx, cmd)
case *redis.Client:
return redisCli.Process(ctx, cmd)
default:
return nil
}
}
func (c *Client) Close() error {
switch redisCli := c.Cmdable.(type) {
case *redis.ClusterClient:
return redisCli.Close()
case *redis.Client:
return redisCli.Close()
default:
return nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.5.24

Search