-
Notifications
You must be signed in to change notification settings - Fork 6
/
xredis.go
76 lines (68 loc) · 2.32 KB
/
xredis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*===============================================================
* Copyright (C) 2020 All rights reserved.
*
* FileName:xredis.go
* Author:WuGuoFu
* Date: 2020-03-19
* Description:
*
================================================================*/
package xredis
import (
"context"
redo "github.com/tal-tech/xredis/handler"
"github.com/tal-tech/xredis/internal/core"
"github.com/tal-tech/xredis/internal/handlers"
"github.com/go-redis/redis"
)
// NewXesRedis returns a client to the Redis Server specified by default options.
func NewXesRedis() *core.XesRedis {
instance_XesRedis := new(core.XesRedis)
handler := &handlers.DefaultHandler{}
instance_XesRedis.SetHandler(handler)
return instance_XesRedis
}
// NewXesRedisOfCtx returns a client to the Redis Server that you can Specify handler.
func NewXesRedisOfCtx(ctx context.Context) *core.XesRedis {
instance_XesRedis := new(core.XesRedis)
instance_XesRedis.SetCtx(ctx)
if hdler, ok := ctx.Value("handler").(func() redo.XesRedisHandler); ok {
instance_XesRedis.SetHandler(hdler())
} else {
handler := &handlers.DefaultHandler{}
instance_XesRedis.SetHandler(handler)
}
return instance_XesRedis
}
//NewSimpleXesRedis returns a client to the Redis Server specified by instance.
func NewSimpleXesRedis(ctx context.Context, instance string) *core.XesRedis {
instance_XesRedis := new(core.XesRedis)
instance_XesRedis.SetCtx(ctx)
handler := &handlers.SimpleHandler{}
handler.SetInstance(instance)
instance_XesRedis.SetHandler(handler)
return instance_XesRedis
}
//NewShardingXesRedis returns a client to the Redis Server specified by cluster Name.
func NewShardingXesRedis(ctx context.Context, cluster string) *core.XesRedis {
instance_XesRedis := new(core.XesRedis)
instance_XesRedis.SetCtx(ctx)
handler := &handlers.ShardingHandler{}
handler.SetCluster(cluster)
instance_XesRedis.SetHandler(handler)
return instance_XesRedis
}
//GetClient return the client of the original reference package "go-redis".
func GetClient(key string) *redis.Client {
selectors := core.GetSelectors()
selectorIns, ok := selectors[key]
if !ok {
return nil
}
server := selectorIns.Select()
return core.GetRedisClient(server)
}
//AddRedis can set redis option without reading config file
func AddRedis(key, addr string, options redis.Options) {
core.AddRedis(key, addr, options)
}