forked from mtusi2026/mtusi2026lab6
Реализация на 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()
|
||||
}
|
||||
Reference in New Issue
Block a user