-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
redis.go
36 lines (29 loc) · 750 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
package database
import (
"context"
"fmt"
"os"
"github.com/bmf-san/gobel-api/app/interfaces/database"
"github.com/go-redis/redis/v9"
)
// A RedisConn represents a redis connection
type RedisConn struct{}
// NewMySQLConn creates a RedisConn struct.
func NewRedisConn() database.RedisConn {
return &RedisConn{}
}
// Conn get a redis connection.
func (rc *RedisConn) Conn() (*redis.Client, error) {
dataSourceName := fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT"))
conn := redis.NewClient(&redis.Options{
Addr: dataSourceName,
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
ctx := context.Background()
_, err := conn.Ping(ctx).Result()
if err != nil {
return nil, err
}
return conn, nil
}