2021-08-09 03:36:09 +00:00
|
|
|
package redisdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
)
|
|
|
|
|
2022-12-05 08:14:06 +00:00
|
|
|
type Server struct {
|
2021-08-09 03:36:09 +00:00
|
|
|
rdb *redis.Client
|
2022-12-05 08:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r *Server
|
2021-08-09 03:36:09 +00:00
|
|
|
|
|
|
|
// Connect ...
|
2022-12-05 08:30:36 +00:00
|
|
|
func Connect(uri, password string) (*Server, error) {
|
2021-08-09 03:36:09 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-05 08:14:06 +00:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
2021-08-09 03:36:09 +00:00
|
|
|
Addr: uri,
|
|
|
|
Password: password,
|
|
|
|
DB: 0, // use default DB
|
|
|
|
})
|
|
|
|
|
|
|
|
// Ping
|
|
|
|
_, err := rdb.Ping(ctx).Result()
|
|
|
|
if err != nil {
|
2022-12-05 07:54:51 +00:00
|
|
|
fmt.Printf("[redisdb] connect to %s error: %s \n", uri, err.Error())
|
2022-12-05 08:14:06 +00:00
|
|
|
return nil, err
|
2021-08-09 03:36:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 08:14:06 +00:00
|
|
|
// assign data
|
|
|
|
r = &Server{rdb: rdb}
|
|
|
|
|
2022-12-21 10:46:39 +00:00
|
|
|
fmt.Printf("⚡️[redis]: connected to %s \n", uri)
|
2021-08-09 03:36:09 +00:00
|
|
|
|
2022-12-05 08:14:06 +00:00
|
|
|
return r, nil
|
2021-08-09 03:36:09 +00:00
|
|
|
}
|