1 Star 0 Fork 0

igo/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
redis.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
layte.xiao 提交于 2023-02-24 16:03 . redis支持keyPrefix3
package xredis
import (
"context"
"gitee.com/igolang/pkg/xredis/hooks"
"github.com/redis/go-redis/v9"
"time"
)
type RedisConfig struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int `json:"DB"`
DialTimeout int64 `json:"dialTimeout"`
ReadTimeout int64 `json:"readTimeout"`
WriteTimeout int64 `json:"writeTimeout"`
IdleTimeout int64 `json:"idleTimeout"` // 连接生效时间,默认5分钟
MaxRetries int `json:"maxRetries"`
PoolSize int `json:"poolSize"`
MinIdleConn int `json:"minIdleConn"`
KeyPrefix string `json:"keyPrefix"`
}
type Client struct {
*redis.Client
keyPrefix *hooks.KeyPrefix
}
func NewClient(config *RedisConfig) (client *Client, err error) {
options := &redis.Options{
Addr: config.Addr,
Password: config.Password,
DB: config.DB,
DialTimeout: time.Duration(config.DialTimeout) * time.Millisecond,
ReadTimeout: time.Duration(config.ReadTimeout) * time.Millisecond,
WriteTimeout: time.Duration(config.WriteTimeout) * time.Millisecond,
MaxRetries: config.MaxRetries,
PoolSize: config.PoolSize,
MinIdleConns: config.MinIdleConn,
}
rdb := redis.NewClient(options)
hk := hooks.NewKeyPrefix(config.KeyPrefix)
client = &Client{
Client: rdb,
keyPrefix: hk,
}
client.AddHook(hk)
return
}
func (c *Client) KeyPrefix() string {
return c.keyPrefix.Prefix()
}
func (c *Client) EnableKeyPrefix() {
c.keyPrefix.Enable()
}
func (c *Client) DisableKeyPrefix() {
c.keyPrefix.Disable()
}
type Logging interface {
Printf(ctx context.Context, format string, v ...interface{})
}
func SetLogger(logger Logging) {
redis.SetLogger(logger)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/igolang/pkg.git
git@gitee.com:igolang/pkg.git
igolang
pkg
pkg
v1.21.2

搜索帮助