-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathcache.go
301 lines (263 loc) · 7.58 KB
/
cache.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license
package mirrors
import (
"fmt"
"strconv"
"strings"
"time"
"unsafe"
"github.com/etix/mirrorbits/database"
"github.com/etix/mirrorbits/filesystem"
"github.com/etix/mirrorbits/network"
"github.com/etix/mirrorbits/utils"
"github.com/gomodule/redigo/redis"
)
// Cache implements a local caching mechanism of type LRU for content available in the
// redis database that is automatically invalidated if the object is updated in Redis.
type Cache struct {
r *database.Redis
fiCache *LRUCache
fmCache *LRUCache
mCache *LRUCache
fimCache *LRUCache
mirrorUpdateEvent chan string
fileUpdateEvent chan string
mirrorFileUpdateEvent chan string
pubsubReconnectedEvent chan string
invalidationEvent chan string
}
type fileInfoValue struct {
value filesystem.FileInfo
}
func (f *fileInfoValue) Size() int {
return int(unsafe.Sizeof(f.value))
}
type fileMirrorValue struct {
value []int
}
func (f *fileMirrorValue) Size() int {
return cap(f.value)
}
type mirrorValue struct {
value Mirror
}
func (f *mirrorValue) Size() int {
return int(unsafe.Sizeof(f.value))
}
// NewCache constructs a new instance of Cache
func NewCache(r *database.Redis) *Cache {
if r == nil || r.Pubsub == nil {
return nil
}
c := &Cache{
r: r,
}
// Create the LRU
c.fiCache = NewLRUCache(1024000)
c.fmCache = NewLRUCache(2048000)
c.mCache = NewLRUCache(1024000)
c.fimCache = NewLRUCache(4096000)
// Create event channels
c.mirrorUpdateEvent = make(chan string, 10)
c.fileUpdateEvent = make(chan string, 10)
c.mirrorFileUpdateEvent = make(chan string, 10)
c.pubsubReconnectedEvent = make(chan string)
c.invalidationEvent = make(chan string, 10)
// Subscribe to events
c.r.Pubsub.SubscribeEvent(database.MIRROR_UPDATE, c.mirrorUpdateEvent)
c.r.Pubsub.SubscribeEvent(database.FILE_UPDATE, c.fileUpdateEvent)
c.r.Pubsub.SubscribeEvent(database.MIRROR_FILE_UPDATE, c.mirrorFileUpdateEvent)
c.r.Pubsub.SubscribeEvent(database.PUBSUB_RECONNECTED, c.pubsubReconnectedEvent)
go func() {
for {
//FIXME add a close channel
select {
case data := <-c.mirrorUpdateEvent:
c.mCache.Delete(data)
select {
case c.invalidationEvent <- data:
default:
// Non-blocking
}
case data := <-c.fileUpdateEvent:
c.fiCache.Delete(data)
case data := <-c.mirrorFileUpdateEvent:
s := strings.SplitN(data, " ", 2)
c.fmCache.Delete(s[1])
c.fimCache.Delete(fmt.Sprintf("%s|%s", s[0], s[1]))
case <-c.pubsubReconnectedEvent:
c.Clear()
}
}
}()
return c
}
// Clear clears the local cache
func (c *Cache) Clear() {
c.fiCache.Clear()
c.fmCache.Clear()
c.mCache.Clear()
c.fimCache.Clear()
}
// GetMirrorInvalidationEvent returns a channel that contains ID of mirrors
// that have just been invalidated. This function is supposed to have only
// ONE reader and is made to avoid a race for MIRROR_UPDATE events between
// a mirror invalidation and a mirror being fetched from the cache.
func (c *Cache) GetMirrorInvalidationEvent() <-chan string {
return c.invalidationEvent
}
// GetFileInfo returns file information for a given file either from the cache
// or directly from the database if the object is not yet stored in the cache.
func (c *Cache) GetFileInfo(path string) (f filesystem.FileInfo, err error) {
v, ok := c.fiCache.Get(path)
if ok {
f = v.(*fileInfoValue).value
} else {
f, err = c.fetchFileInfo(path)
}
return
}
func (c *Cache) fetchFileInfo(path string) (f filesystem.FileInfo, err error) {
rconn := c.r.Get()
defer rconn.Close()
f.Path = path // Path is not stored in the object instance in redis
reply, err := redis.Strings(rconn.Do("HMGET", fmt.Sprintf("FILE_%s", path), "size", "modTime", "sha1", "sha256", "md5"))
if err != nil {
return
}
f.Size, _ = strconv.ParseInt(reply[0], 10, 64)
f.ModTime, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", reply[1])
f.Sha1 = reply[2]
f.Sha256 = reply[3]
f.Md5 = reply[4]
c.fiCache.Set(path, &fileInfoValue{value: f})
return
}
// GetMirrors returns all the mirrors serving a given file either from the cache
// or directly from the database if the object is not yet stored in the cache.
func (c *Cache) GetMirrors(path string, clientInfo network.GeoIPRecord) (mirrors []Mirror, err error) {
var mirrorsIDs []int
v, ok := c.fmCache.Get(path)
if ok {
mirrorsIDs = v.(*fileMirrorValue).value
} else {
mirrorsIDs, err = c.fetchFileMirrors(path)
if err != nil {
return
}
}
mirrors = make([]Mirror, 0, len(mirrorsIDs))
for _, id := range mirrorsIDs {
var mirror Mirror
var fileInfo filesystem.FileInfo
v, ok := c.mCache.Get(strconv.Itoa(id))
if ok {
mirror = v.(*mirrorValue).value
} else {
//TODO execute missing items in a MULTI query
mirror, err = c.fetchMirror(id)
if err != nil {
return
}
}
v, ok = c.fimCache.Get(fmt.Sprintf("%d|%s", id, path))
if ok {
fileInfo = v.(*fileInfoValue).value
} else {
fileInfo, err = c.fetchFileInfoMirror(id, path)
if err != nil {
return
}
}
if fileInfo.Size >= 0 {
mirror.FileInfo = &fileInfo
}
// Add the path in the results so we can access it from the templates
mirror.FileInfo.Path = path
if clientInfo.IsValid() {
mirror.Distance = utils.GetDistanceKm(clientInfo.Latitude,
clientInfo.Longitude,
mirror.Latitude,
mirror.Longitude)
} else {
mirror.Distance = 0
}
mirrors = append(mirrors, mirror)
}
return
}
func (c *Cache) fetchFileMirrors(path string) (ids []int, err error) {
rconn := c.r.Get()
defer rconn.Close()
ids, err = redis.Ints(rconn.Do("SMEMBERS", fmt.Sprintf("FILEMIRRORS_%s", path)))
if err != nil {
return
}
c.fmCache.Set(path, &fileMirrorValue{value: ids})
return
}
func (c *Cache) fetchMirror(mirrorID int) (mirror Mirror, err error) {
rconn := c.r.Get()
defer rconn.Close()
reply, err := redis.Values(rconn.Do("HGETALL", fmt.Sprintf("MIRROR_%d", mirrorID)))
if err != nil {
return
}
if len(reply) == 0 {
err = redis.ErrNil
return
}
err = redis.ScanStruct(reply, &mirror)
if err != nil {
return
}
mirror.Prepare()
c.mCache.Set(strconv.Itoa(mirrorID), &mirrorValue{value: mirror})
return
}
func (c *Cache) GetFileInfoMirror(mirrorID int, path string) (f filesystem.FileInfo, err error) {
var fileInfo filesystem.FileInfo
v, ok := c.fimCache.Get(fmt.Sprintf("%d|%s", mirrorID, path))
if ok {
fileInfo = v.(*fileInfoValue).value
} else {
fileInfo, err = c.fetchFileInfoMirror(mirrorID, path)
if err != nil {
return
}
}
return fileInfo, nil
}
func (c *Cache) fetchFileInfoMirror(id int, path string) (f filesystem.FileInfo, err error) {
rconn := c.r.Get()
defer rconn.Close()
f.Path = path // Path is not stored in the object instance in redis
reply, err := redis.Strings(rconn.Do("HMGET", fmt.Sprintf("FILEINFO_%d_%s", id, path), "size", "modTime", "sha1", "sha256", "md5"))
if err != nil {
return
}
// Note: as of today, only the size is stored by the scanners
// all other fields are left blank.
f.Size, _ = strconv.ParseInt(reply[0], 10, 64)
f.ModTime, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", reply[1])
f.Sha1 = reply[2]
f.Sha256 = reply[3]
f.Md5 = reply[4]
c.fimCache.Set(fmt.Sprintf("%d|%s", id, path), &fileInfoValue{value: f})
return
}
// GetMirror returns all information about a given mirror either from the cache
// or directly from the database if the object is not yet stored in the cache.
func (c *Cache) GetMirror(id int) (mirror Mirror, err error) {
v, ok := c.mCache.Get(strconv.Itoa(id))
if ok {
mirror = v.(*mirrorValue).value
} else {
mirror, err = c.fetchMirror(id)
if err != nil {
return
}
}
return
}