mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-06-03 20:12:58 +00:00
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
type Redis struct {
|
|
Client *redis.Client
|
|
}
|
|
|
|
func NewRedis(redisURL string) (*Redis, error) {
|
|
if strings.TrimSpace(redisURL) == "" {
|
|
return nil, fmt.Errorf("redis URL cannot be empty")
|
|
}
|
|
|
|
opt, err := redis.ParseURL(redisURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid redis URL: %w", err)
|
|
}
|
|
|
|
client := redis.NewClient(opt)
|
|
|
|
return &Redis{Client: client}, nil
|
|
}
|
|
|
|
func (r *Redis) Close() error {
|
|
return r.Client.Close()
|
|
}
|
|
|
|
func (r *Redis) Health(ctx context.Context) error {
|
|
_, err := r.Client.Ping(ctx).Result()
|
|
return err
|
|
}
|
|
|
|
func (r *Redis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
|
return r.Client.Set(ctx, key, value, expiration).Err()
|
|
}
|
|
|
|
func (r *Redis) Get(ctx context.Context, key string) (string, error) {
|
|
return r.Client.Get(ctx, key).Result()
|
|
}
|
|
|
|
func (r *Redis) Del(ctx context.Context, keys ...string) error {
|
|
return r.Client.Del(ctx, keys...).Err()
|
|
}
|
|
|
|
func (r *Redis) Exists(ctx context.Context, key string) (bool, error) {
|
|
result, err := r.Client.Exists(ctx, key).Result()
|
|
return result > 0, err
|
|
}
|