forked from xiaonanln/goworld
/
gwredis.go
43 lines (37 loc) · 1.02 KB
/
gwredis.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
package gwredis
import (
"github.com/garyburd/redigo/redis"
"github.com/xiaonanln/goworld/engine/async"
)
const (
_REDIS_ASYNC_JOB_GROUP = "_redis"
)
type DB struct {
conn redis.Conn
}
func Dial(network, address string, options []redis.DialOption, ac async.AsyncCallback) {
async.AppendAsyncJob(_REDIS_ASYNC_JOB_GROUP, func() (res interface{}, err error) {
conn, err := redis.Dial(network, address, options...)
if err == nil {
return &DB{conn}, nil
} else {
return nil, err
}
}, ac)
}
func DialURL(rawurl string, options []redis.DialOption, ac async.AsyncCallback) {
async.AppendAsyncJob(_REDIS_ASYNC_JOB_GROUP, func() (res interface{}, err error) {
conn, err := redis.DialURL(rawurl, options...)
if err == nil {
return &DB{conn}, nil
} else {
return nil, err
}
}, ac)
}
func (db *DB) Do(commandName string, args []interface{}, ac async.AsyncCallback) {
async.AppendAsyncJob(_REDIS_ASYNC_JOB_GROUP, func() (res interface{}, err error) {
res, err = db.conn.Do(commandName, args...)
return
}, ac)
}