forked from mtusi2026/mtusi2026lab6
32 lines
598 B
Go
32 lines
598 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func NewRedis(ctx context.Context, addr string) (*redis.Client, error) {
|
|
|
|
options := &redis.Options{
|
|
Addr: addr,
|
|
DialTimeout: 3 * time.Second,
|
|
ReadTimeout: 3 * time.Second,
|
|
WriteTimeout: 2 * time.Second,
|
|
PoolSize: 10,
|
|
PoolTimeout: 2 * time.Second,
|
|
ContextTimeoutEnabled: true,
|
|
}
|
|
|
|
client := redis.NewClient(options)
|
|
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
_ = client.Close()
|
|
return nil, err
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|