mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-06-03 20:12:58 +00:00
feat: initial implementation of container management platform
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user