2024-05-15 11:56:40 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-06-04 11:41:50 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2024-05-15 11:56:40 +08:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DBInfo struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Port uint `json:"port"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedisClient(conn DBInfo) (*redis.Client, error) {
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
Addr: fmt.Sprintf("%s:%v", conn.Address, conn.Port),
|
|
|
|
Password: conn.Password,
|
|
|
|
DB: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
if _, err := client.Ping().Result(); err != nil {
|
2024-06-04 11:41:50 +08:00
|
|
|
global.LOG.Errorf("check redis conn failed, err: %v", err)
|
2024-05-15 11:56:40 +08:00
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|