46 lines
798 B
Go
46 lines
798 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"git.hlsq.asia/alex/service-common/config"
|
|
"git.hlsq.asia/alex/service-common/log"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var instance *Client
|
|
|
|
type Client struct {
|
|
*redis.Client
|
|
}
|
|
|
|
func Init(cfg *config.RedisConfig) error {
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: cfg.Addr,
|
|
Password: cfg.Password,
|
|
DB: cfg.DB,
|
|
})
|
|
instance = &Client{
|
|
Client: client,
|
|
}
|
|
cacheInstance = &CacheClient{
|
|
cli: client,
|
|
}
|
|
if logger := log.GetLogger(); logger != nil {
|
|
cacheInstance.logger = logger.Named("CACHE")
|
|
}
|
|
|
|
_, err := client.Ping(context.Background()).Result()
|
|
return err
|
|
}
|
|
|
|
func Close() error {
|
|
if instance != nil && instance.Client != nil {
|
|
return instance.Client.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetClient() *Client {
|
|
return instance
|
|
}
|