48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package redis
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
go_redis "github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Redis struct {
|
|
Client *go_redis.Client
|
|
Prefix string
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Prefix string `mapstructure:"prefix" json:"prefix"`
|
|
Host string `mapstructure:"host" json:"host"`
|
|
Port int `mapstructure:"post" json:"post"`
|
|
DB int `mapstructure:"db" json:"db"`
|
|
Password string `mapstructure:"password" json:"password"`
|
|
}
|
|
|
|
func IsRedisNil(err error) bool {
|
|
return err == go_redis.Nil
|
|
}
|
|
|
|
func NewRedis(conf *RedisConfig) (*Redis, error) {
|
|
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
client := go_redis.NewClient(&go_redis.Options{
|
|
Addr: addr,
|
|
DB: conf.DB,
|
|
Password: conf.Password,
|
|
DialTimeout: 5 * time.Second,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
})
|
|
|
|
// _, err := client.Ping().Result()
|
|
// if err != nil {
|
|
// return nil, fmt.Errorf("failed to ping redis. with addr '%s'", addr)
|
|
// }
|
|
|
|
return &Redis{
|
|
Client: client,
|
|
Prefix: conf.Prefix,
|
|
}, nil
|
|
}
|