加入网络层
This commit is contained in:
34
Server/common/db/redis/client.go
Normal file
34
Server/common/db/redis/client.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
cli *redis.Client
|
||||
Nil = redis.Nil
|
||||
)
|
||||
|
||||
func Init(host string, port int, password string, db int) error {
|
||||
cli = redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||
Password: password,
|
||||
DB: db,
|
||||
})
|
||||
|
||||
_, err := cli.Ping(context.Background()).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
func Client() *redis.Client {
|
||||
return cli
|
||||
}
|
||||
|
||||
func Close() error {
|
||||
if cli != nil {
|
||||
return cli.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user