-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_standalone.go
47 lines (38 loc) · 979 Bytes
/
redis_standalone.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
package letsgo
import (
"fmt"
"github.com/gomodule/redigo/redis" //redigo
)
//Lredis 结构体
type Lredis struct {
Redis *redis.Pool
}
//newLredis 返回一个Lredis结构体指针
func newLredis() *Lredis {
return &Lredis{}
}
//Init 连接redis cluster集群
func (c *Lredis) Init(serverlist []string, options []redis.DialOption) error {
var err error
c.Redis, err = RedisCreatePool(serverlist[0], options...)
if err != nil {
return err
}
return nil
}
//GetConn 得到一个redis.Conn
func (c *Lredis) GetConn(Retry bool) redis.Conn {
return c.Redis.Get()
}
//DoOnce 映射redisc.Do 方法
func (c *Lredis) DoOnce(commandName string, args ...interface{}) (reply interface{}, err error) {
redisconn := c.Redis.Get()
defer redisconn.Close()
if redisconn.Err() != nil {
return nil, fmt.Errorf("Lredis:err while conn: %s", redisconn.Err().Error())
}
return redisconn.Do(commandName, args...)
}
func (c *Lredis) Close() error {
return c.Redis.Close()
}