代码拉取完成,页面将自动刷新
package component
import (
"bytes"
"context"
"fmt"
"gitee.com/kzangv/gsf-fof/component/define"
"gitee.com/kzangv/gsf-fof/logger"
"github.com/go-redis/redis/v8"
"github.com/urfave/cli/v2"
"net/url"
"strconv"
"strings"
"time"
)
type RedisClient struct {
DB *redis.Client
Cfg define.EntityRedis
ref *Redis
}
func (c *RedisClient) Ref() *Redis {
return c.ref
}
func (c *RedisClient) CliFlags(name string) []cli.Flag {
return []cli.Flag{
&cli.IntFlag{
Name: fmt.Sprintf("redis-%s-open-limit", name),
Usage: fmt.Sprintf("redis(%s) max open limit", name),
Action: func(_ *cli.Context, v int) error { c.Cfg.PoolSize = v; return nil },
},
&cli.StringFlag{
Name: fmt.Sprintf("redis-%s-dsn", name),
Usage: fmt.Sprintf("redis(%s) DSN (format: `user:{{password}}@{{ip}}:{{port}}/{{db}}`)", name),
Action: func(_ *cli.Context, dsn string) error {
pv, err := url.Parse("redis://" + dsn)
if err == nil {
c.Cfg.Addr = pv.Host
c.Cfg.Pwd, _ = pv.User.Password()
c.Cfg.DB, _ = strconv.Atoi(strings.Trim(pv.Path, "/"))
}
if err != nil {
return fmt.Errorf("redis DSN is invalid(%s)[url parse faild err_msg:%s]", dsn, err.Error())
}
return nil
},
},
}
}
func (c *RedisClient) Init(env int, cc *Redis) {
c.ref = cc
}
func (c *RedisClient) Config() *define.EntityRedis {
return &c.Cfg
}
func (c *RedisClient) Load(name string) error {
c.DB = redis.NewClient(&redis.Options{
Addr: c.Cfg.Addr,
Password: c.Cfg.Pwd,
DB: c.Cfg.DB,
PoolSize: c.Cfg.PoolSize,
MinIdleConns: c.Cfg.PoolSize/3 + 1,
IdleTimeout: time.Second * time.Duration(c.ref.Cfg.IdleCheckTime),
ReadTimeout: time.Second * time.Duration(c.ref.Cfg.MaxReadTime),
WriteTimeout: time.Second * time.Duration(c.ref.Cfg.MaxWriteTime),
})
if _, err := c.DB.Ping(context.Background()).Result(); err != nil {
c.DB = nil
return fmt.Errorf("Load Redis (%s) Failed (Ping-Error: %s) ", name, err.Error())
}
return nil
}
func (c *RedisClient) ApiLog(args [][2]interface{}) func() {
bTm := time.Now()
return func() {
spendTime := time.Now().Sub(bTm) / time.Millisecond
var handler func(string, ...interface{})
if spendTime > time.Duration(c.ref.Cfg.SlowThreshold) { // 查询超过阈值
handler = c.ref.Log.WarnForce
} else if c.ref.Log.Level() == logger.Info {
handler = c.ref.Log.InfoForce
}
if handler != nil {
var format bytes.Buffer
format.WriteString("Redis[%d ms]: ")
fArgs := make([]interface{}, 0, 2+len(args)*2)
fArgs = append(fArgs, spendTime)
for i, l := 0, len(args); i < l; i++ {
format.WriteString("\n\t%s: %+v")
fArgs = append(fArgs, args[0][0], args[0][1])
}
handler(format.String(), fArgs...)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。