/
redis.go
46 lines (35 loc) · 887 Bytes
/
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
package stores
// import redis
import (
"context"
"time"
"github.com/redis/go-redis/v9"
)
type Redis struct {
client *redis.Client
ctx context.Context
}
func NewRedisStore(redis_host string, redis_port string, redis_user string, redis_pass string, redis_db int) (*Redis, error) {
redisDB := new(Redis)
ctx, cancel := context.WithTimeout(context.Background(), time.Second+10)
defer cancel()
//
redisDB.client = redis.NewClient(&redis.Options{
Addr: redis_host + ":" + redis_port,
Username: redis_user,
Password: redis_pass,
DB: redis_db,
PoolSize: 100,
PoolTimeout: time.Second * 10,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
MaxIdleConns: 10,
})
redisDB.ctx = ctx
// test connection
_, err := redisDB.client.Ping(ctx).Result()
if nil != err {
return nil, err
}
return redisDB, nil
}