-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv.go
More file actions
53 lines (43 loc) · 1.39 KB
/
kv.go
File metadata and controls
53 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package redisdk
import (
"context"
"errors"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func Set(key string, value any, expiration time.Duration) (string, error) {
return universalClient.Set(context.Background(), key, value, expiration).Result()
}
func Get(key string) (string, error) {
val, err := universalClient.Get(context.Background(), key).Result()
if err != nil && !errors.Is(err, redis.Nil) {
return "", err
}
return val, nil
}
func Expire(key string, expiration time.Duration) (bool, error) {
return universalClient.Expire(context.Background(), key, expiration).Result()
}
func PExpire(stream string, expiration time.Duration) error {
ok, err := universalClient.PExpire(context.Background(), stream, expiration).Result()
if err != nil {
return err
}
if !ok {
return fmt.Errorf("failed to set expiration for stream %s", stream)
}
return nil
}
func Del(keys ...string) (int64, error) {
return universalClient.Del(context.Background(), keys...).Result()
}
func SetNX(key string, value any, expiration time.Duration) (bool, error) {
return universalClient.SetNX(context.Background(), key, value, expiration).Result()
}
func Eval(script string, keys []string, args ...any) (any, error) {
return universalClient.Eval(context.Background(), script, keys, args...).Result()
}
func TTL(key string) (time.Duration, error) {
return universalClient.TTL(context.Background(), key).Result()
}