mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
update
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charlienet/go-mixed/expr"
|
||||
@ -13,16 +15,13 @@ const (
|
||||
|
||||
blockingQueryTimeout = 5 * time.Second
|
||||
readWriteTimeout = 2 * time.Second
|
||||
defaultSlowThreshold = time.Millisecond * 100 // 慢查询
|
||||
defaultSlowThreshold = "5000" // 慢查询(单位微秒)
|
||||
)
|
||||
|
||||
var Nil = redis.Nil
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type ReidsOption struct {
|
||||
Addr string
|
||||
Addrs []string
|
||||
Password string // 密码
|
||||
Prefix string
|
||||
@ -41,6 +40,9 @@ type ReidsOption struct {
|
||||
WriteTimeout time.Duration
|
||||
ContextTimeoutEnabled bool
|
||||
|
||||
// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
|
||||
PoolFIFO bool
|
||||
|
||||
PoolSize int
|
||||
PoolTimeout time.Duration
|
||||
MinIdleConns int
|
||||
@ -49,14 +51,36 @@ type ReidsOption struct {
|
||||
ConnMaxLifetime time.Duration
|
||||
}
|
||||
|
||||
var _ Client = redisClient{}
|
||||
|
||||
type Client interface {
|
||||
redis.UniversalClient
|
||||
Prefix() string
|
||||
Separator() string
|
||||
JoinKeys(keys ...string) string
|
||||
FormatKeys(keys ...string) []string
|
||||
}
|
||||
|
||||
func New(opt *ReidsOption) Client {
|
||||
var rdb redis.UniversalClient
|
||||
once.Do(func() {
|
||||
rdb = redis.NewUniversalClient(&redis.UniversalOptions{
|
||||
type redisClient struct {
|
||||
redis.UniversalClient
|
||||
prefix string
|
||||
separator string
|
||||
}
|
||||
|
||||
func New(opt *ReidsOption) redisClient {
|
||||
var rdb redisClient
|
||||
|
||||
if len(opt.Addrs) == 0 && len(opt.Addr) > 0 {
|
||||
opt.Addrs = []string{opt.Addr}
|
||||
}
|
||||
|
||||
separator := expr.Ternary(len(opt.Separator) == 0, defaultSeparator, opt.Separator)
|
||||
prefix := expr.Ternary(len(opt.Prefix) > 0, fmt.Sprintf("%s%s", opt.Prefix, separator), "")
|
||||
|
||||
rdb = redisClient{
|
||||
prefix: prefix,
|
||||
separator: separator,
|
||||
UniversalClient: redis.NewUniversalClient(&redis.UniversalOptions{
|
||||
Addrs: opt.Addrs,
|
||||
Password: opt.Password,
|
||||
|
||||
@ -77,15 +101,46 @@ func New(opt *ReidsOption) Client {
|
||||
MaxIdleConns: opt.MaxIdleConns,
|
||||
ConnMaxIdleTime: opt.ConnMaxIdleTime,
|
||||
ConnMaxLifetime: opt.ConnMaxLifetime,
|
||||
})
|
||||
})}
|
||||
|
||||
if len(opt.Prefix) > 0 {
|
||||
rdb.AddHook(renameKey{
|
||||
prefix: opt.Prefix,
|
||||
separator: expr.Ternary(len(opt.Separator) == 0, defaultSeparator, opt.Separator),
|
||||
})
|
||||
}
|
||||
})
|
||||
rdb.ConfigSet(context.Background(), "slowlog-log-slower-than", defaultSlowThreshold)
|
||||
|
||||
if len(opt.Prefix) > 0 {
|
||||
rdb.AddHook(renameKey{
|
||||
prefix: prefix,
|
||||
})
|
||||
}
|
||||
|
||||
return rdb
|
||||
}
|
||||
|
||||
func (rdb redisClient) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
|
||||
newKeys := rdb.FormatKeys(keys...)
|
||||
|
||||
return rdb.UniversalClient.Eval(ctx, script, newKeys, args...)
|
||||
}
|
||||
|
||||
func (rdb redisClient) Prefix() string {
|
||||
return rdb.prefix
|
||||
}
|
||||
|
||||
func (rdb redisClient) Separator() string {
|
||||
return rdb.separator
|
||||
}
|
||||
|
||||
func (rdb redisClient) JoinKeys(keys ...string) string {
|
||||
return strings.Join(keys, rdb.separator)
|
||||
}
|
||||
|
||||
func (rdb redisClient) FormatKeys(keys ...string) []string {
|
||||
if len(rdb.prefix) == 0 {
|
||||
return keys
|
||||
}
|
||||
|
||||
re := make([]string, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
re = append(re, fmt.Sprintf("%s%s", rdb.prefix, k))
|
||||
}
|
||||
|
||||
return re
|
||||
}
|
||||
|
Reference in New Issue
Block a user