-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
gredis_redis.go
137 lines (122 loc) · 3.88 KB
/
gredis_redis.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gredis
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
)
// Redis client.
type Redis struct {
config *Config
localAdapter
localGroup
}
type (
localGroup struct {
localGroupGeneric
localGroupHash
localGroupList
localGroupPubSub
localGroupScript
localGroupSet
localGroupSortedSet
localGroupString
}
localAdapter = Adapter
localGroupGeneric = IGroupGeneric
localGroupHash = IGroupHash
localGroupList = IGroupList
localGroupPubSub = IGroupPubSub
localGroupScript = IGroupScript
localGroupSet = IGroupSet
localGroupSortedSet = IGroupSortedSet
localGroupString = IGroupString
)
const (
errorNilRedis = `the Redis object is nil`
)
var (
errorNilAdapter = gstr.Trim(gstr.Replace(`
redis adapter is not set, missing configuration or adapter register?
possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis
`, "\n", ""))
)
// initGroup initializes the group object of redis.
func (r *Redis) initGroup() *Redis {
r.localGroup = localGroup{
localGroupGeneric: r.localAdapter.GroupGeneric(),
localGroupHash: r.localAdapter.GroupHash(),
localGroupList: r.localAdapter.GroupList(),
localGroupPubSub: r.localAdapter.GroupPubSub(),
localGroupScript: r.localAdapter.GroupScript(),
localGroupSet: r.localAdapter.GroupSet(),
localGroupSortedSet: r.localAdapter.GroupSortedSet(),
localGroupString: r.localAdapter.GroupString(),
}
return r
}
// SetAdapter changes the underlying adapter with custom adapter for current redis client.
func (r *Redis) SetAdapter(adapter Adapter) {
if r == nil {
panic(gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis))
}
r.localAdapter = adapter
}
// GetAdapter returns the adapter that is set in current redis client.
func (r *Redis) GetAdapter() Adapter {
if r == nil {
return nil
}
return r.localAdapter
}
// Conn retrieves and returns a connection object for continuous operations.
// Note that you should call Close function manually if you do not use this connection any further.
func (r *Redis) Conn(ctx context.Context) (Conn, error) {
if r == nil {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
}
if r.localAdapter == nil {
return nil, gerror.NewCode(gcode.CodeNecessaryPackageNotImport, errorNilAdapter)
}
return r.localAdapter.Conn(ctx)
}
// Do send a command to the server and returns the received reply.
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
if r == nil {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
}
if r.localAdapter == nil {
return nil, gerror.NewCodef(gcode.CodeMissingConfiguration, errorNilAdapter)
}
return r.localAdapter.Do(ctx, command, args...)
}
// MustConn performs as function Conn, but it panics if any error occurs internally.
func (r *Redis) MustConn(ctx context.Context) Conn {
c, err := r.Conn(ctx)
if err != nil {
panic(err)
}
return c
}
// MustDo performs as function Do, but it panics if any error occurs internally.
func (r *Redis) MustDo(ctx context.Context, command string, args ...interface{}) *gvar.Var {
v, err := r.Do(ctx, command, args...)
if err != nil {
panic(err)
}
return v
}
// Close closes current redis client, closes its connection pool and releases all its related resources.
func (r *Redis) Close(ctx context.Context) error {
if r == nil || r.localAdapter == nil {
return nil
}
return r.localAdapter.Close(ctx)
}