forked from mtusi2026/mtusi2026lab2
Реализация на 5
This commit is contained in:
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
)
|
||||
|
||||
const defaultRedisPort = 6379
|
||||
|
||||
// OperationTimeout bounds each optional cache operation independently of the request.
|
||||
const OperationTimeout = 300 * time.Millisecond
|
||||
|
||||
type Client struct {
|
||||
*redis.Client
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
if cfg.Redis.Port == 0 {
|
||||
cfg.Redis.Port = defaultRedisPort
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Client: redis.NewClient(cfg.Redis.BuildRedisOptions()),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, OperationTimeout)
|
||||
defer cancel()
|
||||
return c.Client.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func (c *Client) Start(ctx context.Context) error {
|
||||
return c.Ping(ctx)
|
||||
}
|
||||
|
||||
func (c *Client) Stop(_ context.Context) error {
|
||||
return c.Client.Close()
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func addMaxConnectionsToCfg(cfg any, poolCfg *pgxpool.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
func New(cfg MigrationConfig, afterConnect ...AfterConnect) (*pgxpool.Pool, error) {
|
||||
func New(cfg MigrationConfig, afterConnect ...AfterConnect) *pgxpool.Pool {
|
||||
lvl := getLevelFromConfig(cfg)
|
||||
|
||||
if len(afterConnect) == 0 {
|
||||
@@ -43,7 +43,7 @@ func New(cfg MigrationConfig, afterConnect ...AfterConnect) (*pgxpool.Pool, erro
|
||||
poolConfig, err := pgxpool.ParseConfig(cfg.GetURI())
|
||||
if err != nil {
|
||||
slog.Error("failed to parse postgres config", "error", err)
|
||||
return nil, errParseConfig
|
||||
panic(err)
|
||||
}
|
||||
|
||||
addMaxConnectionsToCfg(cfg, poolConfig)
|
||||
@@ -61,11 +61,11 @@ func New(cfg MigrationConfig, afterConnect ...AfterConnect) (*pgxpool.Pool, erro
|
||||
pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig)
|
||||
if err != nil {
|
||||
slog.Error("failed to create postgres pool", "error", err)
|
||||
return nil, errCreatingPool
|
||||
panic(err)
|
||||
}
|
||||
|
||||
slog.Info("successfully connected to postgres pool")
|
||||
return pool, nil
|
||||
return pool
|
||||
}
|
||||
|
||||
func load(afterConnects ...AfterConnect) func(ctx context.Context, conn *pgx.Conn) error {
|
||||
|
||||
Reference in New Issue
Block a user