/
redis.go
166 lines (134 loc) · 3.48 KB
/
redis.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package redis
import (
"fmt"
"time"
"errors"
"context"
"github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/extra/redisotel/v8"
"github.com/deatil/go-goch/goch"
)
// 日志接口
type iLogger interface {
Errorf(template string, args ...any)
}
// 缓存配置
type Config struct {
Addr string
Password string
DB int
MinIdleConn int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolSize int
PoolTimeout time.Duration
EnableTrace bool
KeyPrefix string
Logger iLogger
}
/**
* redis 缓存
*
* @create 2021-9-8
* @author deatil
*/
type Redis struct {
// 上下文
ctx context.Context
// 前缀
prefix string
// 缓存
cache *cache.Cache
// 客户端
client *redis.Client
}
// 构造函数
func New(config Config) Redis {
client := redis.NewClient(&redis.Options{
Addr: config.Addr,
Password: config.Password,
DB: config.DB,
MinIdleConns: config.MinIdleConn,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
PoolSize: config.PoolSize,
PoolTimeout: config.PoolTimeout,
})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if _, err := client.Ping(ctx).Result(); err != nil {
if config.Logger != nil {
config.Logger.Errorf("redis: %s", err.Error())
}
}
// 调试
if config.EnableTrace {
client.AddHook(redisotel.NewTracingHook())
}
return Redis{
ctx: context.TODO(),
prefix: config.KeyPrefix,
client: client,
cache: cache.New(&cache.Options{
Redis: client,
LocalCache: cache.NewTinyLFU(1000, time.Minute),
}),
}
}
// 设置
func (this Redis) Set(key string, value any, expiration any) error {
ttl := goch.ToDuration(expiration)
return this.cache.Set(&cache.Item{
Ctx: this.ctx,
Key: this.wrapperKey(key),
Value: value,
TTL: ttl,
SkipLocalCache: true,
})
}
// 获取
func (this Redis) Get(key string, value any) error {
err := this.cache.Get(this.ctx, this.wrapperKey(key), value)
if err == cache.ErrCacheMiss {
err = errors.New("Redis Key No Exist")
}
return err
}
func (this Redis) Delete(keys ...string) (bool, error) {
wrapperKeys := make([]string, len(keys))
for index, key := range keys {
wrapperKeys[index] = this.wrapperKey(key)
}
cmd := this.client.Del(this.ctx, wrapperKeys...)
if err := cmd.Err(); err != nil {
return false, err
}
return cmd.Val() > 0, nil
}
func (this Redis) Check(keys ...string) (bool, error) {
wrapperKeys := make([]string, len(keys))
for index, key := range keys {
wrapperKeys[index] = this.wrapperKey(key)
}
cmd := this.client.Exists(this.ctx, wrapperKeys...)
if err := cmd.Err(); err != nil {
return false, err
}
return cmd.Val() > 0, nil
}
func (this Redis) Close() error {
return this.client.Close()
}
func (this Redis) GetClient() *redis.Client {
return this.client
}
// 包装 key 值
func (this Redis) wrapperKey(key string) string {
if this.prefix == "" {
return key
}
return fmt.Sprintf("%s:%s", this.prefix, key)
}