-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisKey.go
66 lines (51 loc) · 1.79 KB
/
redisKey.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
65
66
package redis
import (
"context"
"fmt"
"strings"
"time"
"github.com/farseer-go/fs/parse"
)
type redisKey struct {
*redisManager
}
func (receiver *redisKey) SetTTL(key string, d time.Duration) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("SetTTL", key, "")
result, err := receiver.GetClient().Expire(context.Background(), key, d).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return result, err
}
func (receiver *redisKey) TTL(key string) (time.Duration, error) {
traceDetail := receiver.traceManager.TraceRedis("TTL", key, "")
result, err := receiver.GetClient().TTL(context.Background(), key).Result()
defer func() { traceDetail.End(err) }()
return result, err
}
func (receiver *redisKey) Del(keys ...string) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("Del", strings.Join(keys, ","), "")
result, err := receiver.GetClient().Del(context.Background(), keys...).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(parse.ToInt(result))
}
return result > 0, err
}
func (receiver *redisKey) Exists(keys ...string) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("Exists", strings.Join(keys, ","), "")
result, err := receiver.GetClient().Exists(context.Background(), keys...).Result()
defer func() { traceDetail.End(err) }()
return result > 0, err
}
func (receiver *redisKey) Search(searchKeys string) ([]string, error) {
if searchKeys == "*" {
return nil, fmt.Errorf("考虑性能影响,不支持仅通过*查找")
}
traceDetail := receiver.traceManager.TraceRedis("Search", searchKeys, "")
result := receiver.GetClient().Keys(context.Background(), searchKeys)
keys, err := result.Result()
defer func() { traceDetail.End(err) }()
return keys, err
}