-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
497 lines (444 loc) · 10.5 KB
/
cache.go
File metadata and controls
497 lines (444 loc) · 10.5 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package icalmiddleware
import (
"encoding/gob"
"fmt"
"io"
"os"
"runtime"
"sync"
"time"
)
const (
// NoExpiration indicates that the item never expires
NoExpiration time.Duration = -1
// DefaultExpiration indicates to use the default expiration time
DefaultExpiration time.Duration = 0
)
// Item represents a cache item
type Item struct {
Object bool // The cached value
Expiration int64 // When the item expires (Unix nano time)
LastAccess time.Time // When the item was last accessed
}
// Expired returns true if the item has expired
func (item Item) Expired() bool {
if item.Expiration == 0 {
return false
}
return time.Now().UnixNano() > item.Expiration
}
// Cache is the main cache structure
type Cache struct {
*cache
}
// NewCache creates a new cache with the given expiration and cleanup interval
func NewCache(defaultExpiration, cleanupInterval time.Duration) *Cache {
items := make(map[string]Item, 100) // Pre-allocate space for better performance
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
// NewFrom creates a new cache with the given expiration, cleanup interval, and items
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
// cache is the internal cache implementation
type cache struct {
defaultExpiration time.Duration
items map[string]Item
mu sync.RWMutex
onEvicted func(string, bool)
janitor *janitor
}
// newCache creates a new cache with the given expiration and items
func newCache(de time.Duration, m map[string]Item) *cache {
if de == 0 {
de = -1
}
c := &cache{
defaultExpiration: de,
items: m,
}
return c
}
// newCacheWithJanitor creates a new cache with a janitor goroutine
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *Cache {
c := newCache(de, m)
// This trick ensures that the janitor goroutine (which--granted it
// was enabled--is running DeleteExpired on c forever) does not keep
// the returned C object from being garbage collected. When it is
// garbage collected, the finalizer stops the janitor goroutine, after
// which c can be collected.
C := &Cache{
cache: c,
}
if ci > 0 {
runJanitor(c, ci)
runtime.SetFinalizer(C, stopJanitor)
}
return C
}
// Set adds an item to the cache
func (c *cache) Set(k string, x bool, d time.Duration) {
// "Inlining" of set
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = time.Now().Add(d).UnixNano()
}
c.mu.Lock()
c.items[k] = Item{
Object: x,
Expiration: e,
LastAccess: time.Now(),
}
c.mu.Unlock()
}
// set is an internal method to set a cache item
func (c *cache) set(k string, x bool, d time.Duration) {
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = time.Now().Add(d).UnixNano()
}
c.items[k] = Item{
Object: x,
Expiration: e,
LastAccess: time.Now(),
}
}
// SetDefault adds an item to the cache with the default expiration
func (c *cache) SetDefault(k string, x bool) {
c.Set(k, x, DefaultExpiration)
}
// Add adds an item to the cache only if it doesn't already exist
func (c *cache) Add(k string, x bool, d time.Duration) error {
c.mu.Lock()
_, found := c.get(k)
if found {
c.mu.Unlock()
return fmt.Errorf("item %v already exists", k)
}
c.set(k, x, d)
c.mu.Unlock()
return nil
}
// Replace replaces an existing item in the cache
func (c *cache) Replace(k string, x bool, d time.Duration) error {
c.mu.Lock()
_, found := c.get(k)
if !found {
c.mu.Unlock()
return fmt.Errorf("item %v doesn't exist", k)
}
c.set(k, x, d)
c.mu.Unlock()
return nil
}
// Has checks if an item exists in the cache
func (c *cache) Has(k string) bool {
_, has := c.Get(k)
return has
}
// Get retrieves an item from the cache
func (c *cache) Get(k string) (bool, bool) {
c.mu.RLock()
// "Inlining" of get and Expired
item, found := c.items[k]
if !found {
c.mu.RUnlock()
return item.Object, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
c.mu.RUnlock()
return item.Object, false
}
}
c.mu.RUnlock()
// Update last access time
c.mu.Lock()
if item, found := c.items[k]; found {
item.LastAccess = time.Now()
c.items[k] = item
}
c.mu.Unlock()
return item.Object, true
}
// GetWithExpiration retrieves an item and its expiration time
func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
c.mu.RLock()
// "Inlining" of get and Expired
item, found := c.items[k]
if !found {
c.mu.RUnlock()
return nil, time.Time{}, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
c.mu.RUnlock()
return nil, time.Time{}, false
}
// Return the item and the expiration time
c.mu.RUnlock()
// Update last access time
c.mu.Lock()
if item, found := c.items[k]; found {
item.LastAccess = time.Now()
c.items[k] = item
}
c.mu.Unlock()
return item.Object, time.Unix(0, item.Expiration), true
}
// If expiration <= 0 (i.e. no expiration time set) then return the item
// and a zeroed time.Time
c.mu.RUnlock()
// Update last access time
c.mu.Lock()
if item, found := c.items[k]; found {
item.LastAccess = time.Now()
c.items[k] = item
}
c.mu.Unlock()
return item.Object, time.Time{}, true
}
// get is an internal method to get an item from the cache
func (c *cache) get(k string) (bool, bool) {
item, found := c.items[k]
if !found {
return item.Object, false
}
// "Inlining" of Expired
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
return item.Object, false
}
}
return item.Object, true
}
// Delete removes an item from the cache
func (c *cache) Delete(k string) {
c.mu.Lock()
v, evicted := c.delete(k)
c.mu.Unlock()
if evicted {
c.onEvicted(k, v)
}
}
// delete is an internal method to delete an item from the cache
func (c *cache) delete(k string) (bool, bool) {
if c.onEvicted != nil {
if v, found := c.items[k]; found {
delete(c.items, k)
return v.Object, true
}
}
delete(c.items, k)
return false, false
}
// keyAndValue represents a key-value pair
type keyAndValue struct {
key string
value bool
}
// DeleteExpired deletes all expired items from the cache
func (c *cache) DeleteExpired() {
var evictedItems []keyAndValue
now := time.Now().UnixNano()
c.mu.Lock()
for k, v := range c.items {
// "Inlining" of expired
if v.Expiration > 0 && now > v.Expiration {
ov, evicted := c.delete(k)
if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov})
}
}
}
c.mu.Unlock()
for _, v := range evictedItems {
c.onEvicted(v.key, v.value)
}
}
// DeleteLeastRecent removes the least recently accessed items when the cache exceeds the given size
func (c *cache) DeleteLeastRecent(maxSize int) {
if len(c.items) <= maxSize {
return
}
// Find items to delete
var itemsToDelete []string
var oldestTime time.Time
var oldestKey string
c.mu.RLock()
// Initialize with the first item
for k, v := range c.items {
oldestTime = v.LastAccess
oldestKey = k
break
}
// Find the oldest items
for len(c.items) - len(itemsToDelete) > maxSize {
// Find the oldest item
for k, v := range c.items {
if v.LastAccess.Before(oldestTime) && !contains(itemsToDelete, k) {
oldestTime = v.LastAccess
oldestKey = k
}
}
itemsToDelete = append(itemsToDelete, oldestKey)
// Reset for next iteration
oldestTime = time.Now()
for k, v := range c.items {
if v.LastAccess.Before(oldestTime) && !contains(itemsToDelete, k) {
oldestTime = v.LastAccess
oldestKey = k
}
}
}
c.mu.RUnlock()
// Delete the items
c.mu.Lock()
for _, k := range itemsToDelete {
delete(c.items, k)
}
c.mu.Unlock()
}
// contains checks if a string is in a slice
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
// OnEvicted sets a callback for when items are evicted
func (c *cache) OnEvicted(f func(string, bool)) {
c.mu.Lock()
c.onEvicted = f
c.mu.Unlock()
}
// Save serializes the cache to an io.Writer
func (c *cache) Save(w io.Writer) (err error) {
enc := gob.NewEncoder(w)
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("error registering item types with Gob library")
}
}()
c.mu.RLock()
defer c.mu.RUnlock()
for _, v := range c.items {
gob.Register(v.Object)
}
err = enc.Encode(&c.items)
return
}
// SaveFile saves the cache to a file
func (c *cache) SaveFile(fname string) error {
fp, err := os.Create(fname)
if err != nil {
return err
}
err = c.Save(fp)
if err != nil {
fp.Close()
return err
}
return fp.Close()
}
// Load deserializes the cache from an io.Reader
func (c *cache) Load(r io.Reader) error {
dec := gob.NewDecoder(r)
items := map[string]Item{}
err := dec.Decode(&items)
if err == nil {
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range items {
ov, found := c.items[k]
if !found || ov.Expired() {
c.items[k] = v
}
}
}
return err
}
// LoadFile loads the cache from a file
func (c *cache) LoadFile(fname string) error {
fp, err := os.Open(fname)
if err != nil {
return err
}
err = c.Load(fp)
if err != nil {
fp.Close()
return err
}
return fp.Close()
}
// Items returns all unexpired items in the cache
func (c *cache) Items() map[string]Item {
c.mu.RLock()
defer c.mu.RUnlock()
m := make(map[string]Item, len(c.items))
now := time.Now().UnixNano()
for k, v := range c.items {
// "Inlining" of Expired
if v.Expiration > 0 {
if now > v.Expiration {
continue
}
}
m[k] = v
}
return m
}
// ItemCount returns the number of items in the cache
func (c *cache) ItemCount() int {
c.mu.RLock()
n := len(c.items)
c.mu.RUnlock()
return n
}
// Flush removes all items from the cache
func (c *cache) Flush() {
c.mu.Lock()
c.items = map[string]Item{}
c.mu.Unlock()
}
// janitor cleans up expired items at regular intervals
type janitor struct {
Interval time.Duration
stop chan bool
}
// Run runs the janitor
func (j *janitor) Run(c *cache) {
ticker := time.NewTicker(j.Interval)
for {
select {
case <-ticker.C:
c.DeleteExpired()
// Also clean up if the cache gets too big (more than 10000 items)
c.DeleteLeastRecent(10000)
case <-j.stop:
ticker.Stop()
return
}
}
}
// stopJanitor stops the janitor
func stopJanitor(c *Cache) {
c.janitor.stop <- true
}
// runJanitor starts the janitor
func runJanitor(c *cache, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan bool),
}
c.janitor = j
go j.Run(c)
}