-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_redis.go
More file actions
46 lines (41 loc) · 818 Bytes
/
api_redis.go
File metadata and controls
46 lines (41 loc) · 818 Bytes
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
package redis
import (
"sync"
"github.com/gomodule/redigo/redis"
)
type luaRedis struct {
sync.Mutex
pool *redis.Pool
}
func (l *luaRedis) constructor(conn string, db int) (lRedis, error) {
pool := &redis.Pool{
// Other pool configuration not shown in this example.
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", conn)
if err != nil {
return nil, err
}
if _, err := c.Do("SELECT", db); err != nil {
c.Close()
return nil, err
}
return c, nil
},
}
result := &luaRedis{}
pool.MaxIdle = MaxIdleConns
pool.MaxActive = MaxOpenConns
result.pool = pool
c := pool.Get()
defer c.Close()
_, err := c.Do("PING")
if err != nil {
return nil, err
}
return result, nil
}
func (l *luaRedis) getPool() *redis.Pool {
l.Lock()
defer l.Unlock()
return l.pool
}