forked from mtusi2026/mtusi2026lab2
44 lines
835 B
Go
44 lines
835 B
Go
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()
|
|
}
|