-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisconn_service.go
231 lines (210 loc) · 5.58 KB
/
redisconn_service.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package redisconn
import (
"fmt"
"sync"
"time"
"github.com/go-redis/redis"
"github.com/sivaosorg/govm/logger"
"github.com/sivaosorg/govm/utils"
)
type RedisService interface {
Get(key string) (interface{}, error)
Set(key string, value interface{}, expiration time.Duration) error
Delete(key string) error
ListKeys() map[string]string
ListKeysNearExpired() []string
Increase(key string) error
Decrease(key string) error
Lock(key string, value interface{}, expiration time.Duration) error
Unlock(key string, value interface{}) error
Handler() *redis.Client
// sync pubsub hook
SyncPubSub() RedisPubSubService
// sync keyspace notification hook
SyncKeySpace() RedisKeySpaceService
}
type redisServiceImpl struct {
redisConn *redis.Client
mutex *RedisMutex
pubsub RedisPubSubService
keyspace RedisKeySpaceService
}
func newRedisMutex() *RedisMutex {
return &RedisMutex{make(map[string]*sync.Mutex)}
}
func NewRedisService(redisConn *redis.Client) RedisService {
s := &redisServiceImpl{
redisConn: redisConn,
mutex: newRedisMutex(),
pubsub: NewRedisPubSub(redisConn),
keyspace: NewRedisKeySpaceService(redisConn),
}
return s
}
func (r *RedisMutex) getMutex(key string) *sync.Mutex {
mutex, ok := r.mutexes[key]
if !ok {
mutex = &sync.Mutex{}
r.mutexes[key] = mutex
}
return mutex
}
func (r *redisServiceImpl) Get(key string) (interface{}, error) {
if utils.IsEmpty(key) {
return nil, fmt.Errorf("invalid key")
}
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
// Get the type of the key
keyType, err := r.redisConn.Type(key).Result()
if err == redis.Nil {
return nil, fmt.Errorf("key %s not found", key)
}
if err != nil {
return nil, err
}
// Check if the key is a hash
if keyType == "hash" {
hash, err := r.redisConn.HGetAll(key).Result()
if err != nil {
return nil, err
}
return hash, nil
}
// Otherwise, get the value of the key
value, err := r.redisConn.Get(key).Result()
if err == redis.Nil {
return nil, fmt.Errorf("key %s not found", key)
}
if err != nil {
return nil, err
}
return value, nil
}
func (r *redisServiceImpl) Set(key string, value interface{}, expiration time.Duration) error {
if utils.IsEmpty(key) {
return fmt.Errorf("invalid key")
}
json, err := utils.MarshalToString(value)
if err != nil {
return err
}
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
return r.redisConn.Set(key, json, expiration).Err()
}
func (r *redisServiceImpl) Delete(key string) error {
if utils.IsEmpty(key) {
return fmt.Errorf("invalid key")
}
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
return r.redisConn.Del(key).Err()
}
func (r *redisServiceImpl) ListKeys() map[string]string {
r.mutex.getMutex("*").Lock()
defer r.mutex.getMutex("*").Unlock()
keys := make(map[string]string)
var cursor uint64
for {
var batchKeys []string
var err error
batchKeys, cursor, err = r.redisConn.Scan(cursor, "*", 10).Result()
if err != nil {
logger.Errorf("ListKeys has an error occurred: %s", err, err.Error())
break
}
for _, key := range batchKeys {
keyType, err := r.redisConn.Type(key).Result()
if err != nil {
logger.Errorf("ListKeys has an error occurred: %s", err, err.Error())
break
}
keys[key] = keyType
}
if cursor == 0 {
break
}
}
return keys
}
func (r *redisServiceImpl) ListKeysNearExpired() []string {
r.mutex.getMutex("*").Lock()
defer r.mutex.getMutex("*").Unlock()
keys := []string{}
var cursor uint64
for {
var batchKeys []string
var err error
batchKeys, cursor, err = r.redisConn.Scan(cursor, "*", 10).Result()
if err != nil {
logger.Errorf("ListKeysNearExpired has an error occurred: %s", err, err.Error())
break
}
for _, key := range batchKeys {
ttl, err := r.redisConn.TTL(key).Result()
if err != nil {
logger.Errorf("ListKeysNearExpired has an error occurred: %s", err, err.Error())
break
}
// If TTL is negative, ignore the key
if ttl < 0 {
continue
}
// If TTL is zero or less than a minute, add to the expired keys list
if ttl == 0 || ttl.Minutes() < 1 {
keys = append(keys, key)
}
}
// Exit the loop once cursor gets to zero
if cursor == 0 {
break
}
}
return keys
}
func (r *redisServiceImpl) Increase(key string) error {
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
_, err := r.redisConn.Incr(key).Result()
return err
}
func (r *redisServiceImpl) Decrease(key string) error {
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
_, err := r.redisConn.Decr(key).Result()
return err
}
func (r *redisServiceImpl) Lock(key string, value interface{}, expiration time.Duration) error {
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
v := utils.ToJson(value)
return r.redisConn.SetNX(key, v, expiration).Err()
}
func (r *redisServiceImpl) Unlock(key string, value interface{}) error {
r.mutex.getMutex(key).Lock()
defer r.mutex.getMutex(key).Unlock()
// Get the current value of the key
currentValue, err := r.redisConn.Get(key).Result()
if err == redis.Nil {
return fmt.Errorf("key %s not found", key)
}
if err != nil {
return err
}
// Check if the key is locked by the given value
if currentValue != utils.ToJson(value) {
return fmt.Errorf("key %s is not locked by the given value", key)
}
// Delete the key from redis
return r.redisConn.Del(key).Err()
}
func (r *redisServiceImpl) Handler() *redis.Client {
return r.redisConn
}
func (r *redisServiceImpl) SyncPubSub() RedisPubSubService {
return r.pubsub
}
func (r *redisServiceImpl) SyncKeySpace() RedisKeySpaceService {
return r.keyspace
}