mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-06-03 20:12:58 +00:00
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
type Redis struct {
|
|
Client *redis.Client
|
|
}
|
|
|
|
func NewRedis(redisURL string) *Redis {
|
|
opt, err := redis.ParseURL(redisURL)
|
|
if err != nil {
|
|
// Fallback to default Redis options if URL parsing fails
|
|
opt = &redis.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "",
|
|
DB: 0,
|
|
}
|
|
}
|
|
|
|
client := redis.NewClient(opt)
|
|
|
|
return &Redis{Client: client}
|
|
}
|
|
|
|
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
|
|
}
|