pkg/storage/redis.go

38 lines
894 B
Go

package storage
import (
"net"
"strconv"
"time"
"git.ifooth.com/common/pkg/config"
redis "github.com/redis/go-redis/v9"
)
// RedisSession :
type RedisSession struct {
Client *redis.Client
}
// NewRedisSession
func NewRedisSession(redisConf *config.RedisConf) (*RedisSession, error) {
client := redis.NewClient(&redis.Options{
Addr: net.JoinHostPort(redisConf.Host, strconv.Itoa(redisConf.Port)),
Password: redisConf.Password,
DB: redisConf.DB,
DialTimeout: time.Duration(redisConf.MaxConnTimeout) * time.Second,
ReadTimeout: time.Duration(redisConf.ReadTimeout) * time.Second,
PoolSize: redisConf.MaxPoolSize,
ConnMaxIdleTime: time.Duration(redisConf.IdleTimeout) * time.Second,
})
r := &RedisSession{Client: client}
return r, nil
}
// Close : close redis session
func (r *RedisSession) Close() {
r.Client.Close()
}