Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
46 lines
882 B
Go
46 lines
882 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Redis struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func NewRedis(ctx context.Context, connectionString string) (*Redis, error) {
|
|
opts, err := redis.ParseURL(connectionString)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parsing redis connection string: %w", err)
|
|
}
|
|
|
|
opts.PoolSize = 10
|
|
opts.MinIdleConns = 2
|
|
opts.DialTimeout = 5 * time.Second
|
|
opts.ReadTimeout = 3 * time.Second
|
|
opts.WriteTimeout = 3 * time.Second
|
|
|
|
client := redis.NewClient(opts)
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, time.Second*5)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(pingCtx).Err(); err != nil {
|
|
return nil, fmt.Errorf("connection to redis: %w", err)
|
|
}
|
|
|
|
return &Redis{client: client}, nil
|
|
}
|
|
|
|
func (r *Redis) Client() *redis.Client {
|
|
return r.client
|
|
}
|
|
|
|
func (r *Redis) Close() error {
|
|
return r.client.Close()
|
|
}
|