forked from mtusi2026/mtusi2026lab4
140 lines
3.1 KiB
Go
140 lines
3.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type RedisConfig struct {
|
|
Host string
|
|
Port int
|
|
Password string
|
|
DB int
|
|
MaxRetries int
|
|
DialTimeout time.Duration
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
PoolSize int
|
|
}
|
|
|
|
type RedisCache struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func NewRedis(cfg RedisConfig) (*RedisCache, error) {
|
|
client := redis.NewClient(newClientOptions(cfg))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.DialTimeout)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
_ = client.Close()
|
|
|
|
return nil, fmt.Errorf("ping redis: %w", err)
|
|
}
|
|
|
|
return &RedisCache{client: client}, nil
|
|
}
|
|
|
|
func newClientOptions(cfg RedisConfig) *redis.Options {
|
|
return &redis.Options{
|
|
Addr: net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)),
|
|
Password: cfg.Password,
|
|
DB: cfg.DB,
|
|
MaxRetries: cfg.MaxRetries,
|
|
DialTimeout: cfg.DialTimeout,
|
|
ReadTimeout: cfg.ReadTimeout,
|
|
WriteTimeout: cfg.WriteTimeout,
|
|
PoolSize: cfg.PoolSize,
|
|
}
|
|
}
|
|
|
|
func (r *RedisCache) Get(ctx context.Context, key string) ([]byte, error) {
|
|
value, err := r.client.Get(ctx, key).Bytes()
|
|
if errors.Is(err, redis.Nil) {
|
|
return nil, ErrCacheMiss
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("redis get %q: %w", key, err)
|
|
}
|
|
|
|
return value, nil
|
|
}
|
|
|
|
func (r *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
|
|
if err := r.client.Set(ctx, key, value, ttl).Err(); err != nil {
|
|
return fmt.Errorf("redis set %q: %w", key, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *RedisCache) Delete(ctx context.Context, keys ...string) error {
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if err := r.client.Del(ctx, keys...).Err(); err != nil {
|
|
return fmt.Errorf("redis delete: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var releaseLockScript = redis.NewScript(`
|
|
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
return redis.call("DEL", KEYS[1])
|
|
end
|
|
return 0
|
|
`)
|
|
|
|
func (r *RedisCache) AcquireLock(ctx context.Context, key, token string, ttl time.Duration) (bool, error) {
|
|
acquired, err := r.client.SetNX(ctx, key, token, ttl).Result()
|
|
if err != nil {
|
|
return false, fmt.Errorf("redis acquire lock %q: %w", key, err)
|
|
}
|
|
|
|
return acquired, nil
|
|
}
|
|
|
|
func (r *RedisCache) ReleaseLock(ctx context.Context, key, token string) (bool, error) {
|
|
deleted, err := releaseLockScript.Run(ctx, r.client, []string{key}, token).Int64()
|
|
if err != nil {
|
|
return false, fmt.Errorf("redis release lock %q: %w", key, err)
|
|
}
|
|
|
|
return deleted > 0, nil
|
|
}
|
|
|
|
func (r *RedisCache) LockHeld(ctx context.Context, key string) (bool, error) {
|
|
exists, err := r.client.Exists(ctx, key).Result()
|
|
if err != nil {
|
|
return false, fmt.Errorf("redis check lock %q: %w", key, err)
|
|
}
|
|
|
|
return exists > 0, nil
|
|
}
|
|
|
|
func (r *RedisCache) Ping(ctx context.Context) error {
|
|
if err := r.client.Ping(ctx).Err(); err != nil {
|
|
return fmt.Errorf("redis ping: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *RedisCache) Close() error {
|
|
if err := r.client.Close(); err != nil {
|
|
return fmt.Errorf("redis close: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|