-
Notifications
You must be signed in to change notification settings - Fork 17
/
redisdb.go
278 lines (237 loc) · 5.7 KB
/
redisdb.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package redis
import (
"sync"
"time"
)
const (
keysMapSize = 32
redisDbMapSizeDefault = 3
)
// A redis database.
// There can be more than one in a redis instance.
type RedisDb struct {
// Database id
id DatabaseId
// All keys in this db.
keys Keys
// Keys with expire timestamp.
expiringKeys ExpiringKeys
// TODO long long avg_ttl; /* Average TTL, just for stats */
redis *Redis
}
// Redis databases map
type RedisDbs map[DatabaseId]*RedisDb
// Database id
type DatabaseId uint
// Key-Item map
type Keys map[string]Item
// Keys with expire timestamp.
type ExpiringKeys map[string]time.Time
// The item interface. An item is the value of a key.
type Item interface {
// The pointer to the value.
Value() interface{}
// The id of the type of the Item.
// This need to be constant for the type because it is
// used when de-/serializing item from/to disk.
Type() uint64
// The type of the Item as readable string.
TypeFancy() string
// OnDelete is triggered before the key of the item is deleted.
// db is the affected database.
OnDelete(key *string, db *RedisDb)
}
// NewRedisDb creates a new db.
func NewRedisDb(id DatabaseId, r *Redis) *RedisDb {
return &RedisDb{
id: id,
redis: r,
keys: make(Keys, keysMapSize),
expiringKeys: make(ExpiringKeys, keysMapSize),
}
}
// RedisDb gets the redis database by its id or creates and returns it if not exists.
func (r *Redis) RedisDb(dbId DatabaseId) *RedisDb {
getDb := func() *RedisDb { // returns nil if db not exists
if db, ok := r.redisDbs[dbId]; ok {
return db
}
return nil
}
r.Mu().RLock()
db := getDb()
r.Mu().RUnlock()
if db != nil {
return db
}
// create db
r.Mu().Lock()
defer r.Mu().Unlock()
// check if db does not exists again since
// multiple "mutex readers" can come to this point
db = getDb()
if db != nil {
return db
}
// now really create db of that id
r.redisDbs[dbId] = NewRedisDb(dbId, r)
return r.redisDbs[dbId]
}
// RedisDbs gets all redis databases.
func (r *Redis) RedisDbs() RedisDbs {
r.Mu().RLock()
defer r.Mu().RUnlock()
return r.redisDbs
}
// Redis gets the redis instance.
func (db *RedisDb) Redis() *Redis {
return db.redis
}
// Mu gets the mutex.
func (db *RedisDb) Mu() *sync.RWMutex {
return db.Redis().Mu()
}
// Id gets the db id.
func (db *RedisDb) Id() DatabaseId {
return db.id
}
// Sets a key with an item which can have an expiration time.
func (db *RedisDb) Set(key *string, i Item, expires bool, expiry time.Time) {
db.Mu().Lock()
defer db.Mu().Unlock()
db.keys[*key] = i
if expires {
db.expiringKeys[*key] = expiry
}
}
// Returns the item by the key or nil if key does not exists.
func (db *RedisDb) Get(key *string) Item {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.get(key)
}
func (db *RedisDb) get(key *string) Item {
i, _ := db.keys[*key]
return i
}
// Deletes a key, returns number of deleted keys.
func (db *RedisDb) Delete(keys ...*string) int {
db.Mu().Lock()
defer db.Mu().Unlock()
return db.delete(keys...)
}
// If checkExists is false, then return bool is reprehensible.
func (db *RedisDb) delete(keys ...*string) int {
do := func(k *string) bool {
if k == nil {
return false
}
i := db.get(k)
if i == nil {
return false
}
i.OnDelete(k, db)
delete(db.keys, *k)
delete(db.expiringKeys, *k)
return true
}
var c int
for _, k := range keys {
if do(k) {
c++
}
}
return c
}
func (db *RedisDb) DeleteExpired(keys ...*string) int {
var c int
for _, k := range keys {
if k != nil && db.Expired(k) && db.Delete(k) > 0 {
c++
}
}
return c
}
// GetOrExpire gets the item or nil if expired or not exists. If 'deleteIfExpired' is true the key will be deleted.
func (db *RedisDb) GetOrExpire(key *string, deleteIfExpired bool) Item {
// TODO mutex optimize this func so that a RLock is mainly first opened
db.Mu().Lock()
defer db.Mu().Unlock()
i, ok := db.keys[*key]
if !ok {
return nil
}
if db.expired(key) {
if deleteIfExpired {
db.delete(key)
}
return nil
}
return i
}
// IsEmpty checks if db is empty.
func (db *RedisDb) IsEmpty() bool {
db.Mu().RLock()
defer db.Mu().RUnlock()
return len(db.keys) == 0
}
// HasExpiringKeys checks if db has any expiring keys.
func (db *RedisDb) HasExpiringKeys() bool {
db.Mu().RLock()
defer db.Mu().RUnlock()
return len(db.expiringKeys) != 0
}
// Check if key exists.
func (db *RedisDb) Exists(key *string) bool {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.exists(key)
}
func (db *RedisDb) exists(key *string) bool {
_, ok := db.keys[*key]
return ok
}
// Check if key has an expiry set.
func (db *RedisDb) Expires(key *string) bool {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.expires(key)
}
func (db *RedisDb) expires(key *string) bool {
_, ok := db.expiringKeys[*key]
return ok
}
// Expired only check if a key can and is expired.
func (db *RedisDb) Expired(key *string) bool {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.expired(key)
}
func (db *RedisDb) expired(key *string) bool {
return db.expires(key) && TimeExpired(db.expiry(key))
}
// Expiry gets the expiry of the key has one.
func (db *RedisDb) Expiry(key *string) time.Time {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.expiry(key)
}
func (db *RedisDb) expiry(key *string) time.Time {
return db.expiringKeys[*key]
}
// Keys gets all keys in this db.
func (db *RedisDb) Keys() Keys {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.keys
}
// ExpiringKeys gets keys with an expiry set and their timeout.
func (db *RedisDb) ExpiringKeys() ExpiringKeys {
db.Mu().RLock()
defer db.Mu().RUnlock()
return db.expiringKeys
}
// TimeExpired check if a timestamp is older than now.
func TimeExpired(expireAt time.Time) bool {
return time.Now().After(expireAt)
}