-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisString.go
64 lines (53 loc) · 1.77 KB
/
redisString.go
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
54
55
56
57
58
59
60
61
62
63
64
package redis
import (
"context"
"errors"
"time"
"github.com/go-redis/redis/v8"
)
type redisString struct {
*redisManager
}
// StringSet 设置缓存
func (receiver *redisString) StringSet(key string, value any) error {
traceDetail := receiver.traceManager.TraceRedis("StringSet", key, "")
err := receiver.GetClient().Set(context.Background(), key, value, 0).Err()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return err
}
// StringGet 获取缓存
func (receiver *redisString) StringGet(key string) (string, error) {
traceDetail := receiver.traceManager.TraceRedis("StringGet", key, "")
result, err := receiver.GetClient().Get(context.Background(), key).Result()
if errors.Is(err, redis.Nil) {
err = nil
}
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return result, err
}
// StringSetEX 设置过期时间(如果KEY存在,则会覆盖)
func (receiver *redisString) StringSetEX(key string, value any, expiration time.Duration) (string, error) {
traceDetail := receiver.traceManager.TraceRedis("StringSetNX", key, "")
result, err := receiver.GetClient().SetEX(context.Background(), key, value, expiration).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return result, err
}
// StringSetNX 设置过期时间(如果KEY存在,则会更新失败)
func (receiver *redisString) StringSetNX(key string, value any, expiration time.Duration) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("StringSetNX", key, "")
result, err := receiver.GetClient().SetNX(context.Background(), key, value, expiration).Result()
defer func() { traceDetail.End(err) }()
if err == nil && result {
traceDetail.SetRows(1)
}
return result, err
}