-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
220 lines (211 loc) · 6.15 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package builtin_libs
import (
"reflect"
"strings"
. "github.com/zgg-lang/zgg-go/runtime"
"github.com/gomodule/redigo/redis"
)
var (
redisClientClass ValueType
redisPipeSessionClass ValueType
)
func libRedis(*Context) ValueObject {
lib := NewObject()
lib.SetMember("open", NewNativeFunction("open", func(c *Context, this Value, args []Value) Value {
var (
redisAddr ValueStr
initDB ValueInt
)
EnsureFuncParams(c, "redis.open", args,
ArgRuleOptional{"addr", TypeStr, &redisAddr, NewStr("127.0.0.1:6379")},
ArgRuleOptional{"db", TypeInt, &initDB, NewInt(0)},
)
conn, err := redis.Dial("tcp", redisAddr.Value(), redis.DialDatabase(int(initDB.Value())))
if err != nil {
c.OnRuntimeError("redis.open error: %s", err)
return nil
}
return NewObjectAndInit(redisClientClass, c, NewGoValue(conn))
}), nil)
lib.SetMember("RedisClient", redisClientClass, nil)
lib.SetMember("RedisPipeSession", redisPipeSessionClass, nil)
return lib
}
type redisPipeCmd struct {
cmd string
args []interface{}
}
func initRedisPipeSessionClass() ValueType {
rv := NewClassBuilder("RedisPipeSession").
Constructor(func(c *Context, thisObj ValueObject, args []Value) {
thisObj.SetMember("_cmds", NewGoValue([]redisPipeCmd{}), c)
}).
Method("__getAttr__", func(c *Context, this ValueObject, args []Value) Value {
cmd := c.MustStr(args[0])
if cmd == strings.ToUpper(cmd) {
cmds := this.GetMember("_cmds", c).ToGoValue().([]redisPipeCmd)
session := this
return NewNativeFunction("", func(c *Context, this Value, args []Value) Value {
defer func() {
session.SetMember("_cmds", NewGoValue(cmds), c)
}()
cmdArgs := make([]interface{}, len(args))
for i, arg := range args {
cmdArgs[i] = arg.ToGoValue()
}
cmds = append(cmds, redisPipeCmd{cmd: cmd, args: cmdArgs})
return Undefined()
})
} else {
return Undefined()
}
}).
Build()
return rv
}
func initRedisClientClass() ValueType {
rv := NewClassBuilder("RedisClient").
Constructor(func(c *Context, thisObj ValueObject, args []Value) {
thisObj.SetMember("_conn", args[0], c)
}).
Method("close", func(c *Context, this ValueObject, args []Value) Value {
conn := this.GetMember("_conn", c).ToGoValue().(redis.Conn)
if err := conn.Close(); err != nil {
c.OnRuntimeError("RedisClient.close fail on close: %s", err)
}
return Undefined()
}).
Method("exec", func(c *Context, this ValueObject, args []Value) Value {
if len(args) < 1 {
c.OnRuntimeError("RedisClient.exec requires at least 1 argument")
return nil
}
conn := this.GetMember("_conn", c).ToGoValue().(redis.Conn)
if cmds, ok := args[0].(ValueArray); ok {
n := cmds.Len()
for i := 0; i < n; i++ {
var (
cmdName string
cmdArgs = []interface{}{}
)
cmd := cmds.GetIndex(i, c)
if cmdItems, ok := cmd.(ValueArray); ok {
if n2 := cmdItems.Len(); n2 > 0 {
cmdName = cmdItems.GetIndex(0, c).ToString(c)
cmdArgs = make([]interface{}, 0, n2-1)
for i := 1; i < n2; i++ {
cmdArgs = append(cmdArgs, cmdItems.GetIndex(i, c).ToGoValue())
}
} else {
cmdName = "PING"
cmdArgs = []interface{}{}
}
} else {
cmdName = cmd.ToString(c)
}
if err := conn.Send(cmdName, cmdArgs...); err != nil {
c.OnRuntimeError("redis.exec: send piped command error: %s", err)
}
}
if err := conn.Flush(); err != nil {
c.OnRuntimeError("redis.exec: flush piped command error: %s", err)
}
rv := NewArray(n)
for i := 0; i < n; i++ {
rsp, err := conn.Receive()
if err != nil {
c.OnRuntimeError("redis.exec: receive piped command error: %s", err)
}
if rsp == nil {
rv.PushBack(Nil())
} else {
v := reflect.ValueOf(rsp)
rv.PushBack(FromGoValue(v, c))
}
}
return rv
} else {
cmd := c.MustStr(args[0], "command")
cmdArgs := make([]interface{}, len(args)-1)
for i, arg := range args[1:] {
cmdArgs[i] = arg.ToGoValue()
}
rsp, err := conn.Do(cmd, cmdArgs...)
if err != nil {
if err == redis.ErrNil {
return Nil()
}
c.OnRuntimeError("RedisClient.exec error %s", err)
return nil
}
if rsp == nil {
return Nil()
}
v := reflect.ValueOf(rsp)
return FromGoValue(v, c)
}
}).
Method("pipe", func(c *Context, this ValueObject, args []Value) Value {
action := c.MustCallable(args[0])
session := NewObjectAndInit(redisPipeSessionClass, c)
c.Invoke(action, nil, Args(session))
cmds := session.GetMember("_cmds", c).ToGoValue().([]redisPipeCmd)
conn := this.GetMember("_conn", c).ToGoValue().(redis.Conn)
for _, cmd := range cmds {
if err := conn.Send(cmd.cmd, cmd.args...); err != nil {
c.OnRuntimeError("redis.pipe: send piped command error: %s", err)
}
}
if err := conn.Flush(); err != nil {
c.OnRuntimeError("redis.pipe: flush piped command error: %s", err)
}
n := len(cmds)
rv := NewArray(n)
for i := 0; i < n; i++ {
rsp, err := conn.Receive()
if err != nil {
c.OnRuntimeError("redis.exec: receive piped command error: %s", err)
}
if rsp == nil {
rv.PushBack(Nil())
} else {
v := reflect.ValueOf(rsp)
rv.PushBack(FromGoValue(v, c))
}
}
return rv
}).
Method("__getAttr__", func(c *Context, this ValueObject, args []Value) Value {
cmd := c.MustStr(args[0])
if cmd == strings.ToUpper(cmd) {
conn := this.GetMember("_conn", c).ToGoValue().(redis.Conn)
return NewNativeFunction("", func(c *Context, this Value, args []Value) Value {
cmdArgs := make([]interface{}, len(args))
for i, arg := range args {
cmdArgs[i] = arg.ToGoValue()
}
rsp, err := conn.Do(cmd, cmdArgs...)
if err != nil {
if err == redis.ErrNil {
return Nil()
}
c.OnRuntimeError("RedisClient.exec error %s", err)
return nil
}
if rsp == nil {
return Nil()
}
v := reflect.ValueOf(rsp)
return FromGoValue(v, c)
})
} else {
return Undefined()
}
}).
Build()
return rv
}
func init() {
redisClientClass = initRedisClientClass()
redisPipeSessionClass = initRedisPipeSessionClass()
}